Simple Directmedia Layer

ci: build simple Android SDL app using gradle

authored by

Anonymous Maarten and committed by
Anonymous Maarten
e1066639 8954e42b

+81 -26
+27 -14
.github/workflows/android.yml
··· 15 15 fail-fast: false 16 16 matrix: 17 17 platform: 18 - - { name: "Android.mk" } 18 + - { name: "Android.mk", ndk-build: 1 } 19 + - { name: "Gradle", gradle: 1 } 19 20 - { name: "CMake", cmake: 1, android_abi: "arm64-v8a", android_platform: 23, arch: "aarch64", artifact: "SDL-android-arm64", apk-artifact: "SDL-android-apks-arm64" } 20 21 - { name: "CMake (lean and mean)", cmake: 1, cppflags: "-DSDL_LEAN_AND_MEAN=1", android_abi: "arm64-v8a", android_platform: 23, arch: "aarch64", artifact: "SDL-lean-android-arm64", apk-artifact: "SDL-lean-android-apks-arm64" } 21 22 22 23 steps: 23 24 - uses: actions/checkout@v4 24 25 - uses: nttld/setup-ndk@v1 26 + if: ${{ matrix.platform.cmake || matrix.platform.ndk-build }} 25 27 id: setup_ndk 26 28 with: 27 29 local-cache: true 28 30 ndk-version: r21e 29 31 - name: Build (Android.mk) 30 - if: ${{ contains(matrix.platform.name, 'Android.mk') }} 32 + if: ${{ matrix.platform.ndk-build }} 31 33 run: | 32 34 ./build-scripts/androidbuildlibs.sh 33 35 - uses: actions/setup-java@v4 34 - if: ${{ contains(matrix.platform.name, 'CMake') }} 36 + if: ${{ matrix.platform.cmake || matrix.platform.gradle }} 35 37 with: 36 38 distribution: 'temurin' 37 - java-version: '11' 39 + java-version: '17' 40 + - name: Build app (Gradle) 41 + if: ${{ matrix.platform.gradle }} 42 + run: | 43 + ln -s ${{ github.workspace }} ${{ github.workspace }}/android-project/app/jni/SDL 44 + cd android-project 45 + ./gradlew -i assembleRelease 46 + # - name: Build library (Gradle) 47 + # if: ${{ matrix.platform.gradle }} 48 + # run: | 49 + # cd android-project 50 + # ./gradlew -i assembleRelease -PBUILD_AS_LIBRARY=1 38 51 - name: Setup (CMake) 39 - if: ${{ contains(matrix.platform.name, 'CMake') }} 52 + if: ${{ matrix.platform.cmake }} 40 53 run: | 41 54 sudo apt-get update 42 55 sudo apt-get install ninja-build pkg-config 43 56 - name: Configure (CMake) 44 - if: ${{ contains(matrix.platform.name, 'CMake') }} 57 + if: ${{ matrix.platform.cmake }} 45 58 run: | 46 59 cmake -S . -B build \ 47 60 -DCMAKE_C_FLAGS="${{ matrix.platform.cppflags }}" \ ··· 63 76 -DCMAKE_BUILD_TYPE=Release \ 64 77 -GNinja 65 78 - name: Build (CMake) 66 - if: ${{ contains(matrix.platform.name, 'CMake') }} 79 + if: ${{ matrix.platform.cmake }} 67 80 run: | 68 81 cmake --build build --config Release --parallel --verbose 69 82 - name: Build test apk's (CMake) 70 - if: ${{ contains(matrix.platform.name, 'CMake') }} 83 + if: ${{ matrix.platform.cmake }} 71 84 run: | 72 85 cmake --build build --config Release --parallel --verbose --target testautomation-apk testaudiocapture-apk testcontroller-apk testmultiaudio-apk testsprite-apk 73 86 - name: Install (CMake) 74 - if: ${{ contains(matrix.platform.name, 'CMake') }} 87 + if: ${{ matrix.platform.cmake }} 75 88 run: | 76 89 cmake --install build --config Release 77 90 echo "SDL3_DIR=$(pwd)/prefix" >> $GITHUB_ENV 78 91 ( cd prefix; find ) | LC_ALL=C sort -u 79 92 - name: Package (CPack) 80 - if: ${{ contains(matrix.platform.name, 'CMake') }} 93 + if: ${{ matrix.platform.cmake }} 81 94 run: | 82 95 cmake --build build/ --config Release --target package 83 96 - name: Verify CMake configuration files 84 - if: ${{ contains(matrix.platform.name, 'CMake') }} 97 + if: ${{ matrix.platform.cmake }} 85 98 run: | 86 99 cmake -S cmake/test -B cmake_config_build -G Ninja \ 87 100 -DCMAKE_TOOLCHAIN_FILE=${{ steps.setup_ndk.outputs.ndk-path }}/build/cmake/android.toolchain.cmake \ ··· 91 104 -DCMAKE_PREFIX_PATH=${{ env.SDL3_DIR }} 92 105 cmake --build cmake_config_build --verbose 93 106 - name: Verify sdl3.pc 94 - if: ${{ contains(matrix.platform.name, 'CMake') }} 107 + if: ${{ matrix.platform.cmake }} 95 108 run: | 96 109 export CC="${{ steps.setup_ndk.outputs.ndk-path }}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=${{ matrix.platform.arch }}-none-linux-androideabi${{ matrix.platform.android_platform }}" 97 110 export PKG_CONFIG_PATH=${{ env.SDL3_DIR }}/lib/pkgconfig 98 111 cmake/test/test_pkgconfig.sh 99 112 - uses: actions/upload-artifact@v4 100 - if: ${{ contains(matrix.platform.name, 'CMake') }} 113 + if: ${{ matrix.platform.cmake }} 101 114 with: 102 115 if-no-files-found: error 103 116 name: ${{ matrix.platform.artifact }} 104 117 path: build/dist/SDL3* 105 118 - uses: actions/upload-artifact@v4 106 - if: ${{ contains(matrix.platform.name, 'CMake') }} 119 + if: ${{ matrix.platform.cmake }} 107 120 with: 108 121 if-no-files-found: error 109 122 name: ${{ matrix.platform.apk-artifact }}
+6 -6
android-project/app/build.gradle
··· 24 24 abiFilters 'arm64-v8a' 25 25 } 26 26 cmake { 27 - arguments "-DANDROID_APP_PLATFORM=android-19", "-DANDROID_STL=c++_static" 27 + arguments "-DANDROID_PLATFORM=android-19", "-DANDROID_STL=c++_static" 28 28 // abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' 29 29 abiFilters 'arm64-v8a' 30 30 } ··· 45 45 jniLibs.srcDir 'libs' 46 46 } 47 47 externalNativeBuild { 48 - ndkBuild { 49 - path 'jni/Android.mk' 50 - } 51 - // cmake { 52 - // path 'jni/CMakeLists.txt' 48 + // ndkBuild { 49 + // path 'jni/Android.mk' 53 50 // } 51 + cmake { 52 + path 'jni/CMakeLists.txt' 53 + } 54 54 } 55 55 56 56 }
+22 -6
android-project/app/jni/src/CMakeLists.txt
··· 1 1 cmake_minimum_required(VERSION 3.6) 2 2 3 - project(MY_APP) 3 + project(my_app) 4 4 5 - find_library(SDL3 SDL3) 6 - 7 - add_library(main SHARED) 5 + if(NOT TARGET SDL3::SDL3) 6 + find_package(SDL3 CONFIG) 7 + endif() 8 8 9 - target_sources(main PRIVATE YourSourceHere.c) 9 + if(NOT TARGET SDL3::SDL3) 10 + find_library(SDL3_LIBRARY NAMES "SDL3") 11 + find_path(SDL3_INCLUDE_DIR NAMES "SDL3/SDL.h") 12 + add_library(SDL3::SDL3 UNKNOWN IMPORTED) 13 + set_property(TARGET SDL3::SDL3 PROPERTY IMPORTED_LOCATION "${SDL3_LIBRARY}") 14 + set_property(TARGET SDL3::SDL3 PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${SDL3_INCLUDE_DIR}") 15 + endif() 10 16 11 - target_link_libraries(main SDL3) 17 + if(NOT TARGET SDL3::SDL3) 18 + message(FATAL_ERROR "Cannot find SDL3. 12 19 20 + Possible ways to fix this: 21 + - Use a SDL3 Android aar archive, and configure gradle to use it: prefab is required. 22 + - Add add_subdirectory(path/to/SDL) to your CMake script, and make sure a vendored SDL is present there. 23 + ") 24 + endif() 13 25 26 + add_library(main SHARED 27 + YourSourceHere.c 28 + ) 29 + target_link_libraries(main PRIVATE SDL3::SDL3)
+26
android-project/app/jni/src/YourSourceHere.c
··· 1 + #include <SDL3/SDL.h> 2 + #include <SDL3/SDL_main.h> 3 + 4 + /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 5 + /* */ 6 + /* Remove this source, and replace with your SDL sources */ 7 + /* */ 8 + /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 9 + 10 + int main(int argc, char *argv[]) { 11 + (void)argc; 12 + (void)argv; 13 + if (SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO) < 0) { 14 + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed (%s)", SDL_GetError()); 15 + return 1; 16 + } 17 + 18 + if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Hello World", 19 + "!! Your SDL project successfully runs on Android !!", NULL) < 0) { 20 + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_ShowSimpleMessageBox failed (%s)", SDL_GetError()); 21 + return 1; 22 + } 23 + 24 + SDL_Quit(); 25 + return 0; 26 + }