gomobile是在安卓中使用golang的工程,既可以全部代碼用golang,也可以引用部分golang的代碼,但是現(xiàn)在還不成熟,還是試驗階段。
不過它不需要像C++那樣去手動回收內存。
gomobile介紹wiki
https://github.com/golang/go/wiki/Mobile#sdk-applications-and-generating-bindings
gomobile地址
https://github.com/golang/mobile
支持的類型
- 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.
https://godoc.org/golang.org/x/mobile/cmd/gobind
基本類型也就是
- string(不支持string數(shù)組)
- bool
- int(java這邊引用的時候會是long)
- byte[]
傳遞返回值無法傳遞數(shù)組,可以將數(shù)據(jù)轉成json格式然后通過string或者byte array傳遞過來,這邊再解析。最好不要通過for循環(huán)頻繁調用,因為他們之間的通訊是有代價的。
配置gomobile的環(huán)境
$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init # it might take a few minutes
最好將目錄$GOPATH/bin加到環(huán)境變量,不然運行gomobile命令還需要進入到GOPATH/bin目錄下。
如果go get不下來gomobile的話,可以將鏡像工程:https://github.com/golang/mobileclone到GOPATH/src/golang.org/x目錄下
gomobile init之前需要環(huán)境變量中配置了ndk環(huán)境,或者通過ndk標簽指定ndk目錄gomobile init -ndk ~/soft-code/android-ndk-r14b,試過經典的android-ndk-r10e會報一個pyton錯誤。
運行sample測試環(huán)境是否成功
使用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可執(zhí)行文件的路徑,一般是在GOPATH/bin目錄下??梢圆挥迷O置。(如果gomobile的目錄在GOPATH里)
接下來編譯運行對應的安卓工程應該就ok了,可以看到他在hello的model里有一個aar文件。
這里主要是將安卓工程和對應的go工程通過聯(lián)系起來,方便開發(fā),而不是每次改動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方法都對外提供。
可能遇到的問題
- Permission denied
解決方法:Cause: error=13, Permission denied
https://stackoverflow.com/questions/28564677/android-studio-error-13-permission-denied-in-linux