在Android中引用golang编译的aar包

栏目: Go · 发布时间: 7年前

内容简介:gomobile是在安卓中使用golang的工程,既可以全部代码用golang,也可以引用部分golang的代码,但是现在还不成熟,还是试验阶段。最好将目录如果go get不下来gomobile的话,可以将镜像工程:

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文件


以上所述就是小编给大家介绍的《在Android中引用golang编译的aar包》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Haskell

Haskell

Simon Thompson / Addison-Wesley / 1999-3-16 / GBP 40.99

The second edition of Haskell: The Craft of Functional Programming is essential reading for beginners to functional programming and newcomers to the Haskell programming language. The emphasis is on th......一起来看看 《Haskell》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试