The repository is about to run go code on mobile.
- compile Golang package with gomobile
- embed this output into an Android / iOS app mainly with React Native
- first go is with Android because it is simpler
-
Install golang. Recommended to use goenv.
-
Install JDK (required by gomobile as well) - version 17 will do it for not. Recommend to use some tool - I have used SDKMAN for it.
-
Install Android Studio for easy SDK/NDK management. Android 16 - Api 36 installed with NDK 29.0.14206865
-
Here are some of my configured env vars maybe it helps you
GOROOT=/Users/YOUR_USERNAME/.goenv/versions/1.24.2 GOPATH=/Users/YOUR_USERNAME/go/1.24.2 ANDROID_HOME=/Users/YOUR_USERNAME/Library/Android/sdk ANDROID_NDK_HOME=/Users/YOUR_USERNAME/Library/Android/sdk/ndk/29.0.14206865 SDKMAN_DIR=/Users/YOUR_USERNAME/.sdkman SDKMAN_CANDIDATES_API=https://api.sdkman.io/2 SDKMAN_BROKER_API=https://broker.sdkman.io SDKMAN_PLATFORM=darwinarm64 SDKMAN_CANDIDATES_DIR=/Users/YOUR_USERNAME/.sdkman/candidates binary_input=/Users/YOUR_USERNAME/.sdkman/tmp/java-17.0.12-tem.bin
-
You maybe need to run
go install golang.org/x/mobile/cmd/gomobile@latestcheck it by executegomobliecommand in the terminal -
Run
make installin project root -
Run
make pre-build-javain project root - or check the Makefile if you want to do things manually - what this does is:- use gomobile to compile go code
- copy the binary where the GoInJava app expects it to be (/app/libs)
- Add the following to the build.gradle file
dependencies { implementation fileTree(dir: 'libs', include: ['*.aar']) }
-
GoInJava project is in Android Studio
- To build the project run
make build-java-androidor./gradlewin the GoInJava folder - It uses Gradle and every time you update the *.aar file you have to run a build otherwise it does not detect the changes
- To build the project run
-
Start an emulator in Android Studio and load the app there and you can try
- Create new Expo app for react native - select Blank (TypeScript).
npx create-expo-app --templateI named it GoInReact. - Now a Kotlin/Java wrapper required. cd GoInReact and create expo module
npx create-expo-module@latest --local. I used the following namings. ✔ What is the name of the local module? … calculator ✔ What is the native module name? … Calculator ✔ What is the Android package name? … expo.module.gocalculator - That command created a structure under module and the CalculatorModule class. This class should be implemented for each platform and in this layer the golang lib call can be achieved.
- For testing run npm start which will open App.tsx. This React Component calls out for the module created above and the underlying implementation will be called depending on the platform:
- Web = CalculatorModule.web.ts
- Android = CalculatorModule.kt
- iOS = CalculatorModule.swift
- After the module created have to build the whole together with Gradle otherwise it will not work. Use the following command to cleanup the sample generated code:
npx expo prebuild --clean --platform android - Execute
npm run androidin GoInReact folder and it should result like this:
-
Build Go module and copy the poc.aar file under ./modules/calculator/android/src/libs (run
make pre-build-react-androidin the root directory) -
Add the source above to the Gradle build file at ./modules/calculator/android/build.gradle
dependencies { implementation(name: 'poc', ext: 'aar') }
-
WARNING this is a tricky part because this folder is not in repo! It is created by the command at section II/5 . So now need to change the the prebuild Gradle config and rerun it to make things work
-
Go to android/build.gradle and add this to repositories section like this
allprojects { repositories { ... // Leave everything you jave as-is, but add the following: flatDir { dirs "$rootDir/../modules/golang-calculator/android/src/libs" } } }
-
now run
make build-react-androidin root folder
-
-
Now check that things are still fine rerun
npm run androidin GoInReact folder and things still should work. -
Now to do some useful things open CalculatorModule.kt and add some code.
It is important to reference your go package name (what you used in main.go) - this is some kind of convention. In our case it will be 'calculator'.
import calculator.Calculator as goCalculator // check your go files package name Function("hello") { var sum = goCalculator.add(3.0, 5.0) "3+5 is $sum (calculated by go calculator)!" }
6.Now run npm run android and the result should look like this:

TODO try to parametrize the calls for calculator
TODO
References: Using Golang + Gomobile To Build Android Application How to run GoLang code in your React-Native Android application using Expo Go React Native + Go Backend

