Gomobile 编译jni库 实用 Android和iOS 两端

0x01–what

gomobile是golang 开发的移动端工具。

其中一些功能如下 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
╭─apanda atxx的MacBook Pro in ~/MEGAsync/Projects/GoProjects/src/github.com/golang/mobile/cmd/gomobile on master✔ using ‹› 18-04-04 - 11:13:46
╰─○ tree
.
├── binary_xml.go
├── binary_xml_test.go
├── bind.go
├── bind_androidapp.go
├── bind_iosapp.go
├── bind_test.go
├── build.go
├── build_androidapp.go
├── build_darwin_test.go
├── build_iosapp.go
├── build_test.go
├── cert.go
├── cert_test.go
├── clean.go
├── dex.go
├── doc.go
├── env.go
├── gendex.go
├── init.go
├── init_test.go
├── install.go
├── main.go
├── manifest.go
├── strings_flag.go
├── version.go
├── writer.go
└── writer_test.go

0x02–why

为啥使用golang写jni ,不直接使用as工具中的cmake实现。

优点

  • 1、 gomobile 全自动化,一旦生成iOS和Android直接 调用,不需要再使用Javah 等操作。只需要引入包即可。
  • 2、 golang语言可读性强,上手快,操作简洁。
  • 3、可装x

缺点

  • 相关的bean值操作无法进行传递。
  • Map,List等无法进行传递。

0x03–how

1、准备

1、1 配置golang,以及下载gomobile (自行爬文)

命令行输入 go ,以及 gomobile 可以提示参数,表示ok了 。

Go 命令提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
╭─apanda at 熊凯的MacBook Pro in ~ using ‹› 18-04-04 - 11:07:02
╰─○ go
Go is a tool for managing Go source code.

Usage:

go command [arguments]

The commands are:

build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
bug start a bug report
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages

Use "go help [command]" for more information about a command.
...

GoMobile命令提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
╭─apanda at xx的MacBook Pro in ~ using ‹› 18-04-04 - 11:08:23
╰─○ gomobile
Gomobile is a tool for building and running mobile apps written in Go.

To install:

$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init

At least Go 1.7 is required.
For detailed instructions, see https://golang.org/wiki/Mobile.

Usage:

gomobile command [arguments]

Commands:

bind build a library for Android and iOS
build compile android APK and iOS app
clean remove object files and cached gomobile files
init install mobile compiler toolchain
install compile android APK and install on device
version print version

Use 'gomobile help [command]' for more information about that command.

1、2 配置Android 开发环境

sdk路径到环境变量,提前下载好ndk,存放在指定的目录(SDK下)。

命令行输入adb提示参数, 即表明配置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
╭─apanda at xx的MacBook Pro in ~ using ‹› 18-04-04 - 10:05:40
╰─○ adb
Android Debug Bridge version 1.0.39
Version 0.0.1-4500957
Installed as /usr/local/bin/adb

global options:
-a listen on all network interfaces, not just localhost
-d use USB device (error if multiple devices connected)
-e use TCP/IP device (error if multiple TCP/IP devices available)
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
-t ID use device with given transport id
-H name of adb server host [default=localhost]
-P port of adb server [default=5037]
-L SOCKET listen on given socket for adb server [default=tcp:localhost:5037]
...

Done ! 下面开始操作 😊

2、DO

  • 在gopath 目录下面 创建 netCore文件夹,

mkdir netCore &&cd netCore

  • 创建netCore.go 文件

vi netCore.go

编辑如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package netCore;

import (
"fmt"
"net/http"
"io/ioutil"
"strings"
)

func HttpGet(url string) string {

resp, err := http.Get(url)
if err != nil {
// handle error
return "访问网络错误!!" + err.Error()
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
return "访问网络错误!!" + err.Error()
}

fmt.Println(string(body))
return string(body)
}

  • bash 进入到netCore,初始化gomobile (系统需要配置好JAVA_HOME,ANDROID_HOME )