環(huán)境配置
- 安裝Docker
- 安裝golang
- 安裝xgo鏡像及編譯指令
- 配置環(huán)境變量
1. 安裝docker
針對Mac和Windows可以直接下載安裝包進行安裝,其他系統(tǒng)可以使用命令行進行安裝.參考鏈接,最后通過docker version 指令進行查看
# Jacen at EveeedeMacBook-Pro.local in ~ [10:51:52] docker version
Client:
Version: 1.12.5
API version: 1.24
Go version: go1.6.4
Git commit: 7392c3b
Built: Fri Dec 16 06:14:34 2016
OS/Arch: darwin/amd64
Server:
Version: 1.12.5
API version: 1.24
Go version: go1.6.4
Git commit: 7392c3b
Built: Fri Dec 16 06:14:34 2016
OS/Arch: linux/amd64
2. 安裝golang
同樣的在Windows和Mac下面都有一鍵安裝包,針對其他系統(tǒng)也是通過命令行進行安裝.參考鏈接,安裝好以后還需要進行環(huán)境變量的配置.
在mac系統(tǒng)和Linux系統(tǒng)下面,Windows配置環(huán)境變量
export GOROOT="/usr/local/go"
export GOPATH="你的工作目錄" //這里主要是影響到后面需要安裝的xgo
再執(zhí)行g(shù)o env查看是否配置成功:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Volumes/Work/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/w4/75xgx41n2bb7mm3m0dxbp0d40000gn/T/go-build003828011=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
3. 安裝xgo docker鏡像
xgo的docker鏡像是我們真正的編譯環(huán)境,Github地址
-
安裝xgo docker鏡像,這個鏡像大概4.xG,需要花較長的時間下載
docker pull karalabe/xgo-latest
這里latest指的最新的編譯環(huán)境,如果需要的話也可以指定安裝具體版本的鏡像,在這里,這個版本的差異主要是golang版本的不同,由于編譯的時候可以指定golang版本進行編譯,所以我們直接安裝最新版本即可.
-
安裝xgo
go get github.com/karalabe/xgo
安裝xgo編譯工具,這個工具封裝了go build,簡化了我們的操作.
4. 配置環(huán)境變量
如果我們使用的是bash的話可以將環(huán)境變量配置到.bash_profile中,免去export的過程
使用bash
-
在 ~ 目錄下編輯.bash_profile 文件,在末尾添加
export GOPATH="你的工作目錄"
export GOROOT="usr/local/go"
export PATH=$PATH:$GOPATH/bin
export GOROOT_BOOTSTRAP=$GOROOT source .bash_profile使得配置文件生效
使用如zsh shell程序
- 在~ 目錄下編輯.zshrc 文件,添加上面同樣的文件
- 同上source生效
5. 測試xgo交叉編譯環(huán)境是否OK
$ cd $GOPATH/src // If you have multiple (I have at least 3 in there), change into any, doesn't matter
$ mkdir xgo_test
$ cd xgo_test
$ echo -e 'package main\n import "fmt"\n func main(){\nfmt.Println("Hello")\n}' > main.go
$ xgo .
執(zhí)行完上述命令后在當(dāng)前目錄查看是否有編譯生成的可執(zhí)行文件
6. 打包
~ xgo /Volumes/Public\ Storage/go/src/zhjd()
~ xgo --targets=android-14/arm /Volumes/Storage/go/src/zhjd 指定生成Android端專用的會快很多很多,這里的14指的是最小支持SDK版本
Checking docker installation...
Client:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built: Wed Oct 26 23:26:11 2016
OS/Arch: darwin/amd64
Server:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built: Wed Oct 26 23:26:11 2016
OS/Arch: linux/amd64
Checking for required docker image karalabe/xgo-latest... found.
Cross compiling zhjd...
Building locally zhjd...
Assembling toolchain for android-16/arm...
Bootstrapping android-16/arm...
Compiling for android-16/arm...
Assembling toolchain for android-16/386...
Bootstrapping android-16/386...
Compiling for android-16/386...
No API C header specified, skipping android-16/aar... 重點關(guān)注
Compiling for linux/amd64...
Compiling for linux/386...
Bootstrapping linux/arm-5...
Compiling for linux/arm-5...
Cleaning up Go runtime for linux/arm-5...
Bootstrapping linux/arm-6...
Compiling for linux/arm-6...
Cleaning up Go runtime for linux/arm-6...
Bootstrapping linux/arm-7...
Compiling for linux/arm-7...
Cleaning up Go runtime for linux/arm-7...
Compiling for linux/arm64...
Compiling for linux/mips64...
Compiling for linux/mips64le...
Compiling for windows-4.0/amd64...
Compiling for windows-4.0/386...
Compiling for darwin-10.6/amd64...
Compiling for darwin-10.6/386...
Bootstrapping ios-5.0/arm-7...
# runtime/cgo
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# net
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# os/user
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
Compiling for ios-5.0/arm-7...
# github.com/mattn/go-sqlite3
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# github.com/mattn/go-sqlite3
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
Cleaning up Go runtime for ios-5.0/arm-7...
Bootstrapping ios-5.0/arm64...
# runtime/cgo
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# net
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# os/user
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
Compiling for ios-5.0/arm64...
# github.com/mattn/go-sqlite3
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
# github.com/mattn/go-sqlite3
ldid.cpp(602): _assert(): Swap(mach_header_->filetype) == MH_EXECUTE || Swap(mach_header_->filetype) == MH_DYLIB || Swap(mach_header_->filetype) == MH_BUNDLE
Cleaning up Go runtime for ios-5.0/arm64...
Cleaning up build environment...
查看編譯生成的文件
? all ls -al
total 427872
drwxr-xr-x 21 silence staff 714 Dec 29 11:37 .
drwxr-xr-x+ 29 silence staff 986 Dec 29 11:37 ..
-rw-r--r--@ 1 silence staff 6148 Dec 29 11:37 .DS_Store
-rwxr-xr-x 1 silence staff 14633608 Dec 29 11:27 zhjd-android-16-386
drwxr-xr-x 5 silence staff 170 Dec 29 11:27 zhjd-android-16-aar
-rwxr-xr-x 1 silence staff 14705872 Dec 29 11:26 zhjd-android-16-arm
-rwxr-xr-x 1 silence staff 10418780 Dec 29 11:32 zhjd-darwin-10.6-386
-rwxr-xr-x 1 silence staff 11578172 Dec 29 11:31 zhjd-darwin-10.6-amd64
-rwxr-xr-x 1 silence staff 11224720 Dec 29 11:34 zhjd-ios-5.0-arm64
-rwxr-xr-x 1 silence staff 10569184 Dec 29 11:33 zhjd-ios-5.0-armv7
drwxrwxrwx 3 silence staff 102 Dec 29 11:35 zhjd-ios-5.0-framework
-rwxr-xr-x 1 silence staff 14611432 Dec 29 11:28 zhjd-linux-386
-rwxr-xr-x 1 silence staff 17246480 Dec 29 11:27 zhjd-linux-amd64
-rwxr-xr-x 1 silence staff 13034956 Dec 29 11:28 zhjd-linux-arm-5
-rwxr-xr-x 1 silence staff 12965808 Dec 29 11:29 zhjd-linux-arm-6
-rwxr-xr-x 1 silence staff 12939788 Dec 29 11:29 zhjd-linux-arm-7
-rwxr-xr-x 1 silence staff 17075720 Dec 29 11:29 zhjd-linux-arm64
-rwxr-xr-x 1 silence staff 18700376 Dec 29 11:30 zhjd-linux-mips64
-rwxr-xr-x 1 silence staff 18699640 Dec 29 11:31 zhjd-linux-mips64le
-rwxr-xr-x 1 silence staff 9734407 Dec 29 11:31 zhjd-windows-4.0-386.exe
-rwxr-xr-x 1 silence staff 10889885 Dec 29 11:31 zhjd-windows-4.0-amd64.exe
可以發(fā)現(xiàn),針對不同平臺生成了不同類型的文件.Android支持生成aar庫文件,iOS支持生成Framework庫文件
平臺實測
Android端測試
-
推送到設(shè)備中,之所以不直接推送到app目錄下是因為權(quán)限問題,這里如果提示adb命令不存在同樣的也要把Android SDK中的adb命令配置到環(huán)境變量中
//直接推送到app目錄下會有權(quán)限問題 adb push zhjd-android-16-arm data/data/com,kingdee.zhihuiji adb: error: failed to copy 'zhjd-android-16-arm' to 'data/data/ com.kingdee.zhihuiji': Permission denied //所以推送到該目錄下 adb push zhjd-android-16-arm data/local/tmp [100%] data/local/tmp/zhjd-android-16-arm
切換到Android設(shè)備并執(zhí)行該文件,這里只能使用已經(jīng)root的設(shè)備
adb shell 進入手機命令行
su 切換超級用戶
cd /data/local/tmp
./zhjd-android-16-arm
最后在終端會輸出可執(zhí)行文件的執(zhí)行結(jié)果