在Android中引用golang編譯的aar包

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方法都對外提供。

可能遇到的問題

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評論 25 708
  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,144評論 2 59
  • 一,環(huán)境搭建 1、安裝Go a. 下載地址為:https://golang.org/dl/ b. 這里下載到的版本...
    殤月123閱讀 5,413評論 0 2
  • 在去年oracle和谷歌的官司告一段落,但是Android開發(fā)者仍急切谷歌能夠想蘋果一樣,開發(fā)自己的編程語言進行A...
    Tenny1225閱讀 7,308評論 3 10
  • 文| 夏喜冬 01 “我的意中人是個蓋世英雄,有一天他會踩著七色云彩來娶我。” 曾經,《大話西游》中紫霞仙子對至尊...
    夏喜冬閱讀 959評論 4 4

友情鏈接更多精彩內容