环境变量
- ANDROID_HOME(安卓SDK路径)
- ANDROID_NDK_HOME(安卓NDK路径,>=r14b,但是实测r14b不可用,r20可用)
- GOPATH(GOPATH目录,需要bind的代码在这里写)
- GOROOT(GO根目录)
- JAVA_HOME(JDK根路径,不是JRE,因为需要JAVAC)
- PATH
- %JAVA_HOME%/bin(JAVAC)
- %GOROOT%/bin(GO)
- %GOPATH%/bin(gomobile,gobind)
- CLASSPATH(.;JDKLIB;YOUR-LIBS)
编译gomobile,gobind
- 获取源码
go get golang.org/x/mobile/cmd/gomobile
git clone https://github.com/golang/mobile.git
- https://gopm.io/download?pkgname=golang.org/x/mobile/cmd/gomobile
- 编译gomobile
go build golang.org/x/mobile/cmd/gomobile
- 配置gomobile
gomobile init -ndk NDK路径
- 编译gobind
gomobile init
使用
Android Studio中新建一个Java Libary Module
新建完Module之后,删除src和lib,只保留以下文件即可,其他的都没有用
编辑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中自己将要写的源码
/* GOPATH where the Go package is; check `go env` */
// GOPATH = "D:\\Work\\go\\gopath" // 如果环境变量配置了GOPATH,这里不需要
/* Absolute path to the go binary */
// GO = "D:\\Work\\go\\bin\\go.exe" // 如果GO配置进了PATH,则不需要
/* 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" // 如果gomobile在PATH中,此行不需要
}
Android App Model引用该Module
Build !!!