Building SDL2.0.9 in Android Studio 3.2 in Windows

Hi fellow developers. I have now struggled many days and nights to get my C++ code to work in Android studio.
After almost getting it to work the fourth time I decided to document each step for my own help and also
help others out.

To help others out I decided to document each step so you don’t have to cry i vain and tear your hair.

I realize that these instructions only will be valid until Android Studio decides to change everything again.

I used real examples to not confuse you but please use your own naming.

For this instructions we wish to create a app named DemolitionCrew. Change accordingly to your situation
We need to utilize not only the SDL2 main lib but also SDL2_Image, SDL2_net etc

1. Create this folder structure

Folder Structure

c:\Development\android
c:\Development\android\SDL2
c:\Development\android\SDL2_image
c:\Development\android\SDL2_mixer
c:\Development\android\SDL2_net
c:\Development\android\SDL2_ttf

2. Download these zip archives with source:
https://www.libsdl.org/download-2.0.php
https://www.libsdl.org/projects/SDL_image/
https://www.libsdl.org/projects/SDL_mixer/
https://www.libsdl.org/projects/SDL_net/
https://www.libsdl.org/projects/SDL_ttf

3. Unpack every zip to individual folders

4. Copy the content to correct folders
From the SDL2.0.x folder you copy “include”,“src” and “Android.mk” to c:\Development\android\SDL2
From the “SDL2_image-2.0.x” folder you copy all content to c:\Development\android\SDL2_image
From the “SDL2_ttf-2.0.x” folder you copy all content to c:\Development\android\SDL2_ttf
From the “SDL2_net-2.0.x” folder you copy all content to c:\Development\android\SDL2_mixer
From the “SDL2_mixer-2.0.x” folder you copy all content to c:\Development\android\SDL2_mixer

NOTE! Make sure you copy from the right level in the folder. You must be in the folder that
have the “acinclude” folder present. There will be a lot of other files you don’t need but that
doesn’t matter.

5. Disable WebP in SDL_image
If you doesn’t have to have support for WebP you can edit the “Android.mk” file
so that line 16 says “SUPPORT_WEBP ?= false”
It makes life much easier.

6. Create the Project.
Copy the folder “android-project” from the SDL2-2.0.x folder to “c:\Development\android”-folder

Rename the folder “android-project” to what you want your project to be called.
In this case we name it “DemolitionCrew”

7. Start the command promt (WINKEY+R, Write “cmd” Enter)

Create these links so they will appear as linked fgolders in the project. It is important that
you have named your project folder in beforehand. Otherwise they diappear.

mklink /J c:\Development\android\DemolitionCrew\app\jni\SDL c:\Development\android\SDL2
mklink /J c:\Development\android\DemolitionCrew\app\jni\SDL_image c:\Development\android\SDL2_image
mklink /J c:\Development\android\DemolitionCrew\app\jni\SDL_mixer c:\Development\android\SDL2_mixer
mklink /J c:\Development\android\DemolitionCrew\app\jni\SDL_net c:\Development\android\SDL2_net
mklink /J c:\Development\android\DemolitionCrew\app\jni\SDL_ttf c:\Development\android\SDL2_ttf

8. Start Android Studio 3.2.1
Click on “Open an existying Android Studio Project”
Navigate to “C:\Development\android” and select your project. In this case “DemolitionCrew”.
Click “Open”

9. In the almost top left corner you can alter the view between “Android” and “Project”
Change to “Project” if it is not already choosen.
Expand the tree view DemolitionCrew->app->src->main

Double Click on AndroidManifest.xml

Change the package=“org.libsdl.app” on line 6 to "package=“com.xirbx.game” (Or what your company and app name is)

Change this to required true if you want touch screen to work on all smart phones. (Like Moto G4 Play)

Add these lines under the other permission lines if you wish SDL_net to access your local net and the internet

Save the file (CTRL+S)

10. Expand the tre view to this:
DemolitionCrew->app->jni
Double click on “Application.mk”
Uncomment line 5 so that it says “APP_STL := c++_shared”

Change line 10 so that it says: “APP_PLATFORM=android-16”
Add a line underneath that says “APP_CPPFLAGS += -fexceptions”

Expand the tree to DemolitionCrew->app
Double click on build.gradle
change line 16 from:

minSdkVersion 14

to

minSdkVersion 16

Change line 22 from

arguments “APP_PLATFORM=android-14”

to

arguments “APP_PLATFORM=android-16”

Change the ApplicationId on line 14 to this:
applicationId “com.xirbx.games.demolitioncrew”

On line 18 you need to increase the versionCode for each build you
upload to Google Play

11. Expand the tre view to this:
DemolitionCrew->app->jni->src
Double click on “Android.mk”

Change line 14 from

LOCAL_SHARED_LIBRARIES := SDL2

to:

LOCAL_SHARED_LIBRARIES := SDL2
SDL2_image
SDL2_mixer
SDL2_ttf
SDL2_net

Change line 12 from:

LOCAL_SRC_FILES := YourSourceHere.c

to

LOCAL_SRC_FILES :=
AssetManager.cpp
Components.cpp
ECS.cpp
Game.cpp
GameClient.cpp
GameMap.cpp
main.cpp
TextureManager.cpp
tinyxml2.cpp
UrlReader.cpp
Vector2D.cpp
xmltest.cpp
ResourcePath.cpp
tmxparser/base64/base64.cpp
tmxparser/miniz.c
tmxparser/TmxColor.cpp
tmxparser/TmxEllipse.cpp
tmxparser/TmxGroupLayer.cpp
tmxparser/TmxImage.cpp
tmxparser/TmxImageLayer.cpp
tmxparser/TmxLayer.cpp
tmxparser/TmxMap.cpp
tmxparser/TmxObject.cpp
tmxparser/TmxObjectGroup.cpp
tmxparser/TmxPolygon.cpp
tmxparser/TmxPolyline.cpp
tmxparser/TmxProperty.cpp
tmxparser/TmxPropertySet.cpp
tmxparser/TmxTerrain.cpp
tmxparser/TmxTerrainArray.cpp
tmxparser/TmxText.cpp
tmxparser/TmxTile.cpp
tmxparser/TmxTileLayer.cpp
tmxparser/TmxTileOffset.cpp
tmxparser/TmxTileset.cpp
tmxparser/TmxUtil.cpp

NOTE! You only have to list your .cpp and .c files. It will get the header-files
automatically.

Save the file (CTRL + S)

12. Expand DemolitionCrew->app->src->main
Here you will see “java”, “res” and “AndroidManifest.xml
NOTE! It is easy to accidentally choose the wrong src-folder.

Right click on “DemolitionCrew->app->src->main”
Select New->Folder->Assets Folder. (NOT Directory. the folder option is the one with a android figure on)
A window pops up. Click “Finish”
A new folder is created called “assets”

13. It is OK to breath now! :slight_smile:
Now you can minimize the Android studio window and in windows
open up our project folder:
In this folder i place all my assets:
C:\Development\android\DemolitionCrew\app\src\main\assets
Assets are stuff like all your game graphics, sound, music and levels.

In this folder i place all my C++ and C code
C:\Development\android\DemolitionCrew\app\jni\src
Also place all the header files for SDL including SDL_mixer.h, SDL_image.h, SDL_ttf.h and SDL_net.h
I guess there is a better way to do this but I was desperate. :slight_smile:

14. Go back to Android Studio
Navigate to DemolitionCrew->app->jni->src in the tree view
If you have a a main.cpp file, it could look like this.

#include “main.h”
extern “C”
{
using namespace std;
Game *game=nullptr;

int SDL_main(int argc, char* argv[])
{

game = new Game();
game->gameState=gs_LOADING_GAME;
game->init(“Game”); // Setting up SDL stuff and open screens

while(game->running())
{
game->update();
game->render();

}
game->clean();

return 0;

}

}

The important stuff here is the extern “C” after the #includes but before everything else
and that you should use SDL_main() instead of plain main()

15. It is time to make a java-class for our game.
Navigate to DemolitionCrew->app->src->main->java
Right click “java” and select “New->Package”. Name it “com.xirbx.game”
Right click “com.xirbx.game” and select “New->File”. Name it “DemolitionCrew.java” (Or your game name)

Double click on DemolitionCrew.java
and paste this text:

package com.xirbx.game;

import org.libsdl.app.SDLActivity;

public class DemolitionCrew extends SDLActivity { }

16. Expand the tree view DemolitionCrew->app->src->main

Double Click on AndroidManifest.xml

Change to

<activity android:name=“DemolitionCrew”

Save file (CTRL + S)

17. Naming the Game Icon
Expand the tree to this:
DemolitionCrew->app->src->main->res->values
Double click on the “strings.xml”
Change:

Game

To:

Demolition Crew

This string is where the AndroidManifest.xml gets its game name value from.
I guess you can localize this for different languages.

18. Changing the Icon picture
Expand the tree to this:
DemolitionCrew->app->src->main->res
In each folder that begins with “mipmap” there is a png image called “ic_launcher.png”
Replace those with your own images.
Make sure they are the correct sizes.

I think it is easier to change those from windows and not in Android studio.

19. When all this is done you should be able to build the entire project. (CTRL +F9)

20. Connect a Android device (That is set to developer mode I guess) and press Run (SHIFT +F10)

I hope this tutorial saved you some time. Please check out my games on app store and Google Play!
Jesper / xirBX AB

Ps. I am planning to make more games and I would like to help out the best I can.
After all… I have solved many of these problems by reading other comments on this forum.
Ds.

6 Likes

Thank you very mush DaSkuggo.
I was looking for that since a long time!
I test it on Android Studio 3.4 and everything works.
At step 11, every lib name for LOCAL_SHARED_LIBRARIES should be on the same line, separate by spaces or it complain about separator :
LOCAL_SHARED_LIBRARIES SDL2 SDL2_image SDL2_mixer SDL2_ttf SDL2_net

1 Like

Thanks for this i never can’t build whitout this. Work for me except the library SDL_Mixer i got these erros:

Android Studio: 3.4.2
Windows 10
SDL2 2.0.9
SDL_Mixer 2.0.4
NDK 20.0.5594570

Caused by: Build command failed.
Error while executing process D:\Android\SDK\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\Development\android\DuckDash\app\jni\Android.mk NDK_APPLICATION_MK=D:\Development\android\DuckDash\app\jni\Application.mk APP_ABI=x86_64 NDK_ALL_ABIS=x86_64 NDK_DEBUG=1 APP_PLATFORM=android-16 NDK_OUT=D:/Development/android/DuckDash/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=D:\Development\android\DuckDash\app\build\intermediates\ndkBuild\debug\lib APP_PLATFORM=android-16 D:/Development/android/DuckDash/app/build/intermediates/ndkBuild/debug/obj/local/x86_64/libmpg123.so}
make: *** No rule to make target `D:/Development/android/DuckDash/app/build/intermediates/ndkBuild/debug/obj/local/x86_64/libmpg123.so’. Stop.

I am about to build a new Android version of my game with my new setup so I will update the tutorial with all the components. Give me a couple of days. :slight_smile:

HI,I wasted 3 days about setting up the android-SDL2 envirment.the SDL2 updated 2.0.10,seem like better than 2.0.9,but still not working for me.really need your new tutorial. Thank you .

1 Like

The tutorial is almost done but I have to catch a train. But here is the brutal solution for the libmpg123.so issue. It can be handled better but here it is.

5b. Disable mp3 in SDL_Mixer
If you doesn’t have to support mp3 and can stick to OGG instead do disable this.

I tried around a bit until I got tired of the mpg123 issue. So this was my solution. (Brutal)
I guess this can all be solved by finding a correct library, but since I don’t use mp3 I just removed itsall together.

remove these lines in d:\Development\android\SDL2_mixer\Android.mk

Enable this if you want to support loading MP3 music via MPG123

SUPPORT_MP3_MPG123 ?= true
MPG123_LIBRARY_PATH := external/mpg123-1.25.6

This needs to be a shared library to comply with the LGPL license

ifeq ($(SUPPORT_MP3_MPG123),true)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(MPG123_LIBRARY_PATH)
LOCAL_CFLAGS += -DMUSIC_MP3_MPG123
LOCAL_SHARED_LIBRARIES += mpg123
endif

In the SDL_Mixer folder i removed these files:
d:\Development\android\SDL2_mixer\music_mpg123.h
d:\Development\android\SDL2_mixer\music_mpg123.c
The entire folder d:\Development\android\SDL2_mixer\external\mpg123-1.25.6

In the file d:\Development\android\SDL2_mixer\music.c I commented out line 40
//#include “music_mpg123.h”

If you discover this later on a rebuild might not do it. In that case you just have to
remove the “debug” and “release” folder in
D:\Development\android\DemolitionCrew\app.externalNativeBuild\ndkBuild

This could ofcourse be handled better but I didn’t have time for it. :slight_smile:

Thank you very much,I will try today.

The new updated tutorial for SDL 2.0.10 with android studio can now be found here: Building SDL2.0.10 in Android Studio 3.4.2 in Windows 10

Is there any tutorial for mac version

I will look in to this. I use both Mac and Windows.

1 Like

I am using Catalina OS in my mac.

Here you go my friend:
https://discourse.libsdl.org/t/building-sdl-2-0-12-with-android-studio-4-0-on-mac-os-catalina/