用 go 语言开发 android app

一. 下载官方源码 https://github.com/golang/mobile

git clone https://github.com/golang/mobile.git

二. 将mobile目录移动到 $gopath/golang.org/x中

mkdir -p $gopath/golang.org/x && mv mobile $gopath/golang.org/x/

三. 复制官方例子 basic 到 gopath 中

cp -a $gopath/golang.org/x/mobile/example/basic $gopath/

四. 运行编译命令

cd $gopath/src/basic
./make.bash

1. 如果出现错误 

go build runtime: android/arm must be bootstrapped using make.bash

说明你还没有配置 go 的交叉编译环境,就是说你现在的系统无法编译出可以运行在 android/arm cpu 上的二进制文件 *.so

进入你的 ndk 目录并运行 make-standalone-toolchain.sh

cd ~/android-ndk && ./build/tools/make-standalone-toolchain.sh

Copying prebuilt binaries...
Copying sysroot headers and libraries...
Copying c++ runtime headers and libraries...
Creating package file: /tmp/ndk-oldfeel/arm-linux-androideabi-4.9.tar.bz2
Cleaning up...
Done.

如果出现

Unable to auto-config arch from toolchain

这是说无法自动配置 toolchain,需要手动配置.先看看你的ndk支持编译哪些cpu

oldfeel@oldfeel:~/android-ndk$ ls toolchains/
aarch64-linux-android-4.9        mipsel-linux-android-4.8
aarch64-linux-android-clang3.4   mipsel-linux-android-4.9
aarch64-linux-android-clang3.5   mipsel-linux-android-clang3.4
arm-linux-androideabi-4.6        mipsel-linux-android-clang3.5
arm-linux-androideabi-4.8        renderscript
arm-linux-androideabi-4.9        x86-4.6
arm-linux-androideabi-clang3.4   x86-4.8
arm-linux-androideabi-clang3.5   x86-4.9
llvm-3.4                         x86_64-4.9
llvm-3.5                         x86_64-clang3.4
mips64el-linux-android-4.9       x86_64-clang3.5
mips64el-linux-android-clang3.4  x86-clang3.4
mips64el-linux-android-clang3.5  x86-clang3.5
mipsel-linux-android-4.6

编辑 make-standalone-toolchain.sh,找到并修改 TOOLCHAIN_NAME= 为

vim build/tools/make-standalone-toolchain.sh
TOOLCHAIN_NAME=arm-linux-androideabi-4.9

重新执行

./build/tools/make-standalone-toolchain.sh --platform=android-14

Copying prebuilt binaries...
Copying sysroot headers and libraries...
Copying c++ runtime headers and libraries...
Creating package file: /tmp/ndk-oldfeel/arm-linux-androideabi-4.9.tar.bz2
Cleaning up...
Done.

解压 /tmp/ndk-oldfeel/arm-linux-androideabi-4.9.tar.bz2 (你的可能是别的名字,上面的运行结果里有显示)并将里面的所有文件 arm-linux-androideabi-4.9/* 移动到 ndk 目录中

mv arm-linux-androideabi-4.9/* ~/android-ndk/

这时候就可以到 go/src 下配置 go 的交叉编译环境(比如我的 go 源码是放在 ~/go 中的)

注意:修改第二行命令中的 ~/android-ndk 为自己的 ndk 目录

cd ~/go/src
CC_FOR_TARGET=~/android-ndk/bin/arm-linux-androideabi-gcc GOOS=android GOARCH=arm GOARM=7 CGO_ENABLED=1 ./make.bash

最后出现

---
Installed Go for android/arm in /home/oldfeel/go
Installed commands in /home/oldfeel/go/bin

说明配置成功,再次执行 make.bash

cd $gopath/src/basic
./make.bash

如果出现

./make.bash: 行 16: ndk-build: 未找到命令

说明没有把 ndk 目录放入环境变量中,另外还需要配置 ANDROID_HOME=sdk.dir

vim ~/.profile

在最下面添加

export ANDROID_HOME=$HOME/android-sdk
export PATH=$PATH:$HOME/android-ndk

刷新环境变量(该刷新只对当前终端有效,若想对所有终端有效请重启电脑)

source ~/.profile

再次执行 make.bash 应该就成功了吧

cd $gopath/src/basic
./make.bash

参考Demo: https://github.com/oldfeel/libhello

转载于:https://my.oschina.net/oldfeel/blog/372556

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值