ddmobile

command module
v0.0.0-...-a7246ef Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: MIT Imports: 41 Imported by: 0

README

ddmobile

gomobile 的魔改版本。主要是不满意 gomobile 默认情况下直接构建 module 包这个功能,针对 Android 和 iOS 平台修改为构建产出 .so 和 .a 库文件。(Android 平台还支持直接生成可执行文件)

安装
$ cd <your-work-space>
$ git clone https://github.com/ClarkGuan/ddmobile
$ cd ddmobile
$ go get
$ go install
帮助
$ ddmobile help
Ddmobile is a tool for building and running mobile apps written in Go.

To install:

	$ go get github.com/ClarkGuan/ddmobile
	$ ddmobile init

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

Usage:

	ddmobile command [arguments]

Commands:

	bind        build a library for Android and iOS
	build       compile android APK and iOS app
	build2      compile android shared library and iOS static library
	clean       remove object files and cached ddmobile files
	init        build OpenAL for Android
	install     compile android APK and install on device
	version     print version

Use 'ddmobile help [command]' for more information about that command.
使用
1、初始化

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

  • ANDROID_HOME

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

  • NDK 或 ANDROID_NDK_HOME

因为 Android SDK 新版将 NDK 内置路径(ndk-bundle 目录)作出修改,为了便于 ddmobile 查找,可以定义 ANDROID_NDK_HOME 环境变量指向具体位置。

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

2、Android 构建

我们假设有一个 go 工程在 <your-work-space> 目录下,

$ cd <your-work-space>
$ ddmobile build2 -target android/arm,android/arm64

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

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

$ cd <your-work-space>
$ ddmobile build2 -exe

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

3、iOS 构建
$ cd <your-work-space>
$ ddmobile build2 -target ios

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

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

$ cd <your-work-space>
$ ddmobile build2 -target ios/arm,ios/arm64,ios/386,ios/amd64

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

举例

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

$ mkdir -p <your-work-space>
$ cd <your-work-space>
$ touch main.go

main.go 内容如下:

package main

import (
	"fmt"
)

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

此时运行命令

$ go mod init hello
$ ddmobile build2 -target android/arm -exe

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

$ GO111MODULE=off go get -u github.com/ClarkGuan/arun
$ arun -exe build/android/app/armeabi-v7a/hello

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

prepare to push /Users/xxx/<your-work-space>/build/android/app/armeabi-v7a/hello to device
/Users/xxx/<your-work-space>/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 的位置
  • 2020-04-15
    • 创建分支 v1.1。该分支为 2019 年代码,支持 NDKr17~NDKr20 或更高的版本(最高支持版本未知)
    • 合并最新 gomobile 代码;将生成命令修改为 build2 以兼容 gomobile 已有功能
    • 因为最大限度兼容 gomobile,所以恢复了生成 bitcode 的功能
  • 2021-05-21
    • 适配 Go 1.15+ 版本,移除之后对 iOS arm 和 386 平台的支持

Documentation

Overview

Ddmobile is a tool for building and running mobile apps written in Go.

To install:

$ go get github.com/ClarkGuan/ddmobile
$ ddmobile init

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

Usage:

ddmobile command [arguments]

Commands:

bind        build a library for Android and iOS
build       compile android APK and iOS app
build2      compile android shared library and iOS static library
clean       remove object files and cached ddmobile files
init        build OpenAL for Android
install     compile android APK and install on device
version     print version

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

Build a library for Android and iOS

Usage:

ddmobile 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, ddmobile 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, -trimpath, and -work are shared with the build command. For documentation, see 'go help build'.

Compile android APK and iOS app

Usage:

ddmobile 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, ddmobile 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 7.0.

Flag -androidapi sets the Android API version to compile against. The default and minimum is 15.

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, -trimpath, and -work are shared with the build command. For documentation, see 'go help build'.

Compile android shared library and iOS static library

Usage:

ddmobile build2 [-target android|ios] [-o output] [build flags] [package]

Build2 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, 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, ddmobile must be run on an OS X machine with Xcode installed.

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

Flag -androidapi sets the Android API version to compile against. The default and minimum is 15.

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, -trimpath, and -work are shared with the build command. For documentation, see 'go help build'.

Remove object files and cached ddmobile files

Usage:

ddmobile clean

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

Build OpenAL for Android

Usage:

ddmobile init [-openal dir]

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

Compile android APK and install on device

Usage:

ddmobile 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, -trimpath, and -work are shared with the build command. For documentation, see 'go help build'.

Print version

Usage:

ddmobile version

Version prints versions of the ddmobile 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