November, 2020 – Graham Miln

Notes for gomobile on macOS

I have been exploring Android development and attempting to use a go code base. This appears possible with bindings created by gomobile. Getting gomobile to work without installing onerous system wide packages on my computer has been time consuming.

The Go mobile subrepository adds support for mobile platforms (Android and iOS) and provides tools to build mobile applications.

Thankfully it is possible to use gomobile on macOS with only Android Studio. This means that clean up is limited to removing:

Set-Up Android Studio

Within Android Studio, install the optional NDK:

  1. Open Android Studio.app > Tools (menu) > SDK Manager
  2. Select SDK Tools (tab)
  3. Enable NDK (Side by side)
  4. Select Apply

With this done, the Native Development Kit (NDK) will be installed within your Android folder.

You will also need the Android SDK Platform-Tools installed but this was enabled by default.

gomobile

Via the environment, pass paths for JAVA_HOME, ANDROID_NDK_HOME, and ANDROID_HOME when calling gomobile:

JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home ANDROID_NDK_HOME=~/Library/Android/sdk/ndk/21.3.6528147 ANDROID_HOME=~/Library/Android/sdk $GOPATH/bin/gomobile bind -target android -o example.aar example.miln.eu

This was enough for the basic go code to compile through to an importable .aar file.

Using an aar file in Android Studio

To use the generated Android Archive (aar) file in Android Studio, I had to do two things.

Set up involved creating a new module:

  1. Open Android Studio.app > File (menu) > New > New Module
  2. Select Import .JAR/.AAR Package

This created boilerplate files around the archive. Then I could import the module for use in an existing project:

  1. Open Android Studio.app > File (menu) > New > Import Module
  2. Select Import .JAR/.AAR Package

In settings.gradle add:

include ':example'