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:
- /Applications/Android Studio.app
- ~/Library/Android
Set-Up Android Studio
Within Android Studio, install the optional NDK:
- Open Android Studio.app > Tools (menu) > SDK Manager
- Select SDK Tools (tab)
- Enable NDK (Side by side)
- 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:
- Open Android Studio.app > File (menu) > New > New Module
- Select Import .JAR/.AAR Package
This created boilerplate files around the archive. Then I could import the module for use in an existing project:
- Open Android Studio.app > File (menu) > New > Import Module
- Select Import .JAR/.AAR Package
In settings.gradle add:
include ':example'