這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
在去年oracle和Google的官司告一段落,但是Android開發人員仍急切Google能夠想蘋果一樣,開發自己的程式設計語言進行Android應用的開發,至於選擇什麼語言,大多數開發人員希望是golang,首先他是Google自我研發的一門程式設計語言,因為他文法簡單,運行效率高。
隨著golang迭代更新,我們漸漸看到Google在這一方面的努力,golang.org/x/mobile的發布,讓開發人員能夠使用go語言使用ndk進行安卓應用的開發。這個連結http://www.jianshu.com/p/403aa507935b 介紹了如何使用gomobile,進行安卓應用的開發和安卓庫檔案的開發。
但是使用過程中就會發現一個問題,go開發安卓app,要麼全部使用go用ndk進行開發(使用OpenGL繪製介面),要麼只能用來寫庫檔案讓java進行調用(打包一個aar檔案進行調用),並不能像kotlin語言那樣,真正做到文法和編程上的一一映射。
不過最近偶然發現mobile庫有更新,readme為Go reverse bind android app in 100% Go.go反轉和Android應用的100%綁定(翻譯可能不到位,英文太差)。
1 安裝gomobile
這裡需要go get mobile這個包,命令為go get golang.org/x/mobile/cmd/gomobile。因為googlesource被 牆,你可以去github下載相關檔案,地址為https://github.com/golang/mobile,將這個mobile檔案夾放到GOPATH>src>golang.org>x 下面,然後執行go get golang.org/x/mobile/cmd/...,此時你會在GOPATH>bin這個檔案夾下面看到產生了兩個可執行檔,一個是gobind,一個是gomobile。
2 初始化gomobile
使用gomobile init -v初始化,此時可能需要等待一段時間(直接gomobile init -v 命令,必須保證ndk 已經加入了環境變數path下面,如果沒有加入,也可以使用gomobile init -v -ndk 你的ndk目錄 進行初始化)
3 與Android studio配合使用
這裡直接使用官方的example,開啟Android studio,點擊File>New>Import Moudle,將GOPATH>src>golang.org>x>mobile>example>reverse>android匯入到你的project中,你會發現多了一個android的moudle,這裡你會發現,啟動的activity是用golang寫的,源碼在golang.org/x/mobile/example/reverse/reverse/reverse.go這個檔案裡,下面是源碼
package reverseimport ( "Java/android/databinding/DataBindingUtil" "Java/android/os" "Java/android/support/v7/app" gopkg "Java/reverse" rlayout "Java/reverse/R/layout" "Java/reverse/databinding" "Java/reverse/databinding/ActivityMainBinding")type MainActivity struct { app.AppCompatActivity binding databinding.ActivityMainBinding}func (a *MainActivity) OnCreate(this gopkg.MainActivity, b os.Bundle) { this.Super().OnCreate(b) db := DataBindingUtil.SetContentView(this, rlayout.Activity_main) a.binding = ActivityMainBinding.Cast(db) a.binding.SetAct(this)}func (a *MainActivity) OnDestroy(this gopkg.MainActivity) { a.binding = nil // break reference cycle this.Super().OnDestroy()}func (a *MainActivity) GetLabel() string { return "Hello from Go!"}
可以發現和我們平時是用java編寫activity非常的相似,具體的使用,大家可以自己探索
查看這個moudle的build.gradle檔案,會看到下面的代碼
gobind { pkg = "golang.org/x/mobile/example/reverse/reverse"}
發現linux系統下面必須添加GOPATH的路徑
gobind { pkg = "golang.org/x/mobile/example/reverse/reverse" GOPATH="/home/tenny/gopath"}
此時點擊run按鈕,就會發現app安裝到手機上面了
webwxgetmsgimg.jpg
可能出現的問題(坑死了)
Android studio 報 'gomobile' finished with non-zero exit value 1錯誤
查看gradle console,錯誤為 'gomobile' go not found
這個錯誤是我deepin linux系統出現的,window上沒有出現,go not found證明go執行檔案沒有找到,但是單獨開啟命令列,使用go 命令是可用的,後面發現原來系統只會找/usr/local/go/bin 下面的執行檔案,所以需要將go語言套件放在/usr/local/處,同時需要修改一下環境變數。
Android studio報 javac cannot execute binary file
這個錯誤和上邊一個很像,也是在linux上邊發現的,window上沒有出現,如果你是手動安裝的jdk,這時你需要使用adt-get 安裝jdk,具體方法自行百度。
3.
$ gomobile bind golang.org/x/mobile/example/reverse/reversegomobile: loadExportData failed go install -pkgdir=/Users/hajimehoshi/go/pkg/gomobile/pkg_android_arm -tags="" -gcflags=-shared -ldflags=-shared golang.org/x/mobile/example/reverse/reverse failed: exit status 1../../../golang.org/x/mobile/example/reverse/reverse/reverse.go:9:2: cannot find package "Java/android/databinding/DataBindingUtil" in any of: /usr/local/go/src/Java/android/databinding/DataBindingUtil (from $GOROOT) /var/folders/7t/qw3np69559591s1v0mk5_p1m0000gn/T/gomobile-work-600308528/gen/src/Java/android/databinding/DataBindingUtil (from $GOPATH) /Users/hajimehoshi/go/src/Java/android/databinding/DataBindingUtil../../../golang.org/x/mobile/example/reverse/reverse/reverse.go:11:2: cannot find package "Java/android/support/v7/app" in any of: /usr/local/go/src/Java/android/support/v7/app (from $GOROOT) /var/folders/7t/qw3np69559591s1v0mk5_p1m0000gn/T/gomobile-work-600308528/gen/src/Java/android/support/v7/app (from $GOPATH) /Users/hajimehoshi/go/src/Java/android/support/v7/app../../../golang.org/x/mobile/example/reverse/reverse/reverse.go:12:2: cannot find package "Java/go/reverse/R/layout" in any of: /usr/local/go/src/Java/go/reverse/R/layout (from $GOROOT) /var/folders/7t/qw3np69559591s1v0mk5_p1m0000gn/T/gomobile-work-600308528/gen/src/Java/go/reverse/R/layout (from $GOPATH) /Users/hajimehoshi/go/src/Java/go/reverse/R/layout../../../golang.org/x/mobile/example/reverse/reverse/reverse.go:13:2: cannot find package "Java/go/reverse/databinding/ActivityMainBinding" in any of: /usr/local/go/src/Java/go/reverse/databinding/ActivityMainBinding (from $GOROOT) /var/folders/7t/qw3np69559591s1v0mk5_p1m0000gn/T/gomobile-work-600308528/gen/src/Java/go/reverse/databinding/ActivityMainBinding (from $GOPATH) /Users/hajimehoshi/go/src/Java/go/reverse/databinding/ActivityMainBinding
這個錯誤在mac上會出現,這是mobile這個庫的問題,幸虧有大神做出了修改,需要到googlesource上checkout相關分支
git fetch https://go.googlesource.com/mobile refs/changes/35/38635/1 && git checkout FETCH_HEAD
googlesource 因為被 牆,可能無法git下來,不過別擔心,我在百度雲上上傳一個備份http://pan.baidu.com/s/1hsDUp28 , 大家可以下載下來,然後替換mobile檔案就行。