Building SDL2 for android with gradle and CMake

Hello everyone !

I’ve been struggling for several days now to make SDL2 work on android.

I’ve found a great SDL2 + gradle example: https://github.com/stephen47/android-sdl2-gradle-template

However, it uses an old version of the gradle plugin that doesn’t handle CMake, and I need CMake to easily build a big project that already uses it on desktop platforms. Fortunately, the last versions of gradle and android studio base their native builds on CMake and Android makefiles (which I can use to easily build SDL2 for android).

I’ve managed to create a sample project that builds successfully: https://github.com/pvallet/hello-sdl2-android

But it crashes on execution: the programs receives SIGSEGV (signal SIGSEGV invalid address (fault address 0x10)) at the end of the function onCreate() in SDLActivity.java. The debugger is set to be able to debug C++, but the execution stops before reaching the first breakpoint in the C++ main function.

Moreover, when building, I get a message saying SDLActivity uses a deprecated API:
Note: /path/to/SDLActivity.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

It makes me wonder, is SDLActivity.java up-to-date with the 2017 versions of android SDK (and gradle ?)

Any help would be much appreciated, I’m pretty much stuck there :slight_smile:

I’ve finally found a good CMake + android example \o/, I think I’ll try building my project from this one.

Nice that you found it useful. I haven’t even myself migrated my actual bigger project to use cmake on android. I’m still using ndk-build mainly because I’m using SDL_image and SDL_net that don’t have cmake build support yet.

But please tell if you find any issues with the example/test project or have any improvement suggestions.

There are at least a couple of known issues to be aware of in SDL when using gradle+cmake. I made a patch too but it has not been applied yet:
https://bugzilla.libsdl.org/show_bug.cgi?id=3665

Thanks for the info ! I had indeed noticed that SDL_Delay was not working.

In the end I fixed my repository, because I needed to be able to use SDL2_image along with CMake. It does work now, what I was missing was that I needed to include SDL2/src/main/android/SDL_android_main.c along with my source files.

I have to thank you for your repository, it helped me a lot building my project and identifying this problem.

Here is the repository containing the starter example with gradle + CMake + SDL + SDL_image
Any remark or suggestion is much welcome.