2015年9月21日 星期一

[Golang]GoMobile的安裝與建置 (Android)

有個風傳是如無法支援 Mobile ,那麼這個語言的前景有限。 Go 一直有被這風傳掃到,雖然之前還是有方法可給 mobile 使用,不過 Go 開發Team的人自己吐槽說很難用,搞了半天心都死了。 可喜可賀的是,Go  在 1.5 正是支援了 跨平台的到 Mobile 的工具叫 gomobile! 這消息並沒有在 google io 發佈,所以好像也沒有啥麼 Big Deal。但我覺得這是Go 即將要崛起的一個重要序曲。 Go 不但能寫後端 還可以跨到 Mobile , 甚至可以獨立完全開發 Android APK 真是太棒! 當然很多語言都可以,但很多語言就沒 Go 這麼簡單美麗而強大呀呀呀!

GoMobile Installation and Building

Command gomobile
gomobile 是用來build & run 用go 寫的 app 。 Go 語言版本需要 1.5 以上。

Go Mobile GitHub
gomobile 的 github 有比較詳盡的介紹

GoDoc for gomobile
gomobile package 的文件

Go Mobile ReadMe Note:  

根據 Gomobile github 上 ReadMe 的筆記跟做心得

Go Mobile Tool:
Go Mobile 提供兩個方法,一個是全部用 Go build 的 App , 一個是 build 成 SDK給 Java/Android/ios 使用



下載安裝 gomobile :
$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init # it might take a few minutes


**gomobile init  會將 gomobile.exe 加到 gopath 的 bin 裡。如果go workspace 跟安裝 go 的目錄不一樣,會找不到 gomobile , 可把這 path 加到環境變數就可找到。

Native Application (使用 go build 的 android/ios app)

  • 目前 Go Package 只支援一部分的 packages,主要是下面功能的
  • App control and configuration
  • OpenGL ES 2 bindings
  • Asset management
  • Event management
  • Experimental packages include OpenAL bindings, audio, font, sprite and motion sensors
  • Build Android Sample Code

  • Build \golang.org\x\mobile\example\basic  這個 example
  •  如果 golang 裡沒有這個目錄,使用下面取得  
    • go get -d golang.org/x/mobile/example/basic
  • gomobile build : build 成 Android apk 
    • gomobile build -target=android golang.org/x/mobile/example/basic
  • gomobile install:  將 apk 安裝到 Android device 但前提是你有裝 adb 還有就是抓得到 device, 事實上直接用 adb 安裝還比較安心 .  gomobile 的 cmd 是:
    •  gomobile install golang.org/x/mobile/example/basic
  • Result of basic.apk  同目錄下產生一個 basic.apk
  • Native Application 安裝問題
    目前我碰到的是 路徑問題, 所以要檢查一下 gomobile.exe 儲存在哪? 如果 build 不起來, 直接到 要 build 的目錄下 build 跟 install 試試看。

  • Build ios Sample Code (我還不會 ios 先這樣)


  •  SDK applications and generating bindings: 產生 mobile 可用的 sdk

    • 好處是: 1. 只要小改就可以 reuse 你的 go library 2. 如果你的 ios & android 有使用到相同的 code , 那麼只要寫一隻 go function 然後 跟android & ios bind 就可以啦! 
    • 目前有的限制:
    • 只支援一部分的 go type
    • bind code 會有 performance overhead
    • API function 應該長怎樣會根據要 bind 的語言會有些微的不同
    • Support Type
        - Signed integer and floating point types.
        - String and boolean types.
        - Byte slice types. Note the current implementation does not
          support data mutation of slices passed in as function arguments.
          (https://golang.org/issues/12113)
        - 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.

    ** 看起來 unsigned 都要改呀!  還有 function pass slice  也要小心。 只可以沒有Return, Return 一個,或是 (return 一個 +  一個 Error)
    • Building and deploying to Android
    • Android SDK sample code 在  src\golang.org\x\mobile\example\bind\android
    • 在 AndroidStudio 裡 import src\golang.org\x\mobile\example\bind\android\App
    • AndroidStudio 裡注意一下這是用 gradle-2.4-all.zip ,沒有,需要下載。下載很簡單,到Terminal 下 gradlew task,我是下  gradlew assembleDebug 就自動幫忙下載了。
    • 下 gradlew installDebug 就 install 到手機裡了 AppName: GoBindExample
    •  也可用 Build-> Make Project

     

     










沒有留言:

張貼留言