gomobile command - golang.org/x/mobile/cmd/gomobile - Go Packages (2024)

Overview

  • Build a library for Android and iOS
  • Compile android APK and iOS app
  • Remove object files and cached gomobile files
  • Build OpenAL for Android
  • Compile android APK and install on device
  • Print version

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

To install:

$ go install golang.org/x/mobile/cmd/gomobile@latest$ gomobile init

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

Usage:

gomobile command [arguments]

Commands:

bind build a library for Android and iOSbuild compile android APK and iOS appclean remove object files and cached gomobile filesinit build OpenAL for Androidinstall compile android APK and install on deviceversion 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|iossimulator|macos|maccatalyst] [-bootclasspath <path>] [-classpath <path>] [-o output] [build flags] [package]

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

The -target flag takes either android (the default), or one or morecomma-delimited Apple platforms (ios, iossimulator, macos, maccatalyst).

For -target android, the bind command produces an AAR (Android ARchive)file that archives the precompiled Java API stub classes, the compiledshared libraries, and all asset files in the /assets subdirectory underthe package directory. The output is named '<package_name>.aar' bydefault. This AAR file is commonly used for binary distribution of anAndroid library project and most Android IDEs support AAR import. Forexample, in Android Studio (1.2+), an AAR file can be imported usingthe 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 16 or newer) to build thelibrary for Android. The environment variable ANDROID_HOME must be setto the path to Android SDK. Use the -javapkg flag to specify the Javapackage prefix for the generated classes.

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

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

For -target android, the -bootclasspath and -classpath flags are used tocontrol the bootstrap classpath and the classpath for Go wrappers to Javaclasses.

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

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

Compile android APK and iOS app

Usage:

gomobile build [-target android|ios|iossimulator|macos|maccatalyst] [-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 either android (the default), or one or morecomma-delimited Apple platforms (ios, iossimulator, macos, maccatalyst).

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

For Apple -target platforms, gomobile must be run on an OS X machine withXcode installed.

By default, -target ios will generate an XCFramework for both iosand iossimulator. Multiple Apple targets can be specified, creating a "fat"XCFramework with each slice. To generate a fat XCFramework that supportsiOS, macOS, and macCatalyst for all supportec architectures (amd64 and arm64),specify -target ios,macos,maccatalyst. A subset of instruction sets can beselectged by specifying the platform with an architecture name. E.g.-target=ios/arm64,maccatalyst/arm64.

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

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

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

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

The -o flag specifies the output file name. If not specified, theoutput 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 areshared 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.

Build OpenAL for Android

Usage:

gomobile init [-openal dir]

If a OpenAL source directory is specified with -openal, init willbuild an Android version of OpenAL for use with gomobile buildand 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 theattached 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 areshared with the build command.For documentation, see 'go help build'.

Print version

Usage:

gomobile version

Version prints versions of the gomobile binary and tools

gomobile command - golang.org/x/mobile/cmd/gomobile - Go Packages (2024)
Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5709

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.