SDL2 is a well known library for making games or interactive application for Linux, Windows or macOS. The library has also support for mobile devices. Building application for Android requires a little bit more effort than build for desktops.
Let’s go step-by-step.
First of all download Gradle 4 and add bin directory with gradle to your PATH environment variable.
Download and install Android Studio.
Clone sdl2-android-example – branch gradle-4-using-android-experimental-plugin project from GitHub.
Open the repo in Android Studio and open SDK Manager. Go to Tools, select menu Android, and select item SDK Manager.
In the lower right corner check option Show Package Details. Install Android SDK Build Tool e.g. version 28.0.0 and NDK.
When you open the sdl2-android-example you will see following structure. There are two gradle files in the top level directory. The first is build.gradle. The file contains a dependency on gradle-experimental plugin which is recommended for NDK builds for Android.
The second Gradle file is settings.gradle. The file contains a list of modules which are in the project. One module is SDL2 library. Then there are two further modules app and main. The app module is the bridge between Java and SDL2. The main module contains C code for the application.
These two files provide a skeleton for the build of the application. Each module SDL2, app and main are stored in the directory that matches the name a of module. Each module contains its own build.gradle which describes how to build the module.
The module file SDL2/build.gradle is interesting because it contains many excludes. It does not make sense to build all C files from SDL2 for Android.
The module file main/build.gradle contains information for linking the C application with SDL2 library.
The module file app/build.gradle contains information how to wrap all the C source code into final application.
First of all, it is necessary to build the libraries (.so files):
If you want to build just one library, you can type:
Gradle will build all flavors. If you’re experiencing problems with Android Studio, just sync the project after executing this command from command line.
Now you can deploy the application to a device or an emulator. Go to Gradle projects, expand :app module, expand Tasks, expand install and double click installDebug target.
We will discuss how to use further SDL2 libraries in next articles. You can find more under topic SDL2.
Note: the sample projet at GitHub already contains reference to other libraries like PNG. Please follow instructions described in the article SDL2_image for Android with PNG image format. The other option is to clone older version of application without PNG.
hello,
looks veryinteresting, but I keep getting the following error before being able to run the app:
Gradle ‘sdl2-android-example’ project refresh failed
Error:Exception thrown while executing model rule: NdkComponentModelPlugin.Rules#createNativeBuildModel(NativeBuildConfig, ModelMap, ModelMap, NdkHandler) > create(debug-arm64-v8a)
Shared library runtime files /home/alaindef/Android/sdl2-android-example/distribution/png/lib/arm64-v8a/libpng.so does not exist for prebuilt shared library ‘png:arm64-v8aDebugShared’.
can you help?
Hi Alain,
thank you for the question. The issue is caused by missing libpng. All ‘.so’ libraries must be build using distributeLib target.
This is described in the next article: https://georgik.rocks/sdl2_image-for-android-with-png-image-format/
To fix your problem, just type:
gradle :zip:distributeLib
gradle :png:distributeLib
gradle :SDL2_image:distributeLib
I will update the article and add a pointer to the article about PNG.
Please try it and let me know whether it works for you.
Hi Juraj,
Thank you so much for your work and your quick support. Thanks to that I got where I wanted to be!
of course, if I had read your readme.md, I should have found out for myself, but wtf reads the readme files? :)
I have put my cpp files in the right directory, and after a few hickups it worked fantastically. Our Android app now shows a map of the world, in which you can zoom and pan. the assets dir contains now more than 200Mb of data.
the final project should handle more than 800 files. right now the java heap size limit is a problem, but I did it already with the old ANT tool, so it should work with gradle too.
next challenge is to put the assets in an obb file (which worked with the old ant system). If you have any suggestions on how to do that with the gradle tool, I will be most happy to hear from you!
Thanks again for your great contribution to the community!
alain de feyter
belgium
Hi Alain,
I’m glad that it worked for you.
Yesterday I’ve published an update to the sdl2-android-example with JPEG support. Maybe it could be useful also for your app: https://georgik.rocks/sdl2_image-for-android-with-jpeg-image-format/
Here is hint how to increase heap size: https://stackoverflow.com/questions/37203884/increasing-the-gradle-heap-size
Private assets on Android might look like a common file system, but they’re not. All assets are compressed and Android creates virtual FS which is accessible only by the application. I would recommend storing big amounts of assets in External Storage. This will speed up the build and application will be smaller.
Here you can find more information about app-private data storage: https://developer.android.com/guide/topics/data/data-storage.html#AccessingExtFiles
Hi Alain,
I have important update for you. If you’d like to target API level 19 in your application then you can find here some hints how to make SDL2 works with API level 19: https://georgik.rocks/sdl2-for-android-api-level-19/
The source code of sdl2-android-example has been already updated.
Hi,
Everything compiles and installs, but when I run it on a device or emulator I get the following error:
SDL Error
An error occurred while trying to start the application. Please try again and/or reinstall.
Error: dlopen failed: library “libpng.so” not found.
if I open the apk with archive manager , the lib is not there. (eg. under “/lib/armeabi-v7/” which contains only these 3 files: libmain.so, libSDL2.so, libSDL2_image.so)
I have also tried the compatibility stuff for sdk-19 from your other post on this page. It doesn’t change anything.
here are some version numbers I use:
————————————————————
Gradle 4.0
————————————————————
Build time: 2017-06-14 15:11:08 UTC
Revision: 316546a5fcb4e2dfe1d6aa0b73a4e09e8cecb5a5
Groovy: 2.4.11
Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM: 1.8.0_131 (Oracle Corporation 25.131-b11)
OS: Linux 4.10.0-35-generic amd64
Android-SDK 23
Android Build tools 26.0.1
I have changed the SdkVersion variable to 23, and changed the buildtools version in all build.gradle files to ‘26.0.1’
Any idea what could be wrong? This problem has me puzzled for a few weeks now…
Grts
Karel
Hi Karel.
You’re using older verdion of source code.
Please update to latest master.
Here is the explanation: https://georgik.rocks/sdl2_image-for-android-with-png-image-format/
See the paragraph with update.
The library should have diferent name than png on system with API 19.
Hi Juraj,
Thanks for the quick reply, really appreciate it.
I am working on this project with Alain (my father and other poster on this website), and I copied his project, strange that on his system it works, and on mine not. I have tried the API-19 explanation you are referring to, but without success, although I am using API-21. I could indeed start over from your latest example, but I would have to redo everything, and the code I have at the moment works great on Alain his system.
In the meantime we will continue to try and solve it. If you come up with any more tips, always appreciated.
Grts
Karel
Hi Karel,
in my opinion, it is not necessary to restart the work. Just follow commit history from https://github.com/georgik/sdl2-android-example/commits/master
To fix your issue, just rename png library to SDL2_png. You can do it via refactor in Android Studio or just grab the folder from: https://github.com/georgik/sdl2-android-example/tree/master/SDL2_png
Then change settings.gradle to reflect the new name.
The major change is in https://github.com/georgik/sdl2-android-example/blob/master/main/build.gradle , make sure to include SDL2_png in repositories section and in dependencies section as:
library ‘SDL2_png’ linkage ‘shared’
Then clean the whole solution and build distributionLibs:
gradle clean dL
Then everything should work fine for you.
If it’s still not working, just let me know. We can arrange a call and solve the issue.
Hey Juraj,
I have tried renaming the ‘png’ library to SDL2_png, first by the AS refactor method, then by changing all gradle scripts and configurations. But it ended up complaining that it couldn’t find ‘libSDL2_png.so’.
Then comparing my devices with Alain, I found out that he is using a API23 emulator and a older harware nexus device to test, as I have an API24 emulator and a nvidia shield tablet running Nougat.
It seems that the problem is only from API24 and above, because on API23 it works well (Alain tried with API24 emulator and reproduced the dlopen error).
I found a solution to make it work on my tablet as well:
Copy the library files libpng.so (or libSDL2_png.so) from the distribution directory into
‘projectdir/app/src/main/jniLibs/CPU_ABI/libpng.so’
where ‘CPU_ABI’ are all the different directories related to the CPU: eg. armeabi, armeabi-v7a, x86, x86_64, etc.
Then rerun ‘gradle installDebug’.
I want to automate this copy in a Gradle task.
Are there any snags in this approach?
grts
Karel
Hi Juraj,
Yes I was too quick, after again trying to rename the png library it works.
I forgot to add the extra dependency in main/build.gradle.
My bad.
Again, thanks for the help!
grts Karel
Hi Karel.
I’m very glad that the solution works for you.
Yes, the gradle files are a little bit tricky. I was in similar situation like you. One of the projects was working the second was not working. It took me several hours to figure our reasons of problem.
I’ve been adding further libraries to sdl2-android-example, like JPEG, TTF and WAV support. Let me know if you’d like to use further libraries. I’d like to test them and add them to the main example, so other people in the community can utilize SDL2 for their projects.