1. September 2017

SDL2 for Android API level 19

SDL2 example application works without problem with newer Androids API 21+. The problem was with API level 19. It took me some time to figure out reasons for very interesting errors.

Let’s examine error messages and investigate how to fix them.

When I’ve started sdl2-android-example on a device with API 19, it failed with error message: dlopen failed: cannot locate symbol “signal” referenced by libSDL2.so”

The root cause is not that obvious from the error message. It failed because the application has been built for API 21 instead of API 19.

The fixture is easy, just set sdkVersion to 19.

For this purpose, I’ve extracted sdkVersion to settings.gradle which can be found in the root directory of the project.

gradle.ext.sdkVersion = 19

Each module then has reference to this value:

model {
    android {
        compileSdkVersion = gradle.sdkVersion
...

Make complete clean and distributeLib to rebuild for proper API.

gradle clean dL

Hooray! Fixed. Let’s start the application.

Yey. New error: dlopen failed: could not load library “libSDL2_image.so” needed by “libmain.so”; caused by cannot locate symbol “png_set_longjmp_fn” referenced by “libSDL2_image.so”

The root cause of this issue is similar to the problem with JPEG described in the previous article. Paul Asiimwe found out that there is another library the name png and it has a different version so the application crashes.

To fix this issue it was necessary to rename whole png submodule to SDL2_png. Renaming requires clean and new distributeLib:

gradle clean dL

After these small tricks, the sdl2-android-example works even with API level 19.

31. August 2017

SDL2_image for Android with JPEG image format

In the previous article, we were discussing how to add support for PNG image format to SDL2 application for Android. Let’s add another more common format. What about JPEG?

The first step is to enable JPEG in SDL2_image. Just add a proper definition to SDL2_image/build.gradle.

ndk {
...
CFlags.addAll([
    "-DLOAD_JPG"
    "-DLOAD_PNG",
    "-DLOAD_XPM"
])}

Then it is necessary to add C implementation of JPEG library. There is one big catch. Do not call it jpeg. The problem is that the name will collide with another library in Android system and function jpeg_create_decompress(&cinfo) will crash. Thanks to DevMultiTech for a hint about the solution.

Let’s call the jpeg library SDL2_jpeg.

Register modules in settings.gradle:

include ':SDL2_jpeg'

Build all libraries:

gradle distributeLib

Here is a sample code for loading and displaying JPEG image:

SDL_Surface *backgroundSurface = IMG_Load("brno-snow.jpg")
SDL_Texture *backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
SDL_RenderCopy(renderer, backgroundTexture, NULL, &dstrect);

Result looks like this:

You can find whole source code at https://github.com/georgik/sdl2-android-example.

You can find about SDL2 and libraries more under the topic SDL2.

16. July 2017

Android Studio: Failure INSTALL_FAILED_TEST_ONLY

Starting application from Android Studio may fail with the mysterious message: Installation failed with message INSTALL_FAILED_TEST_ONLY.

The IDE will prompt you whether you want to uninstall the existing application.

Even this uninstallation may fail. When you open Run console, you can see something like this:

$ adb shell pm uninstall rocks.georgik.sdlapp
$ adb shell pm install -r "/data/local/tmp/rocks.georgik.sdlapp"
	pkg: /data/local/tmp/rocks.georgik.sdlapp
Failure [INSTALL_FAILED_TEST_ONLY]

$ adb shell pm uninstall rocks.georgik.sdlapp
DELETE_FAILED_INTERNAL_ERROR
Error while Installing APK

There is a relatively simple fix to this issue. Click drop-down menu with your configuration and choose Edit Configurations…

Select tab General and add -t to Install Flags field. Click Ok.

Now start the application again and it should work.

If this article didn’t help you to resolve your issue with Android Studio please let me know in comments below. There might be also other reasons for the error and we can discuss them.

9. July 2017

SDL2_image for Android with PNG image format

Update of the article #1: If you’d like to support API level 19, the library must have a different name than png. Therefore I changed names in the article to SDL2_png. Read more about SDL2 API level 19 support.

Update of the article #2: Original article contained a dependency on the custom version of zip lib. It is not necessary because it is sufficient to link libz from Android. The library was removed from the example.

In the previous article, we were discussing how to add support for XPM image format to SDL2 application for Android. Let’s add another more common format. What about Portable Network Graphic (a.k.a. PNG)?

The first step is to enable PNG in SDL2_image. Just add a proper definition to SDL2_image/build.gradle.

ndk {
...
CFlags.addAll([
    "-DLOAD_PNG",
    "-DLOAD_XPM"
])}

Unfortunately, it’s not that simple. PNG support has a dependency on PNG library with a patch and PNG library. To get PNG working you need to add these one C library into the project. The approach is the same like when adding SDL2_image. The library is stored as a separate module in the directory with the same name like module name.

Register modules in settings.gradle:

include ':SDL2_image'
include ':SDL2_png'

Add build.gradle: SDL2_png/build.gradle.

You can verify compilation of the whole suite by following commands:

gradle :SDL2_png:distributeLib
gradle :SDL2_image:distributeLib

or simply:

gradle distributeLib

The library is ready. Now it’s necessary to add PNG file to the project. The location for graphic assets is app/src/main/assets.

Here is a sample code for loading and displaying PNG image:

SDL_Surface *loadedSurface = IMG_Load("smiley.png");
SDL_Texture *smileyTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
SDL_RenderCopy(renderer, smileyTexture, NULL, NULL);

Result looks like this:

As you can see the PNG version of smiley has support for background transparency.

You can find the whole source code at https://github.com/georgik/sdl2-android-example.

We will discuss how to use further SDL2 libraries in next articles. You can find more under topic SDL2.

8. July 2017

SDL2_image for Android with XPM image format

In the previous article, we were discussing how to start building SDL2 application for Android. Let’s add some images.

SDL2 library does not contain support for any image formats, you have to add further library SDL2_image which has support for several formats.

In order to add SDL2_image into Android project it is necessary to add a new module to settings.gradle:

include ':SDL2_image'

The module itself will be stored in SDL2_image directory with build.gradle which contains reference to main SDL2 library:

model {
    repositories {
        libs(PrebuiltLibraries) {
            SDL2 {
                headers.srcDir "../SDL2/include"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("${lib_distribution_root}/SDL2/lib/${targetPlatform.getName()}/libSDL2.so")
                }
            }
        }
    }

SDL2_image has support for several formats. You need to turn them on based on your requirements. It’s easy, just add proper define to compiler. Let’s start with simplest format XPM:

ndk { 
...
   CFlags.addAll(["-DLOAD_XPM"])
...
}

X PixMap (or XPM) is very simple image format which could be embedded directly into the source code.

static char * icon_xpm[] = {
        "32 23 3 1",
        "     c #FFFFFF",
        ".    c #000000",
        "+    c #FFFF00",
        "                                ",
        "            ........            ",
        "          ..++++++++..          ",
        "         .++++++++++++.         ",
        "        .++++++++++++++.        ",
        "       .++++++++++++++++.       ",
        "      .++++++++++++++++++.      ",
        "      .+++....++++....+++.      ",
        "     .++++.. .++++.. .++++.     ",
        "     .++++....++++....++++.     ",
        "     .++++++++++++++++++++.     ",
        "     .++++++++++++++++++++.     ",
        "     .+++++++++..+++++++++.     ",
        "     .+++++++++..+++++++++.     ",
        "     .++++++++++++++++++++.     ",
        "      .++++++++++++++++++.      ",
        "      .++...++++++++...++.      ",
        "       .++............++.       ",
        "        .++..........++.        ",
        "         .+++......+++.         ",
        "          ..++++++++..          ",
        "            ........            ",
        "                                "}

The next step is to transform XPM from the source code into memory structure which could be rendered on the screen.

SDL_Surface *surface;
surface = IMG_ReadXPMFromArray(icon_xpm);
texture = SDL_CreateTextureFromSurface(renderer, surface);

The last part is to call render function. You can find whole source code at https://github.com/georgik/sdl2-android-example.

We will discuss how to use further SDL2 libraries in next articles. You can find more under topic SDL2.