Go Mobile

repository·master·Indexed 27 days ago

https://github.com/golang/mobile

A collection of packages and build tools for using Go on mobile platforms (Android and iOS). It supports building all-Go applications and Go libraries integrated into native SDK apps via the gomobile and gobind tools. Key features include the App interface for event handling and rendering, and the ability to generate Java and Objective-C language bindings for Go functions and structs.

Tokens
9.1K
Snippets
18
Records
69
Agent score
91%

What's inside golang/mobile

  1. Overview of Go Mobile

    master

    The golang/mobile repository provides packages and build tools designed to enable Go code to run on mobile platforms (Android and iOS). It supports two primary workflows:

    1. All-Go apps: Building applications written entirely in Go.
    2. Libraries for SDK apps: Building Go libraries that can be called from existing native Android or iOS applications using gobind.

    Warning: The Go Mobile project is experimental. Use it at your own risk; neither Google nor the Go team provides end-user support.

  2. Configure Android SDK and NDK environment variables

    master

    The gomobile command requires access to the Android SDK and NDK. It attempts to find them automatically, but if it fails, you must manually set the ANDROID_HOME and ANDROID_NDK_HOME environment variables to point to their respective directories.

    export ANDROID_HOME=/path/to/sdk-directory
    export ANDROID_NDK_HOME=/path/to/ndk-directory
  3. Update Ivy and golang.org/x/mobile dependencies

    master

    The project depends on robpike.io/ivy and golang.org/x/mobile. To update these dependencies to their latest versions, use go get followed by go mod tidy to clean up the module file.

    go get -d golang.org/x/mobile@latest
    go get -d robpike.io/ivy/mobile
    go mod tidy
  4. Build the Mobile.xcframework for the Ivy iOS app

    master

    Before building the Ivy iOS app, you must generate the Mobile.xcframework from the Go implementation of Ivy. This requires installing gomobile and gobind, initializing a workspace, and running the gomobile bind command with specific targets.

    # 1. Install tools
    go install golang.org/x/mobile/cmd/gomobile@latest
    go install golang.org/x/mobile/cmd/gobind@latest
    
    # 2. Create workspace and fetch dependencies
    mkdir work; cd work
    go mod init work
    go get -d golang.org/x/mobile/bind@latest
    go get -d robpike.io/ivy/mobile
    
    # 3. Bind the Go packages to iOS targets
    gomobile bind -target=ios,iossimulator,maccatalyst,macos robpike.io/ivy/mobile robpike.io/ivy/demo
  5. Configure code signing for the Ivy iOS project

    master

    After placing the generated Mobile.xcframework directory into the Ivy iOS source directory, open ivy.xcodeproj in Xcode. You must configure a Development Team to enable code signing:

    Navigate to: Project Settings -> Targets -> Signing & Capabilities -> Signing -> Team.

  6. Build the Ivy Android project

    master

    To build the Ivy Big Number Calculator Android project, you must first ensure you have the necessary Go and Android development tools installed. The build process involves using gomobile bind to create an .aar file from the robpike.io/ivy/mobile package, which can then be used in Android Studio.

    # Install required tools
    go install golang.org/x/mobile/cmd/gomobile@latest
    go install golang.org/x/mobile/cmd/gobind@latest
    
    # Build the Android Archive (.aar)
    gomobile bind -o app/ivy.aar robpike.io/ivy/mobile
  7. Configure the Android NDK path

    master

    The gomobile tool requires an Android NDK to build for Android. It searches for the NDK in the following order of priority:

    1. The ANDROID_NDK_HOME environment variable (highest priority).
    2. The side-by-side NDK directory located under your Android SDK path ($ANDROID_HOME/ndk).
    3. The legacy ndk-bundle directory under your Android SDK path.

    If you need to explicitly point to a specific NDK version, set the ANDROID_NDK_HOME environment variable.

  8. Bind a Go package for iOS using gomobile bind

    master

    To create an XCFramework for iOS from a Go package, use the gomobile bind command with the -target=ios flag. This generates the necessary framework files required for integration into an Xcode project.

    Run the following command in your terminal, replacing the package path with your target package:

    gomobile bind -target=ios golang.org/x/mobile/example/bind/hello
  9. Generate a DEX file with gendex

    master

    The gendex tool generates a .dex file used by Go apps created with gomobile. This DEX file acts as a thin extension of NativeActivity, providing access to certain Android platform features that are not easily accessible via standard NDK headers.

    Prerequisites

    • Android SDK: The tool respects the ANDROID_HOME environment variable to locate the Android SDK.
    • Java Compiler: javac must be available in your system PATH.
    • Android Build Tools: The tool requires the dx tool from your Android SDK build-tools directory.
  10. Initialize the gomobile toolchain

    master

    Before using gomobile to build mobile applications, you must initialize the toolchain. If the toolchain is not installed or the path is not found in your $GOPATH/pkg/gomobile, you will encounter an error. Run the following command to set up the environment:

    gomobile init