Mac 下用 go 开发Android应用环境设置

需要的工具

设置代理

请参考:http://www.cnblogs.com/ghj1976/p/5087049.html 

Mac 下命令行设置代理:

export http_proxy=http://127.0.0.1:8787

git config --global http.proxy http://127.0.0.1:8787
git config --global https.proxy https://127.0.0.1:8787

下载 gomoblie 工具

go get -u  golang.org/x/mobile/cmd/gomobile

gomobile init

image

如果出现下面错误:

$ gomobile init

gomobile: xcrun --show-sdk-path: exit status 1

xcrun: error: SDK "iphoneos" cannot be located

xcrun: error: SDK "iphoneos" cannot be located

xcrun: error: unable to lookup item 'Path' in SDK 'iphoneos'

 

则需要确保你安装了 XCode

安装 Android SDK,并确保adb在PATH中。

我直接下载的的 Android Studio, 一步到位, 下载地址:

https://developer.android.com/sdk/installing/index.html?pkg=studio

Android Studio 的 Configure –》 SDK Manager 中可以看到 SDK的安装目录。

image

image

image

adb 命令 就在 SDK 安装目录下的 platform-tools 目录下,即下面目录。

/software/adt-bundle-mac-x86_64-20140321/sdk/platform-tools

把这个目录增加到 PATH 设置中。

http://stackoverflow.com/questions/17901692/set-up-adb-on-mac-os-x

adb 调试

首先我们确保 android studio 可以真机调试

并且你的设备要能够使用 adb 调试。

我的手机是华为荣耀6

手机上打开USB调试的方法请参考下面方法:

http://jingyan.baidu.com/article/f25ef25466fbfc482d1b8272.html

需要打开两个设置,如下图:

开启USB调试

image

开启 MTP,不选这个没法USB调试的。

image

这两个设置后,我们在 android studio 上就可以看到可以真机调试了。

image

adb 真机调试

adb devices 命令可以看到我们刚刚配好的真机。

image

编号跟上面编号一致。

执行

gomobile install golang.org/x/mobile/example/basic
我们会看见桌面上安装好了这个 basic 应用。

image

我们在手机上运行这个应用就可以看到效果。

image

使用go run 在桌面测试程序

这个例子在桌面是可以跑的。只需如下运行:

$ cd $GOPATH/src/golang.org/x/mobile/example/basic
$ go run main.go
image
go build 也可以生产该操作系统下的执行文件。

使用gomobile直接生成apk格式Android应用.

$ cd $GOPATH/src/golang.org/x/mobile/example/basic
$ gomobile build
$ ls
main.go basic.apk

其他例子

go 提供的几个例子运行截图如下:

gomobile install golang.org/x/mobile/example/audio

image

gomobile install golang.org/x/mobile/example/flappy

image

参考:

http://studygolang.com/topics/967

https://github.com/golang/go/wiki/Mobile

http://www.jianshu.com/p/403aa507935b

https://blog.weizhe.net/?p=534

一些例子:

https://github.com/lomoalbert/gomobileapp

原文地址:https://www.cnblogs.com/ghj1976/p/5174026.html