3 minutes
SDL2 example Android
Introduction
These instructions will focus on Linux / CMake but it can be easily adopted when using Android Studio on other platforms like Windows and OSX. I have used [Allegro] for many years but recently I decided to switch to SDL2 because it’s a somewhat lower level library and has more cross platform support for some applications such as bluetooth & USB name a few. I usually develop under Linux whenever I can and I needed to port my old graphics engine to the Android platform. Since my engine already was developed for cross platform - and has been ported to Windows and OSX - I thought Android should be fairly simple. Well as it turns out I was in for a surprise. I like difficult challanges as much as any other C++ developer but Android Studio + SDL2 was more like being in the passenger seat of destruction derby. I admit I tried doing things the hard way (the right way in my view): use only command line tools and the cross compiler. But not knowing about the pitfalls of Android build system I had to resort to for now using Android Studio, sigh. So this guide describes how to build a simple SDL2 project with the latest Android Studio 2020.3.2. I will focus on the pitfalls I ran into as the SDL2 android docs are missing some details.
Install Android Studio
I won’t go into how to install Android Studio as there are a few good tutorials online not to mention the Android documentation.
Here is the download site.
Don’t forget that afterwards you will need to install the Android NDK as our code is in C/C++ and without it we can only write Java/Kotlin, which would make us all very sad developers indeed…
You want to select from the SDK manager->SDK Tools->CMake to be sure.
Create a virtual device via AVD
There is an AVD(Virtual Device Manager) built into Android Studio that let’s you create devices if you choose to use the emulator. Just click on the AVD icon on the right top corner to bring up the manager and follow the wizard to create a new device.
Download SDL2Hello
- Download or clone my SDL2Hello example code from github.
$git clone git@github.com:ambitslix/SDL2Hello.git
Or download the zip/tar.gz and extract it to a convenient location.
# Clone both
$git clone https://github.com/libsdl-org/SDL
$git clone https://github.com/libsdl-org/SDL_image
Now we will symlink SDL and SDL_image into our SDL2Hello/app/src/main/cpp folder so our CMakeLists.txt will find. You can alternately clone both repos inside that folder as well. But I prefer symlinks as I don’t want to clutter my project repo with other git repos inside it. Git Submodules are a nightmare, but that’s a topic of another post…
# Create symlinks
ln -s /path/SDL /path/SDL2Hello/app/src/main/cpp
ln -s /path/SDL_image /path/SDL2Hello/app/src/main/cpp
Now you should be able to open the folder SDL2Hello from Androd Studio via File->Open. And SDL and SDL_image should be inside app/src/main/cpp.
Build + Run SDL2Hello
Android Studio will - after you open the project - automatically configure it. Then you can build it from the menu. If all goes well you should see in the build log at the bottom that that the build succeded.
BUILD SUCCESSFUL in 56s
Now you can try to run the app in the emulated device, and you should see the folllowing:
PitFalls
564 Words
2021-09-02 17:00