Golang for Android or iOS 使用記錄

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

[TOC]

環境

  • OSX 10.10.5
  • jdk1.8.0_92
  • golang 1.7
  • Android SDK 25.1.7
  • Andorid-NDK-r12b
  • Andorid Studio 1.5.1
  • xcode 7.2.1
  • swift 2.1.1

目前golang bind 支援的類型說明

https://godoc.org/golang.org/x/mobile/cmd/gobind

安裝 gomobile

go get -u -v golang.org/x/mobile

會報告一個錯誤

package golang.org/x/mobile: no buildable Go source files in /Users/sinlov/go/src/golang.org/x/mobile

無視,這個是因為這不是一個可以build的項目,你以後也需要這些庫,全down下來吧

遇到類似的問題可以使用 -d 參數來規避test代碼導致的無法build問題,不過如果本來就不是一個可以build的目錄,go get 命令會去嘗試構建,仍然會警告你

  • 安裝 go mobile
go get -v golang.org/x/mobile/cmd/gomobile

然後,翻牆走起,運行

gomobile init

初始化會等幾分鐘,看網速,初始化後才可以正式使用!

gomobile 介紹

gomobile 可以讓golang在行動裝置中使用

  • bind 動態庫方式native開發
  • build 直接產生行動裝置 App
  • install 將產生的app,安裝到裝置或者模擬器
  • clean 清空緩衝

一般使用bind方式開發,build方式還是實驗性的

在行動裝置中使用 golang 動態串連庫

  • 準備

gomobile bind 查看協助文檔

gomobile bind -help

執行這個命令的時候注意 Go版本 GOPATH 等,這是go開發的基礎,不屬於這篇文章的內容

一個沒有錯誤的golang專案檔包,注意檔案包的包名,後面會用到

後面樣本的golang虛擬碼為 package.go

package [GoPackageName]func[GoFunction] (in tpye) type {}

Android 使用

gomobile bind -target=android [packageName]gomobile bind -target=android -o [output][packageName]

會在目前的目錄產生這個包名命名的一個 .arr檔案

預設情況下輸出支援平台是 armeabi-v7a arm64-v8a x86 x86_64

使用 Android Studio 匯入這個 arr 檔案後

Android 使用類似

import go.package.[GoPakcageName];private void (){  [GoPakcageName].[GoFunction]();}

iOS 使用

gomobilebind-target=ios[packageName]gomobilebind-target=ios-o [output][packageName]

會產生一個對應 [工程包名].framework 的 bundle,把這個 bundle 拖拽到 iOS 項目中

使用的時候類似

#import"hello/Hello.h"Go[PackageName][GoFunction](@ type);

如果想使用 swift

swift 自動產生協助文檔

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

[package].Go[PackageName][GoFunction](type)

使用golang的意義

這個是最容易撕逼的地方,技術的意義就是自己閉嘴做,別管別人怎麼說

golang for mobile 本質是讓go build 的時候使用 arm 或者 x86 指令集進行編譯,和移動的native開發沒啥區

你可以使用

gomobile bind-target=android/arm,android/386 [pakcageName]

來指定對應的輸出平台

  • 好處是可以共用代碼,比開發native效率高不少
  • 壞處是go產生的當地套件比較大2MB以上

在未來,這個so檔案的大小不是問題,當前環境可能會讓某些強迫症技術負責受不了

當年謠傳,比爾蓋茨說記憶體只需要640K,這個謠傳真假不知道,只知道現在手機隨便RAM都是2G,ROM起跑16G

利用strip 工具來減少Android 的 so的大小

strip 工具會去掉debug和符號資訊

測試可以減少到原來的 60%左右工具在 android-ndk 的目錄中

類似

cd android-ndk/android-ndk-r12b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/arm-linux-androideabi/bin./strip [yourOSFile]

最佳化前需要備份,並測試一下是否最佳化後是否可以使用

golang運行效率問題,native bind效率問題

單獨拉出來說的原因是,native bind肯定存在效率問題,沒有爭議的,看你的功能是否不在意效率

golang的運行效率問題,大兄弟,語言的運行效率肯定在程式員的手上,沒有廢材的語言,只有不努力的程式員

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.