用 go 语言开发 android app

oldfeel · · 20849 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

一. 下载官方源码 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



有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:oldfeel

查看原文:用 go 语言开发 android app

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

20849 次点击  
加入收藏 微博
5 回复  |  直到 2000-01-01 00:00:00
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传