Building SDL2.0.9 in Android Studio 3.2 in Windows


#1

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.