SDL2 / Android setup & support

Hi All,

I have been struggling for a while trying to get a simple SDL app to work on Android. While there are many tutorials out there, they don’t really explain things that a beginner like me can understand. Although I am fluent in C++ and SDL, when it comes to porting I am a beginner and really need some help.

So far I have

  1. Downloaded the SDL 2 source files and stored them on my hard drive in a folder
  2. I have the Android SDK and NDK in seperate folders
  3. I know I have to build the SDL source files using the NDK, and also eventually build all my game files with the NDK.
  4. I have added the path of the NDK folder to my PATH, so I can run the ndk command from anywhere in the cmd window.
  5. I am aware that SDL provides a “android-project” folder, where I need to place everything in the “jni/src” folder
  6. Now this is where I get stuck - do I place all the SDL2 source files one by one in the jni folder or just my game files. How do I build, because when I run “ndk-build” I get all sorts of errors.

Would somebody be kind enough to give me a full explanation of how this works. I am even willing to skype with someone so I can get a better understanding of it all.

Many many thanks guys

:slight_smile:

You should copy only the source code of your application inside jni/src, as well as edit jni/src/Android.mk to include your files (you should see something like YourSourceHere.c)

For the application data, create a new top-level directory inside the android project named ‘assets’. Copy all of your application files there.

After that, you should be ready to build your app with ndk-build. For more information, read README-android.txt or ask here…

– Aggelos KolaitisOn Sep 27, 2013, at 11:28 AM, “djkarstenv” wrote:

Hi All,

I have been struggling for a while trying to get a simple SDL app to work on Android. While there are many tutorials out there, they don’t really explain things that a beginner like me can understand. Although I am fluent in C++ and SDL, when it comes to porting I am a beginner and really need some help.

So far I have

  1. Downloaded the SDL 2 source files and stored them on my hard drive in a folder
  2. I have the Android SDK and NDK in seperate folders
  3. I know I have to build the SDL source files using the NDK, and also eventually build all my game files with the NDK.
  4. I have added the path of the NDK folder to my PATH, so I can run the ndk command from anywhere in the cmd window.
  5. I am aware that SDL provides a “android-project” folder, where I need to place everything in the “jni/src” folder
  6. Now this is where I get stuck - do I place all the SDL2 source files one by one in the jni folder or just my game files. How do I build, because when I run “ndk-build” I get all sorts of errors.

Would somebody be kind enough to give me a full explanation of how this works. I am even willing to skype with someone so I can get a better understanding of it all.

Many many thanks guys


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi

thanks for your reply. I put a main.cpp file in the jni/src folder but i still get errors.

my android.mk file looks like this :

LOCAL_PATH = $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE = main

SDL_PATH = /SDL2

LOCAL_C_INCLUDES = /SDL2/include

Add your application source files here…

LOCAL_SRC_FILES = /SDL2/src/main/android/SDL_android_main.c
main.cpp

LOCAL_SHARED_LIBRARIES = SDL2

LOCAL_LDLIBS = -lGLESv1_CM -llog

include $(BUILD_SHARED_LIBRARY)

  • All the SDL2 source files are in C:\SDL2
  • when i build it says “C:\Android\NDK\build\core\main.cpp is not found” (why is it looking for main.cpp in the NDK folder when its supposed to be looking in the jni/src folder?)

thanks again

:slight_smile: :slight_smile:

Well, that I do not know :slight_smile: My knowledge is limited to this basic stuff [which is enough for my needs].

Looks like something is messed up with the configuration files somewhere, and I really hope some guru will be able to answer that.

The only thing I can suggest is starting over from a clean ‘android-project’, doing every step carefully.

– Aggelos KolaitisOn Sep 27, 2013, at 4:20 PM, “djkarstenv” wrote:

Hi

thanks for your reply. I put a main.cpp file in the jni/src folder but i still get errors.

my android.mk file looks like this :

LOCAL_PATH = $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE = main

SDL_PATH = /SDL2

LOCAL_C_INCLUDES = /SDL2/include

Add your application source files here…

LOCAL_SRC_FILES = /SDL2/src/main/android/SDL_android_main.c
main.cpp

LOCAL_SHARED_LIBRARIES = SDL2

LOCAL_LDLIBS = -lGLESv1_CM -llog

include $(BUILD_SHARED_LIBRARY)

  • All the SDL2 source files are in C:\SDL2
  • when i build it says “C:\Android\NDK\build\core\main.cpp is not found” (why is it looking for main.cpp in the NDK folder when its supposed to be looking in the jni/src folder?)

thanks again


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Edit :

ok, so now my Android.mk file looks like this :

LOCAL_PATH = $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE = main

SDL_PATH = /SDL2

LOCAL_C_INCLUDES = /SDL2/include

Add your application source files here…

LOCAL_SRC_FILES = /SDL2
LOCAL_SRC_FILES = /SDL2/src/main/android/SDL_android_main.c
LOCAL_SRC_FILES = /Users/Karsten/Desktop/android-project/jni/src/main.cpp

LOCAL_SHARED_LIBRARIES = SDL2

LOCAL_LDLIBS = -lGLESv1_CM -llog

include $(BUILD_SHARED_LIBRARY)

  • under LOCAL_SRC_FILES, this is where i placed the directories for my SDL source folder and the main.cpp file. Now things are slowly building, BUT i get a lot of errors stating “…undefined reference to …” (about 13 of those errors) I am guessing i did not successfully build the SDL source code. So how do i do that?

Thanks again!

:slight_smile: :slight_smile: :slight_smile:

LOL, that is a clean setup, i just changed a few configs around to avoid errors and got a dose of extra errors.

are there absolutely no tutorials on this or books? Its frustrating to dig for clues on this

:slight_smile:

You have an error in your Android.mk file.

It should be:

LOCAL_SRC_FILES = /SDL2/src/main/android/SDL_android_main.c
/Users/Karsten/Desktop/android-project/jni/src/main.cpp

The way you put it, you are overwriting the LOCAL_SRC_FILES on each line
(the \ at the end of the line means “this line continues on the next one”)

You also edited other variables (like SDL_PATH), I suggest you use the
default Android.mk file provided and just edit the bare minimum (that is,
add your sources and leave the rest be).

2013/9/27 djkarstenv > **

Edit :

ok, so now my Android.mk file looks like this :

LOCAL_PATH = $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE = main

SDL_PATH = /SDL2

LOCAL_C_INCLUDES = /SDL2/include

Add your application source files here…

LOCAL_SRC_FILES = /SDL2
LOCAL_SRC_FILES = /SDL2/src/main/android/SDL_android_main.c
LOCAL_SRC_FILES = /Users/Karsten/Desktop/android-project/jni/src/main.cpp

LOCAL_SHARED_LIBRARIES = SDL2

LOCAL_LDLIBS = -lGLESv1_CM -llog

include $(BUILD_SHARED_LIBRARY)

  • under LOCAL_SRC_FILES, this is where i placed the directories for my SDL
    source folder and the main.cpp file. Now things are slowly building, BUT i
    get a lot of errors stating “…undefined reference to …” (about 13 of
    those errors) I am guessing i did not successfully build the SDL source
    code. So how do i do that?

Thanks again!

[image: Smile] [image: Smile] [image: Smile]


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Gabriel.

djkarstenv wrote:

I am guessing i did not successfully build the SDL source code. So how do i do that?

all the info is in the readmes and on google. take it step by step.

you don’t need to build the SDL sources for android, that is done for you by the build system.

you should mention you are on Windows. i think you can still make symbolic links but that’s not the way windows ppl think so it’ll just be copies :slight_smile:

  1. first of all, start with a fresh copy of the SDL2 sources extracted to C:\SDL2_android. it’s important that this folder is never used to build the SDL for the windows target.

  2. copy C:\SDL2_android\android-project to C:<project>. this whole directory will only ever be for one android project so name it like that.

  3. copy your whole source tree into C:<project>\jni\src

  4. copy the whole C:\SDL2_android folder into C:<project>\jni

  5. rename the C:<project>\jni\SDL2_android folder to C:<project>\jni\SDL (this is another place where symbolic linking makes sense but copies work!)

  6. edit the C:<project>\jni\src\Android.mk file (info below)

  7. run ndk-build from C:<project>

now everything is in place and the only step left is to configure Android.mk to help the NDK build system. i’m copying my working Android.mk file below for reference. any wrong move here will make it totally fail so don’t edit any files unless you find some documentation that says to.

notice:

the SDL_PATH is …/SDL this points to the folder you copied then renamed in steps 4 and 5.

files have $(LOCAL_PATH) in front of them. if you want to compile C:<project>\jni\src\main.cpp, you want to have
LOCAL_SRC_FILES := $(LOCAL_PATH)/main.cpp
in my case, i have a folder under /jni/src so my main is C:<project>\jni\src\Source\main.cpp and this file gets added automatically by the lines
SOURCE_DIRS := Source
SOURCE_FILES := $(foreach dir,$(SOURCE_DIRS),$(subst $(LOCAL_PATH)/,$(wildcard $(LOCAL_PATH)/$(dir)/*.cpp)))
LOCAL_SRC_FILES := $(SOURCE_FILES)

also helpful is the line
$(warning $(SOURCE_FILES))
which prints out exactly what you are sending to the compiler.
in your case you would want to put
$(warning $(LOCAL_SRC_FILES))
near the bottom of the file.

Code:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := main

SDL_PATH := …/SDL

SOURCE_DIRS := Source Source/App Source/Platform Source/Environment Source/Environment/Expression Source/GUI Source/Dash Source/Platform/SDL2_0 Source/Platform/Android Source/Dash Source/Platform/SDL2_0/OpenGLES20
SOURCE_FILES := $(foreach dir,$(SOURCE_DIRS),$(subst $(LOCAL_PATH)/,$(wildcard $(LOCAL_PATH)/$(dir)/*.cpp)))
$(warning “-------------------------”)
$(warning “target files:”)
$(warning $(SOURCE_FILES))
$(warning “-------------------------”)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include

Add your application source files here…

LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c
$(SOURCE_FILES)

LOCAL_SHARED_LIBRARIES := SDL2

LOCAL_CFLAGS := -Wno-format-security -fexceptions -I/usr/local/include/boost_1_49_0/

LOCAL_LDLIBS := -llog -lGLESv2

include $(BUILD_SHARED_LIBRARY)

if you still have errors, paste the exact text and give more info about your compiler.

from one noob to another: HTH :slight_smile:

Ok got it working

Thanks a million guys - much appreciated!!!

:slight_smile: :slight_smile: :slight_smile:

thanks Carl - i just saw your post now.

I will give your method a shot as well and see how I get on.

much appreciated!!

:slight_smile: :slight_smile: :slight_smile:

ok great Carl, that worked like magic.

no errors whatsoever during the build, and now i have loads of object files scattered about.

I am guessing my next step is to use Eclipse to actually build the apk app and try run it on an emulator?

:slight_smile:

Yup, build the app with ‘ant debug’ and test it [ prefer an actual device over an emulator ]On Sep 27, 2013, at 7:09 PM, “djkarstenv” wrote:

ok great Carl, that worked like magic.

no errors whatsoever during the build, and now i have loads of object files scattered about.

I am guessing my next step is to use Eclipse to actually build the apk app and try run it on an emulator?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Ok awesome, will give that a go.

Thanks a lot guys, you’ve been a great help so far.

:slight_smile: :slight_smile:

You might be interested in my blogpost and the related example on github.

http://mgerhardy.blogspot.de/2013/10/sdl2-and-android.htmlAm 27.09.2013 10:28, schrieb djkarstenv:

Hi All,

I have been struggling for a while trying to get a simple SDL app to
work on Android. While there are many tutorials out there, they don’t
really explain things that a beginner like me can understand. Although
I am fluent in C++ and SDL, when it comes to porting I am a beginner
and really need some help.

So far I have

  1. Downloaded the SDL 2 source files and stored them on my hard drive
    in a folder
  2. I have the Android SDK and NDK in seperate folders
  3. I know I have to build the SDL source files using the NDK, and also
    eventually build all my game files with the NDK.
  4. I have added the path of the NDK folder to my PATH, so I can run
    the ndk command from anywhere in the cmd window.
  5. I am aware that SDL provides a “android-project” folder, where I
    need to place everything in the “jni/src” folder
  6. Now this is where I get stuck - do I place all the SDL2 source
    files one by one in the jni folder or just my game files. How do I
    build, because when I run “ndk-build” I get all sorts of errors.

Would somebody be kind enough to give me a full explanation of how
this works. I am even willing to skype with someone so I can get a
better understanding of it all.

Many many thanks guys

Smile


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks Martin, will check it out :slight_smile: