gomobile是在安卓中使用golang的工程雌澄,既可以全部代碼用golang滚婉,也可以引用部分golang的代碼豁陆,但是現(xiàn)在還不成熟,還是試驗階段沛慢。
不過它不需要像C++那樣去手動回收內(nèi)存赡若。
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ù)轉(zhuǎn)成json格式然后通過string或者byte array傳遞過來团甲,這邊再解析逾冬。最好不要通過for循環(huán)頻繁調(diào)用,因為他們之間的通訊是有代價的躺苦。
配置gomobile的環(huán)境
$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init # it might take a few minutes
最好將目錄$GOPATH/bin
加到環(huán)境變量身腻,不然運行g(shù)omobile命令還需要進入到GOPATH/bin
目錄下。
如果go get不下來gomobile的話匹厘,可以將鏡像工程:https://github.com/golang/mobile
clone到GOPATH/src/golang.org/x
目錄下
gomobile init之前需要環(huán)境變量中配置了ndk環(huán)境嘀趟,或者通過ndk標簽指定ndk目錄gomobile init -ndk ~/soft-code/android-ndk-r14b
,試過經(jīng)典的android-ndk-r10e
會報一個pyton錯誤愈诚。
運行sample測試環(huán)境是否成功
使用android studio導(dǎo)入$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
找到對應(yīng)的安裝路徑。 - 第三個GOMOBILE就是指gomobile可執(zhí)行文件的路徑陵刹,一般是在GOPATH/bin目錄下丈攒。可以不用設(shè)置授霸。(如果gomobile的目錄在GOPATH里)
接下來編譯運行對應(yīng)的安卓工程應(yīng)該就ok了巡验,可以看到他在hello的model里有一個aar文件。
這里主要是將安卓工程和對應(yīng)的go工程通過聯(lián)系起來碘耳,方便開發(fā)显设,而不是每次改動go都需要重新生成aar,然后導(dǎo)入aar辛辨。這里通過這個配置每次編譯運行安卓工程都會自動生成并且更新aar捕捂。
通過命令生成aar
有的時候可能就是想要一個aar,沒必要建一個安卓工程配置各種東西斗搞。
這時候有一個go工程就好了指攒,要把需要提供給外部使用的方法放到一個目錄下,然后對外開放的方法設(shè)置為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