android编译golang,在Android中引用golang编译的aar包-Go语言中文社区

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

不过它不需要像C++那样去手动回收内存。

支持的类型

Signed integer and floating point types.

String and boolean types.

Byte slice types. Note that byte slices are passed by reference,

and support mutation.

Any function type all of whose parameters and results have

supported types. Functions must return either no results,

one result, or two results where the type of the second is

the built-in 'error' type.

Any interface type, all of whose exported methods have

supported function types.

Any struct type, all of whose exported methods have

supported function types and all of whose exported fields

have supported types.

基本类型也就是

string(不支持string数组)

bool

int(java这边引用的时候会是long)

byte[]

传递返回值无法传递数组,可以将数据转成json格式然后通过string或者byte array传递过来,这边再解析。最好不要通过for循环频繁调用,因为他们之间的通讯是有代价的。

配置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/mobileclone到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目录下。可以不用设置。(如果gomobile的目录在GOPATH里)

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

这里主要是将安卓工程和对应的go工程通过联系起来,方便开发,而不是每次改动go都需要重新生成aar,然后导入aar。这里通过这个配置每次编译运行安卓工程都会自动生成并且更新aar。

通过命令生成aar

有的时候可能就是想要一个aar,没必要建一个安卓工程配置各种东西。

这时候有一个go工程就好了,要把需要提供给外部使用的方法放到一个目录下,然后对外开放的方法设置为public的(方法名开头大写)。然后对这个目录运行gomobile bind命令

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

这个命令会在当前运行的目录底下生成.aar好source.jar文件

这里的hello就是需要打包的go文件所在的目录。

需要注意的地方:

后面的目录写从GOPATH/src后面开始就可以了,不要写绝对路径。

不要写到文件名,写到文件名上一级的文件夹名称就可以了。他会把这个文件夹里所有的public方法都对外提供。

可能遇到的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值