Feature request) cmake build for Android
This bug report was migrated from our old Bugzilla tracker.
Reported in version: don't know Reported for operating system, platform: Android (All), All
Comments on the original bug report:
On 2020-03-23 02:50:10 +0000, wrote:
Currently the cmake build doesn't support building Android apks directly and require adding a manual java project, etc. which makes it difficult to create a clean build process.
This would allow for easier adoption and cleaner automation for all platforms, aligning with the windows, mac, and ios builds.
Building an apk means building a finished application, which SDL isn't: it is a library.
Android is a java/kotlin-first platform with custom build pipelines, for which CMake has limited support. Google has selected gradle/ant as their build system of choice, used by 99% of app developers. We would always be playing catch-up if we would add a complete CMake based toolchain for building Android apk's. To give an idea of the complexity, see this project: https://github.com/jbendtsen/tiny-android-template
Instead, I propose the following.
Currently, the SDL repo contains an android project which users are supposed to put their game sources into, create a subclass SDLActivity and modify AndroidManifest.xml.
I propose to modify the gradle script to create a Android Library (.aar file).
This file, e.g. named SDL2-2.23.0.aar would contain (compiled versions of) the java sources + SDL.so.
Mobile developers can then use this archive as a base.
The only thing I don't know yet is how to link to the libSDL2.so library inside the aar archive, and how CMake will be able to use+find it.
If the above concept ends up working, we could modify the satellite libraries to also create aar libraries. Then, a developer can use SDL2 inside his mobile Android game by adding SDL2 to its gradle file.
currently I have Java/Kotlin + SDL2 activity + SDL2 C stuff + cpp engine... Using CMake through gradle. What is the idea here, you plan to remove the Android project?
I really use the Java/Kotlin part to provide native menus and Android specific features.
The Android app project is a feature because it gives a how to start a project that uses SDL2!
I"m thinking out aloud. I'm not planning to do anything yet (or remove anything). I haven't even installed android-ndk yet.
Most Desktop SDL CMake applications development goes as follows:
find_package(SDL2 REQUIRED)
add_executable(my_game ${MY_SOURCES})
target_link_libraries(my_game PRIVATE SDL2::SDL2)
I think it would be nice to let Android development re-use the same pattern.
My idea (I am not sure it will actually work) is that you then will add a dependency on SDL2 in your gradle build script and call your cmake build script in gradle.
Gradle will then combine the SDL2 java sources, SDL2.so, my_game.so and your java+kotlin sources into an apk.
Please keep the existing way available. Any change will be really disruptive. I haven't yet been able to switch out from 2.0.12 due to the regressions and am just waiting for a stable SDL2 to happen. And I see this has the 2.0.24 milestone attached to it, so this is a really big break in compatibility in a minor release!!!
I do have ObjectiveC in apple code, and iOS requires me Xcode, Emscripten requires HTML+JS. If you are properly supporting a platform you are going to have to provide specific changes to make it natural in that platform, there's no way SDL2 can be magical and make it work, because then it would need to support all features that exist exclusively in each platform in all platforms. If you go for a CMake only without gradle, you already lose the Play Asset, so you get games that are limited to 100MBs including everything, according to Play Store rules.
Android is the only port of SDL2 that the project to the user is mapped on how to do (#5237), and instead of having this for other platforms you are proposing removing the only one that is correct.
Please keep the existing way available. Any change will be really disruptive. I haven't yet been able to switch out from 2.0.12 due to the regressions and am just waiting for a stable SDL2 to happen.
Do you have bugs entered for the regressions you're seeing?
#6291 addresses this. When adopted, Android projects should simply need to add sdl to its dependencies. Java classes + libraries are all encapsulated in the Android archive.
#7581 adds creating a SDL3.jar archive to the cmake script, that might be injected in your gradle script. (I did not test this)
It also adds a way to create apks using CMake, without gradle. The code is not supported for external projects, but can be used as a guide.