ddmobile

command module
v0.0.0-...-0b23cb7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2019 License: MIT Imports: 38 Imported by: 0

README

ddmobile

gomobile 的魔改版本。主要是不满意 gomobile 默认情况下直接构建 module 包的功能,针对 Android 和 iOS 平台修改为构建产出 .so 和 .a 库文件。

安装
go get github.com/ClarkGuan/ddmobile
使用
1、初始化

我们假设有一个 go 工程在 $GOPATH/src/hello_world 目录下,

Android 环境初始化依赖 NDK 工具,默认情况下会搜索如下环境变量:

  • ANDROID_HOME

实际上是指定 Android SDK 的位置,如果 SDK 中装有 NDK(ndk-bundle 目录)则使用该版本 NDK。

iOS 环境初始化依赖 Xcode 以及相关命令行工具(xcrun 等)。

2、Android 构建
cd $GOPATH/src/hello_world
ddmobile build -target android/arm,android/arm64

如果构建顺利,会在 $GOPATH/src/hello_world 目录中生成 build/android 子目录,并列出 arm 32 位和 64 位的动态库。

如果我们编译的是可执行文件,则运行

cd $GOPATH/src/hello_world
ddmobile build -exe

这时生成的子目录为 build/android/。另:我们并没有特殊指明 -target android/arm,android/arm64,默认会产出 Android 所有支持的平台产物。

3、iOS 构建
cd $GOPATH/src/hello_world
ddmobile build -target ios

和 Android 构建类似,因为这里并没有指定使用何种架构编译,所以会生成所有支持的 iOS 架构产物,构建目录是 build/ios 子目录。

当然,我们也可以指定目标架构:

cd $GOPATH/src/hello_world
ddmobile build -target ios/arm,ios/arm64,ios/386,ios/amd64

这个命令和上一个命令是等价的。

举例

我们以可以在 Android 上运行的 HelloWorld 工程为例,创建目录

mkdir -p $GOPATH/src/hello
cd $GOPATH/src/hello
touch main.go

main.go 内容如下:

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello world")
}

此时运行命令

ddmobile build -target android/arm -exe

使用我编写的另一个工具 arun( https://github.com/ClarkGuan/arun ):

arun -exe build/android/armeabi-v7a/hello

输出类似下面(我们假定您已经将 adb 命令加入到 $PATH 中)

prepare to push /Users/xxx/gopath/src/hello/build/android/app/armeabi-v7a/hello to device
/Users/xxx/gopath/src/hello/build/android/app/armeabi-v7a/hello: 1 file pushed. 9.9 MB/s (1963673 bytes in 0.189s)
[程序输出如下]
Hello world
[程序执行完毕]
修改日志
  • 2019-09-05 去掉 iOS 生成的目标文件中的 bitcode 段(一是增大了包体积;二是仍然无法满足 Apple 100% bitcode 覆盖的要求)
  • 2019-10-10 创建分支 v1.0。该分支为 2018 年代码,支持 NDKr17 以及更早的版本
  • 2019-10-11
    • 添加选项 -p,可以指定 Android 和 iOS 输出库文件的名称。例如 -p hello,对应 Android 动态库 libhello.so;对应 iOS 静态库 libhello.a
    • 【Android SDK 内 NDK 目录名称又变化了】添加对 $NDK 环境变量的识别,优先使用该环境变了定位 NDK 的位置

Documentation

Overview

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.10 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 NDK toolchains and build OpenAL for Android
install     compile android APK and install on device
version     print version

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

Build a library for Android and iOS

Usage:

gomobile bind [-target android|ios] [-bootclasspath <path>] [-classpath <path>] [-o output] [build flags] [package]

Bind generates language bindings for the package named by the import path, and compiles a library for the named target system.

The -target flag takes a target system name, either android (the default) or ios.

For -target android, the bind command produces an AAR (Android ARchive) file that archives the precompiled Java API stub classes, the compiled shared libraries, and all asset files in the /assets subdirectory under the package directory. The output is named '<package_name>.aar' by default. This AAR file is commonly used for binary distribution of an Android library project and most Android IDEs support AAR import. For example, in Android Studio (1.2+), an AAR file can be imported using the module import wizard (File > New > New Module > Import .JAR or .AAR package), and setting it as a new dependency (File > Project Structure > Dependencies). This requires 'javac' (version 1.7+) and Android SDK (API level 15 or newer) to build the library for Android. The environment variable ANDROID_HOME must be set to the path to Android SDK. Use the -javapkg flag to specify the Java package prefix for the generated classes.

By default, -target=android builds shared libraries for all supported instruction sets (arm, arm64, 386, amd64). A subset of instruction sets can be selected by specifying target type with the architecture name. E.g., -target=android/arm,android/386.

For -target ios, gomobile must be run on an OS X machine with Xcode installed. The generated Objective-C types can be prefixed with the -prefix flag.

For -target android, the -bootclasspath and -classpath flags are used to control the bootstrap classpath and the classpath for Go wrappers to Java classes.

The -v flag provides verbose output, including the list of packages built.

The build flags -a, -n, -x, -gcflags, -ldflags, -tags, and -work are shared with the build command. For documentation, see 'go help build'.

Compile android APK and iOS app

Usage:

gomobile build [-target android|ios] [-o output] [-bundleid bundleID] [build flags] [package]

Build compiles and encodes the app named by the import path.

The named package must define a main function.

The -target flag takes a target system name, either android (the default) or ios.

For -target android, if an AndroidManifest.xml is defined in the package directory, it is added to the APK output. Otherwise, a default manifest is generated. By default, this builds a fat APK for all supported instruction sets (arm, 386, amd64, arm64). A subset of instruction sets can be selected by specifying target type with the architecture name. E.g. -target=android/arm,android/386.

For -target ios, gomobile must be run on an OS X machine with Xcode installed.

If the package directory contains an assets subdirectory, its contents are copied into the output.

Flag -iosversion sets the minimal version of the iOS SDK to compile against. The default version is 6.1.

The -bundleid flag is required for -target ios and sets the bundle ID to use with the app.

The -o flag specifies the output file name. If not specified, the output file name depends on the package built.

The -v flag provides verbose output, including the list of packages built.

The build flags -a, -i, -n, -x, -gcflags, -ldflags, -tags, and -work are shared with the build command. For documentation, see 'go help build'.

Remove object files and cached gomobile files

Usage:

gomobile clean

Clean removes object files and cached NDK files downloaded by gomobile init

Install NDK toolchains and build OpenAL for Android

Usage:

gomobile init [-ndk dir] [-openal dir]

If the -ndk flag is specified or the Android NDK is installed at $ANDROID_HOME/ndk-bundle, init will create NDK standalone toolchains for Android targets.

If a OpenAL source directory is specified with -openal, init will build an Android version of OpenAL for use with gomobile build and gomobile install.

Compile android APK and install on device

Usage:

gomobile install [-target android] [build flags] [package]

Install compiles and installs the app named by the import path on the attached mobile device.

Only -target android is supported. The 'adb' tool must be on the PATH.

The build flags -a, -i, -n, -x, -gcflags, -ldflags, -tags, and -work are shared with the build command. For documentation, see 'go help build'.

Print version

Usage:

gomobile version

Version prints versions of the gomobile binary and tools

Directories

Path Synopsis
internal
binres
Package binres implements encoding and decoding of android binary resources.
Package binres implements encoding and decoding of android binary resources.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL