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

配置gomobile的环境

$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init # it might take a few minutes
$GOPATH/binGOPATH/bin
https://github.com/golang/mobileGOPATH/src/golang.org/x
gomobile init -ndk ~/soft-code/android-ndk-r14bandroid-ndk-r10e

运行sample测试环境是否成功

$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"
}
GOPATHgo envwhich go

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

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

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

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