SDL: Create a functional CMake project using androidbuild.sh

From 1a68d846deb457894c0e7b6c4b5c9f277782aebd Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Mon, 17 Jun 2024 23:46:08 +0200
Subject: [PATCH] Create a functional CMake project using androidbuild.sh

---
 .github/workflows/android.yml    | 20 ++++++++++++++++----
 Android.mk                       |  2 +-
 android-project/app/build.gradle | 14 +++++++++-----
 build-scripts/androidbuild.sh    |  5 +++++
 4 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml
index fc083958d9bd3..d30559a869ca3 100644
--- a/.github/workflows/android.yml
+++ b/.github/workflows/android.yml
@@ -37,16 +37,28 @@ jobs:
         with:
           distribution: 'temurin'
           java-version: '17'
-      - name: Build app (Gradle)
+      - name: Create Gradle project
         if: ${{ matrix.platform.gradle }}
         run: |
-          ln -s ${{ github.workspace }} ${{ github.workspace }}/android-project/app/jni/SDL
-          cd android-project
+          build-scripts/androidbuild.sh org.libsdl.testcontroller src/test/SDL_test_* test/testcontroller.c test/gamepad* test/testutils*
+          echo ""
+          echo "Project contents:"
+          echo ""
+          find "build/org.libsdl.testcontroller"
+      - name: Build app (Gradle & ndk-build)
+        if: ${{ matrix.platform.gradle }}
+        run: |
+          cd build/org.libsdl.testcontroller
           ./gradlew -i assembleRelease
+      - name: Build app (Gradle & CMake)
+        if: ${{ matrix.platform.gradle }}
+        run: |
+          cd build/org.libsdl.testcontroller
+          ./gradlew -i assembleRelease -PBUILD_WITH_CMAKE=1
 #      - name: Build library (Gradle)
 #        if: ${{ matrix.platform.gradle }}
 #        run: |
-#          cd android-project
+#          cd build/org.libsdl.testcontroller
 #          ./gradlew -i assembleRelease -PBUILD_AS_LIBRARY=1
       - name: Setup (CMake)
         if: ${{ matrix.platform.cmake }}
diff --git a/Android.mk b/Android.mk
index 01f7460898d4a..0bdb6386d26a7 100644
--- a/Android.mk
+++ b/Android.mk
@@ -12,7 +12,7 @@ LOCAL_MODULE := SDL3
 
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/src
 
-LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
+LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
 
 LOCAL_SRC_FILES := \
 	$(subst $(LOCAL_PATH)/,, \
diff --git a/android-project/app/build.gradle b/android-project/app/build.gradle
index 35cb0ebf6bf04..d34ba31457712 100644
--- a/android-project/app/build.gradle
+++ b/android-project/app/build.gradle
@@ -1,3 +1,4 @@
+def buildWithCMake = project.hasProperty('BUILD_WITH_CMAKE');
 def buildAsLibrary = project.hasProperty('BUILD_AS_LIBRARY');
 def buildAsApplication = !buildAsLibrary
 if (buildAsApplication) {
@@ -45,11 +46,14 @@ android {
             jniLibs.srcDir 'libs'
         }
         externalNativeBuild {
-            // ndkBuild {
-            //     path 'jni/Android.mk'
-            // }
-            cmake {
-                path 'jni/CMakeLists.txt'
+            if (buildWithCMake) {
+                cmake {
+                    path 'jni/CMakeLists.txt'
+                }
+            } else {
+                ndkBuild {
+                    path 'jni/Android.mk'
+                }
             }
         }
 
diff --git a/build-scripts/androidbuild.sh b/build-scripts/androidbuild.sh
index 1a107e27dee25..399bb3fbde184 100755
--- a/build-scripts/androidbuild.sh
+++ b/build-scripts/androidbuild.sh
@@ -61,8 +61,13 @@ else
     cp -r $SDLPATH/include $BUILDPATH/app/jni/SDL
 fi
 
+cp -r $SDLPATH/LICENSE.txt $BUILDPATH/app/jni/SDL
+cp -r $SDLPATH/README.md $BUILDPATH/app/jni/SDL
 cp -r $SDLPATH/Android.mk $BUILDPATH/app/jni/SDL
+cp -r $SDLPATH/CMakeLists.txt $BUILDPATH/app/jni/SDL
+cp -r $SDLPATH/cmake $BUILDPATH/app/jni/SDL
 sed -i -e "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/app/jni/src/Android.mk
+sed -i -e "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/app/jni/src/CMakeLists.txt
 sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/build.gradle
 sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/src/main/AndroidManifest.xml