原文[https://blog.weizhe.net/?p=534](https://blog.weizhe.net/?p=534)
有兴趣的同学欢迎来一起玩:[https://github.com/vzever/gomobileapp](https://github.com/vzever/gomobileapp)
go1.5beta2已经发布,正式版将在8月份发布.而早在go1.4就已发布了golang/mobile项目用以实现Android应用的开发.当前gomobile的环境部署已经相对成熟了.但项目声明必须基于go1.5,所以go1.4的同学必须要升级.因为在go1.4执行gomobile init的时候是不能通过的.
注:如果没有网络权限访问golang.org,请创建$GOPATH/src/github.com/golang的软链接为$GOPATH/src/golang.org/x,并且后续go get的时候把golang.org/x/* 替换为 github.com/golang/*
### 第一步. 部署go1.5beta2
[官方网站](http://golang.org/doc/install)下载go1.5beta2的linux安装包,并按照官网部署到/usr/local. 配置/usr/local/go/bin 到$PATH中,如下写入/etc/profile 或$HOME/.profile
export PATH=$PATH:/usr/local/go/bin
然后还需要将其他golang环境变量加入$HOME/.profile
exprot GOPATH=$HOME/gocode/
export PATH=$PATH:$GOPATH/bin
### 第二步 部署golang/mobile
参照[golang/mobile文档](https://godoc.org/golang.org/x/mobile/cmd/gomobile)执行以下命令
$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init -v
如果提示没有权限如下
#Building standard library for android/arm.
runtime
go install runtime: mkdir /usr/local/go/pkg/android_arm/: permission denied
gomobile: go install std for android/arm failed: exit status 1
需要授权/usr/local/go/pkg给当前用户
# chown -R myname:myname /usr/local/go/pkg
然后再次执行
$ gomobile init -v
Downloading https://dl.google.com/go/mobile/gomobile-ndk-r10e-linux-x86_64.tar.gz.
Downloading https://dl.google.com/go/mobile/gomobile-openal-soft-1.16.0.1.tar.gz.
Building standard library for android/arm.
runtime
errors
sync/atomic
...
testing/quick
net/rpc/jsonrpc
Done, build took 23s.
### 第三步 使用go run 在桌面测试程序
$ cd $GOPATH/src/golang.org/x/mobile/example/basic
$ go run main.go
### 第四步 使用gomobile直接生成apk格式Android应用.
$ cd $GOPATH/src/golang.org/x/mobile/example/basic
$ gomobile build
$ ls
main.go basic.apk
或者连接android手机,确保adb shell可以登录手机之后,直接编辑并安装APK
$ cd $GOPATH/src/golang.org/x/mobile/example/basic
$ gomobile install
### 最后安装到Android L
除network.apk无法启动外,basic audio和sprite都正常.甚至github.com/mmchugh/gomobile-examples的cube也运行正常.以下为截图
手机截图 basic/audio/sprite/cube
![](https://blog.weizhe.net/wp-content/uploads/2015/07/Screenshot_2015-07-16-00-55-20-169x300.png)![](https://blog.weizhe.net/wp-content/uploads/2015/07/Screenshot_2015-07-16-01-17-18-169x300.png)![](https://blog.weizhe.net/wp-content/uploads/2015/07/Screenshot_2015-07-16-01-17-29-169x300.png)![](https://blog.weizhe.net/wp-content/uploads/2015/07/Screenshot_2015-07-18-00-43-35-169x300.png)
PC截图
![](https://blog.weizhe.net/wp-content/uploads/2015/07/Screenshot-from-2015-07-17-18-53-45-275x300.png)![](https://blog.weizhe.net/wp-content/uploads/2015/07/Screenshot-from-2015-07-17-23-33-09-275x300.png)
请教, go get golang.org/x/mobile/cmd/gomobile 怎么直接报错:
package golang.org/x/mobile/cmd/gomobile: unrecognized import path "golang.org/x/mobile/cmd/gomobile"
#15