Skip to content

Commit cf5bb59

Browse files
committed
add support for android
JNI_VERSION_1_8 isn't defined in android ndk for some reason so we change that
1 parent 95e69f2 commit cf5bb59

5 files changed

Lines changed: 43 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ jobs:
5555
target:
5656
- os: ubuntu-latest
5757
type: linux
58+
- os: ubuntu-latest
59+
type: android
5860
- os: ubuntu-latest
5961
type: windows
6062
- os: macos-latest
@@ -82,6 +84,7 @@ jobs:
8284

8385
- name: Build
8486
run: |
87+
export NDK_HOME=$ANDROID_NDK_HOME
8588
chmod +x buildSrc/scripts/build.sh
8689
buildSrc/scripts/build.sh ${{ matrix.target.type }}
8790

buildSrc/scripts/build.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,28 @@ case "$VTYPE" in
9797
fi
9898
echo "Universal library created at /tmp/imgui/dst/libimgui-java64.dylib successfully"
9999
;;
100+
android)
101+
echo "Running Gradle task for Android..."
102+
./gradlew imgui-binding:generateLibs -Denvs=android -Dfreetype=false
103+
if [ $? -ne 0 ]; then
104+
echo "Gradle task for Android failed"
105+
exit 1
106+
fi
107+
108+
echo "Checking if the generated SO files exists..."
109+
check_file_exists /tmp/imgui/libs/arm64-v8a/libimgui-java64.so
110+
check_file_exists /tmp/imgui/libs/armeabi-v7a/libimgui-java64.so
111+
check_file_exists /tmp/imgui/libs/x86/libimgui-java64.so
112+
check_file_exists /tmp/imgui/libs/x86_64/libimgui-java64.so
113+
114+
echo "Copying the generated SO files to the destination directory..."
115+
cp /tmp/imgui/libs/* /tmp/imgui/dst
116+
if [ $? -ne 0 ]; then
117+
echo "Failed to copy SO files to /tmp/imgui/dst/libimgui-java64.so"
118+
exit 1
119+
fi
120+
echo "SO files copied to /tmp/imgui/dst/<abi> successfully"
121+
;;
100122
*)
101123
echo "Unknown vendor type: $VTYPE"
102124
exit 1

buildSrc/scripts/vendor_freetype.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ case "$VTYPE" in
9898
fi
9999
echo "Universal library created at lib/libfreetype.a"
100100
;;
101+
android)
102+
# Skip freetype building for now because freetype repo doesn't seem to support android builds.
103+
;;
101104
*)
102105
echo "Unknown vendor type: $VTYPE"
103106
exit 1

buildSrc/src/main/groovy/tool/generator/GenerateLibs.groovy

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class GenerateLibs extends DefaultTask {
3232
private final String[] buildEnvs = System.getProperty('envs')?.split(',')
3333
private final boolean forWindows = buildEnvs?.contains('windows')
3434
private final boolean forLinux = buildEnvs?.contains('linux')
35+
private final boolean forAndroid = buildEnvs?.contains('android')
3536
private final boolean forMac = buildEnvs?.contains('macos')
3637
private final boolean forMacArm64 = buildEnvs?.contains('macosarm64')
3738

@@ -103,7 +104,8 @@ class GenerateLibs extends DefaultTask {
103104
}
104105

105106
// Generate platform dependant ant configs and header files
106-
def buildConfig = new BuildConfig('imgui-java', tmpDir, libsDirName, jniDir)
107+
String soFileName = !forAndroid ? 'imgui-java' : 'imgui-java64' // Android generates libimgui-java.so otherwise which isn't expected
108+
def buildConfig = new BuildConfig(soFileName, tmpDir, libsDirName, jniDir)
107109
BuildTarget[] buildTargets = []
108110

109111
if (forWindows) {
@@ -118,6 +120,12 @@ class GenerateLibs extends DefaultTask {
118120
buildTargets += linux64
119121
}
120122

123+
if (forAndroid) {
124+
buildTargets += new BuildTarget(Os.Android, Architecture.Bitness._32, new String[] {"**/*.c"}, new String[0],
125+
new String[] {"**/*.cpp"}, new String[0], new String[0], "", "-O2 -Wall -D__ANDROID__", "-O2 -Wall -D__ANDROID__",
126+
"-lm -Wl,-z,max-page-size=0x4000 -stdlib=libc++ -lc++_shared");
127+
}
128+
121129
if (forMac) {
122130
buildTargets += createMacTarget(Architecture.x86)
123131
}
@@ -137,12 +145,15 @@ class GenerateLibs extends DefaultTask {
137145
BuildExecutor.executeAnt(jniDir + '/build-windows64.xml', commonParams)
138146
if (forLinux)
139147
BuildExecutor.executeAnt(jniDir + '/build-linux64.xml', commonParams)
148+
if (forAndroid) // Contrary to the name, this builds all four ABIs (arm64, arm, x86, x86_64)
149+
BuildExecutor.executeAnt(jniDir + '/build-android32.xml', commonParams)
140150
if (forMac)
141151
BuildExecutor.executeAnt(jniDir + '/build-macosx64.xml', commonParams)
142152
if (forMacArm64)
143153
BuildExecutor.executeAnt(jniDir + '/build-macosxarm64.xml', commonParams)
144-
145-
BuildExecutor.executeAnt(jniDir + '/build.xml', '-v', 'pack-natives')
154+
// Exclude android because android packages into aar, not jar
155+
if (!forAndroid)
156+
BuildExecutor.executeAnt(jniDir + '/build.xml', '-v', 'pack-natives')
146157

147158
if (forWindows)
148159
checkLibExist("windows64/imgui-java64.dll")

imgui-binding/src/main/native/jni_jvm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Jni
1010

1111
JNIEnv* GetEnv() {
1212
JNIEnv* env;
13-
jint res = jvm->GetEnv((void**)(&env), JNI_VERSION_1_8);
13+
jint res = jvm->GetEnv((void**)(&env), JNI_VERSION_1_6);
1414
assert(res == JNI_OK);
1515
return env;
1616
}

0 commit comments

Comments
 (0)