Lazy Foo' Productions


Setting up SDL_image on Windows Android Studio 3.0.1

Last Updated: Jan 6th, 2018

1) First, download the SDL_image source on this page.
download src
Extract the source code so it's at C:\androidlib\SDL2_image-2.0.2.

2) Download the demo source/assets. Copy the directory inside to C:\androidprojects\SDL\app\src\main\assets\. Remember: if the application needs to load 53_extensions_and_changing_orientation/portrait.png it needs to be at C:\androidprojects\SDL\app\src\main\assets\53_extensions_and_changing_orientation\portrait.png when building.

3) Copy demo source to C:\androidprojects\SDL\app\src\main\jni\src\53_extensions_and_changing_orientation.cpp. Open the game make file at C:\androidprojects\SDL\app\src\main\jni\src\Android.mk and change local source files to include the new demo source file:
LOCAL_SRC_FILES := 53_extensions_and_changing_orientation.cpp

Open up Android Studio and try to build. You'll get an error: Error:(6, 10) fatal error: 'SDL_image.h' file not found

This error means that our game source file can't find SDL_image.h which makes sense since we haven't set it up yet.

4) SDL_image is just another shared object library that needs to be built along SDL 2 and our C++ application. So that means we need to create a symbolic link to the SDL_image source code. Go to the start menu and run cmd as administrator:
run cmd as admin

Go to the JNI directory inside the project using this command: cd C:\androidprojects\SDL\app\src\main\jni

And create a hard symbolic link directory to the SDL2_image source directory we extracted (REMEMBER: This path will vary depending on your version of SDL_image):
mklink /D SDL2_image C:\androidlib\SDL2_image-2.0.2

You should get the following message back: symbolic link created for SDL2_image <<===>> C:\androidlib\SDL2_image-2.0.2

Build again, you'll get the same error but at least SDL2_image has a symbolic link in the project now.

5) There may be a symbolic link to SDL_image in the JNI folder but our demo application doesn't know where it is. To fix this error open up C:\androidprojects\SDL\app\src\main\jni\src\Android.mk and change:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
to
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include $(LOCAL_PATH)/../SDL2_image/

So now our demo application can find the SDL_image headers. Build again and you'll get a new error:

Error:(120) undefined reference to `IMG_Load'
Error:(432) undefined reference to `IMG_Init'
Error:(477) undefined reference to `IMG_Quit'

6) Those last errors were linker errors. While SDL_image managed to compile and our game managed to compile, we didn't tell the NDK to link our game against SDL_image. In C:\androidprojects\SDL\app\src\main\jni\src\Android.mk change
LOCAL_SHARED_LIBRARIES := SDL2
to
LOCAL_SHARED_LIBRARIES := SDL2 SDL2_image.

Build again. Hopefully you should get the application to build, but you may get an error.

7) You may have gotten a No rule to make target error for IMG_WIC.c. This is the webp library being a pain in the butt. We're just going to yank it out. Open up C:\androidlib\SDL2_image-2.0.2\Android.mk and change: SUPPORT_WEBP ?= true to SUPPORT_WEBP ?= false.

Delete the line that says IMG_WIC.c \ and just straight up delete the C:\androidlib\SDL2_image-2.0.2\external\libwebp-0.6.0 directory.

Build again and you should get no errors.

8) The application should now build but IT WILL NOT WORK. We need to get our Java activity load the SDL extension library. Open C:\androidprojects\SDL\app\src\main\java\org\libsdl\app\SDLActivity.java and look for this section:

    protected String[] getLibraries() {
        return new String[] {
            "SDL2",
            // "SDL2_image",
            // "SDL2_mixer",
            // "SDL2_net",
            // "SDL2_ttf",
            "main"
        };
    }

Uncomment the libraries you'll be using so they'll be loaded.

9) Build and run. The application should run and rotate. Now that the application built, it's time to go over the source code.