Using gomobile to compile openim-sdk-core for Android/iOS
June 10, 2026 · View on GitHub
Environment Setup
1. Go Language Environment and gomobile Setup
1. Install Go Language Environment
- Install Go language (version 1.18 or higher).
2. Configure GOPATH Environment Variable
-
Use
go envto check Go's environment variable settings. Here's a typical output example:> go env set GOPATH=D:\Go\gopath set GOMODCACHE=D:\Go\gopath\pkg\mod -
Based on the
go envoutput, add GOPATH to your system's environment variables:- Windows: Right-click "This PC", select "Properties -> Advanced system settings -> Environment Variables", add the GOPATH path (e.g., D:\Go\gopath) to the Path variable.
- Mac/Linux: Edit ~/.bashrc or ~/.zshrc in terminal, add the following content:
export GOPATH=/Users/youruser/go # Replace with your actual path export PATH=$PATH:$GOPATH/bin
3. Install gomobile and gobind
In Go 1.18 or higher, execute the following commands to install the latest gomobile and gobind:
go install golang.org/x/mobile/cmd/gomobile@latest
go install golang.org/x/mobile/cmd/gobind@latest
4. Initialize gomobile
Execute the following command to complete gomobile initialization:
gomobile init
Compiling Android AAR Package on Windows Platform
Environment Requirements
-
Ensure Go and gomobile are configured correctly: Execute
gomobile versionto verify if the tools are installed successfully. -
Install Android Development Environment: Ensure the latest version of Android Studio is installed.
-
Configure Android NDK: Download NDK for Windows, extract it to the
ndk-bundledirectory of Android SDK. For example:C:\Users\Admin\AppData\Local\Android\Sdk\ndk-bundle -
Configure Make Command Support (Optional):
-
Windows doesn't come with
make, you can install MinGW64:- Download and install MinGW64.
- After installation, add MinGW's
bindirectory (e.g.,C:\mingw64\bin) to system environment variables.
-
- If you can't install
make, you can directly use thegomobile bindcommand for compilation.
Compiling AAR Package
Navigate to your project root directory, such as openim-sdk-core, and run the following command to compile the Android AAR package:
gomobile bind -androidapi 21 -v -trimpath -ldflags='-s -w -extldflags "-Wl,--gc-sections,--as-needed,-z,max-page-size=16384"' -o ./open_im_sdk.aar -target=android ./open_im_sdk/ ./open_im_sdk_callback/
Notes:
- Ensure network connectivity, as dependency packages need to be downloaded from GitHub during compilation. First run may take a long time.
- If using MinGW64, you can execute:
mingw32-make android - After compilation is complete, you'll see output similar to:
aar: jni/armeabi-v7a/libgojni.so aar: jni/arm64-v8a/libgojni.so aar: jni/x86/libgojni.so aar: jni/x86_64/libgojni.so aar: R.txt aar: res/ ...
The open_im_sdk.aar file will be generated in the current directory.
- Import the generated AAR package into your project through Android Studio's local import method to use the exported functions and callback interfaces.
Compiling Android AAR Package and iOS xcframework on macOS Platform
Environment Requirements
- Install Xcode: Ensure Xcode is installed (recommended version: 15.4 or higher).
- Install Android Studio: Ensure Android SDK and NDK are installed and configured (Mac recommended NDK version:
20.1.5948944(r20b) or20.0.5594570).
Compiling Android AAR Package
Navigate to the project root directory (e.g., openim-sdk-core) and run:
make android
After compilation is complete, import the generated AAR package into your Android Studio project.
Compiling iOS xcframework Library
-
Navigate to the project root directory.
-
Execute the following command to compile iOS xcframework:
make ios -
After compilation is complete, import the generated
.xcframeworkfile into your Xcode project.
Common Issues and Solutions
- Stuck on writing
go.modfile
- This might be due to network issues causing dependency download failures. Try setting Go proxy:
If you're in China, you can use:go env -w GOPROXY=https://proxy.golang.org,directgo env -w GOPROXY=https://goproxy.cn,direct
- Cannot find
makecommand
- On Windows, ensure MinGW64 is installed and use
mingw32-makecommand instead ofmake. - Or directly run the
gomobile bindcommand.
- NDK Version Compatibility Issues
- If you encounter NDK version compatibility issues, try switching to the recommended version (
20.1.5948944(r20b) or20.0.5594570) and ensure the path is correct.
Through gomobile, you can easily package Go language written openim-sdk-core code into Android AAR packages or iOS xcframework libraries, making it convenient for integration and use on mobile