gomobile是在安卓中使用golang的工程,既可以全部代码用golang,也可以引用部分golang的代码,但是现在还不成熟,还是试验阶段。

配置gomobile的环境

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

$ gomobile init # it might take a few minutes

最好将目录 $GOPATH/bin

加到环境变量,不然运行gomobile命令还需要进入到 GOPATH/bin

目录下。

如果go get不下来gomobile的话,可以将镜像工程: https://github.com/golang/mobile

clone到 GOPATH/src/golang.org/x

目录下

gomobile init之前需要环境变量中配置了ndk环境,或者通过ndk标签指定ndk目录 gomobile init -ndk ~/soft-code/android-ndk-r14b

,试过经典的 android-ndk-r10e

会报一个pyton错误。

运行sample测试环境是否成功

使用android studio导入 $GOPATH/src/golang.org/x/mobile/example/bind/android

项目。

打开hello模块底下的build.gradle填充里面的目录

plugins {

id "org.golang.mobile.bind" version "0.2.13"

}

gobind {

/* The Go package path; must be under one of the GOPATH elements or

a relative to the current directory (e.g. ../../hello) */

pkg = "golang.org/x/mobile/example/bind/hello"

/* GOPATH where the Go package is; check `go env` */

GOPATH = "~/go"

/* Absolute path to the go binary */

GO = "/usr/local/bin/go"

/* Optionally, set the absolute path to the gomobile binary if the

/* gomobile binary is not located in the GOPATH's bin directory. */

// GOMOBILE = "~/go/src/golang.org/x/mobile"

}

需要修改几个变量,一个是 GOPATH

这个只需要写自己 go env

里的gopath就可以。

GO目录,其实就是 go 的安装目录,在mac下可以通过命令 which go

找到对应的安装路径。

第三个GOMOBILE就是指gomobile可执行文件的路径,一般是在GOPATH/bin目录下。可以不用设置。

接下来编译运行对应的安卓工程应该就ok了,可以看到他在hello的model里有一个aar文件。

还可以直接通过命令生成aar文件

gomobile bind -target=android golang.org/x/mobile/example/bind/hello

会在当前目录底下生成.aar好source.jar文件

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐