GOMOBILE写Android和iOS移动端SDK

来源:互联网 发布:mac打字左下角 编辑:程序博客网 时间:2024/06/07 18:42

GOMOBILE

设置GO环境以及GOPATH,参考GO环境设置。

GOMOBILE资料参考官方WIKI,这个WIKI貌似对SDK和NDK没有说清楚。

Android SDK+NDK

下载和安装AndroidSDK和AndroidNDK,MacOS安装到:

# AndroidSDK/Users/winlin/Library/Android/sdk# AndroidNDK/Users/winlin/Library/Android/sdk/ndk-bundle

也就是看到下面的文件就算成功:

Mac:sdk winlin$ ls /Users/winlin/Library/Android/sdkbuild-tools extras      ndk-bundle  platform-tools  skins       system-images cmake     licenses    patcher     platforms   sources     toolsMac:sdk winlin$ ls /Users/winlin/Library/Android/sdk/ndk-bundleCHANGELOG.md        ndk-gdb         prebuilt        sources android-ndk-r13b    ndk-stack       python-packages     toolchains build            ndk-which       shader-tools    ndk-build       package.xml     simpleperf  ndk-depends     platforms       source.properties

一般AndroidStudio的local.properties文件,这个是自动生成的,会默认这指定SDK和NDK的地址:

Mac:hello-jni winlin$ cat android-ndk/hello-jni/local.properties ## This file is automatically generated by Android Studio.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!ndk.dir=/Users/winlin/Library/Android/sdk/ndk-bundlesdk.dir=/Users/winlin/Library/Android/sdk

参考项目android-ndk/hello-jni。

GOMOBILE TOOLS

直接下载和安装gomobile:

go get golang.org/x/mobile/cmd/gomobile

备注:如果被墙了,可以自己把gomobile项目clone到$GOPATH/src/golang.org/x下面,GITHUB地址。

先设置下SDK的环境变量给gomobile用,当然如果NDK在其他地方需要指定NDK路径。然后初始化gomobile:

export ANDROID_HOME=/Users/winlin/Library/Android/sdk$GOPATH/bin/gomobile init

注意:如果NDK没有安装对,gomobile init不会出错,但是后面gomobile bind会出错。

GO Code

GOLANG的CODE非常简单,参考gomobile/examples/bind/hello:

// Copyright 2015 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Package hello is a trivial package for gomobile bind example.package helloimport "fmt"func Greetings(name string) string {    return fmt.Sprintf("Hello, %s!", name)}

备注:需要按照GOLANG的package的规则,放在$GOPATH/src下面,否则编译会报错。

Build Mobile SDK

然后编译AndroidNDK用的库:

$GOPATH/bin/gomobile bind -target=android

生成了Android可以引用的.aar,直接搞定:

Mac:temp winlin$ ls -lhtotal 5712-rw-r--r--  1 winlin  staff   2.8M Mar  9 10:31 hello.aar-rw-r--r--  1 winlin  staff   333B Mar  9 10:28 hello.go

还可以生成iOS用的库:

$GOPATH/bin/gomobile bind -target=ios

可以看到iOS的库:

Mac:temp winlin$ ls -lh Hello.framework/total 32lrwxr-xr-x  1 winlin  staff    24B Mar  9 10:32 Headers -> Versions/Current/Headerslrwxr-xr-x  1 winlin  staff    22B Mar  9 10:32 Hello -> Versions/Current/Hellolrwxr-xr-x  1 winlin  staff    24B Mar  9 10:32 Modules -> Versions/Current/Moduleslrwxr-xr-x  1 winlin  staff    26B Mar  9 10:32 Resources -> Versions/Current/Resourcesdrwxr-xr-x  4 winlin  staff   136B Mar  9 10:32 Versions

库的大小:

Mac:temp winlin$ du -sh *5.1M    Hello.framework2.8M    hello.aar4.0K    hello.go

Android APP

新建一个Android APP,目录在/Users/winlin/git/MyApplication

hello.aar链接或拷贝到app/libs下面:

cd ~/git/MyApplication/app/libsln -sf $GOPATH/src/temp/hello.aar

然后修改app/build.gradle(在Android Studio中也可以改,打开Gradle Scripts => build.gradle(Module: app)这个文件就是),添加下面内容:

repositories {    flatDir {        dirs 'libs'    }}dependencies {    compile(name: 'hello', ext: 'aar')}

注意:dependencies一般都有这个了,只需要把compile加进去就可以。

注意:compilename不能瞎写,和那个hello.aar 文件名一致。

然后在项目中就可以看到内容了:

Libraries

在MainActivity中可以直接引用:

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView txt = new TextView(this);        txt.setText(Hello.greetings("SRS"));        setContentView(txt);    }}

运行结果:

GOMOBILE for Android

iOS APP

新建一个iOS的Application,选Swift语言:
Create Project

Main.storyboard中,添加一个UILabel控件:
Add UILabel

ViewController.swift中引用UILabel控件为成员。在Main.storyboard中选中label后,右键,拖New Referencing OutletViewController.swift中:
Map

输入这个引用,也就是成员变量的名字:
Map

可以看到创建了一个引用:
Map

viewDidLoad中修改Label的值:

class ViewController: UIViewController {    @IBOutlet weak var txtMain: UILabel!    override func viewDidLoad() {        super.viewDidLoad()        txtMain.text = "Hello, iOS"    }

运行就可以看到Label的值改变了。下面引入Hello.framework,先拷贝到项目下面:

cd ~/git/HelloWorldln -sf $GOPATH/src/temp/Hello.framework

文件如下:

Mac:HelloWorld winlin$ ls -lhtotal 0drwxr-xr-x  7 winlin  staff   238B Mar  9 13:56 Hello.frameworkdrwxr-xr-x  7 winlin  staff   238B Mar  9 13:48 HelloWorlddrwxr-xr-x  5 winlin  staff   170B Mar  9 13:56 HelloWorld.xcodeprojdrwxr-xr-x  4 winlin  staff   136B Mar  9 13:21 HelloWorldTestsdrwxr-xr-x  4 winlin  staff   136B Mar  9 13:21 HelloWorldUITestsMac:HelloWorld winlin$ ls -lh Hello.framework/total 32lrwxr-xr-x  1 winlin  staff    24B Mar  9 13:56 Headers -> Versions/Current/Headerslrwxr-xr-x  1 winlin  staff    22B Mar  9 13:56 Hello -> Versions/Current/Hellolrwxr-xr-x  1 winlin  staff    24B Mar  9 13:56 Modules -> Versions/Current/Moduleslrwxr-xr-x  1 winlin  staff    26B Mar  9 13:56 Resources -> Versions/Current/Resourcesdrwxr-xr-x  4 winlin  staff   136B Mar  9 13:56 Versions

在xcode中,添加framework:

Add Framework

在代码中引入framework:

Import Framework

代码改成这样:

import UIKitimport Helloclass ViewController: UIViewController {    @IBOutlet weak var txtMain: UILabel!    override func viewDidLoad() {        super.viewDidLoad()        txtMain.text = HelloGreetings("SRS")    }

调用的是SDK中的函数。运行结果:

GOMOBILE for iOS

Conclusion

总结下:

  1. 直接用AndroidNDK编写SDK,需要自己写JNI。而gomobile一个命令,把脏活累活都给弄好了。便利性gomobile占优。
  2. 可以一份代码支持Android和iOS,维护上比较方便。
  3. 体积上,gomobile的so最起码有2.8MB,比C要大很多。
  4. 成熟度上,gomobile目前还没release,虽然2015就已经在GO大会上出现了。
0 0