Using your own app name in a SDL Android app

Nov
2011
10

Android, Programming

No comments

Update (Oct 9th, 2012): The third message in this thread explains a simpler alternative to what’s explained below. I haven’t tried it myself but it seems cleaner and easier to implement, so you should probably try that one first.

Update 2 (Feb 21st, 2013): I’m now using the simplified method mentioned above in my engine. I copy the org.libsdl.app.SDLActivity into my project and inherit from it from a class with the package and class name that I want to use in my app.

The only change I had to do on the standard SDLActivity.java is to modify the static section of this file to load the rest of the remaining libraries I require, doing this in your inherited class doesn’t quite work depending on how each library depends on the others.

The method stated below is still useful if your app does something that the default SDLActivity can’t really handle, such as a live wallpaper.

The Android SDL backend works great, it comes with a full skeleton project that you can use to get up to speed quickly. There is however one minor problem, everything in said project is hardwired to use the “org.libsdl.app” app name, which is all fine and dandy until you want to make a real world app with your own name…and keep using the SDL source with as little modifications as possible to benefit from the latest and greatest modifications provided by the open source community.

So, it’s time to take out the glue gun and put it to good use. What I do is deploy a little piece of code that I call jni_glue.cpp and that goes into jni/src/jni_glue.cpp. In my case the app name is “com.mdqinc.dp”…

#include "jni.h"
#include "SDL_config.h"
#include "SDL_stdinc.h"

//static float fLastAccelerometer[3];
extern "C" {
    void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(JNIEnv* env, jclass cls);
    void Java_org_libsdl_app_SDLActivity_onNativeResize(
                                    JNIEnv* env, jclass jcls,
                                    jint width, jint height, jint format);
    void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
                                    JNIEnv* env, jclass jcls, jint keycode);
    void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
                                    JNIEnv* env, jclass jcls, jint keycode);
    void Java_org_libsdl_app_SDLActivity_onNativeTouch(
                                    JNIEnv* env, jclass jcls,
                                    jint touch_device_id_in, jint pointer_finger_id_in,
                                    jint action, jfloat x, jfloat y, jfloat p);
    void Java_org_libsdl_app_SDLActivity_onNativeAccel(
                                    JNIEnv* env, jclass jcls,
                                    jfloat x, jfloat y, jfloat z);
    void Java_org_libsdl_app_SDLActivity_nativeQuit(
                                    JNIEnv* env, jclass cls);
    void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
                                    JNIEnv* env, jclass cls);
    void Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject obj);
};

// Resize
extern "C" void Java_com_mdqinc_dp_SDLActivity_onNativeResize(
                                    JNIEnv* env, jclass jcls,
                                    jint width, jint height, jint format)
{
    Java_org_libsdl_app_SDLActivity_onNativeResize(env, jcls, width, height, format);
}

// Keydown
extern "C" void Java_com_mdqinc_dp_SDLActivity_onNativeKeyDown(
                                    JNIEnv* env, jclass jcls, jint keycode)
{
    Java_org_libsdl_app_SDLActivity_onNativeKeyDown(env, jcls, keycode);
}

// Keyup
extern "C" void Java_com_mdqinc_dp_SDLActivity_onNativeKeyUp(
                                    JNIEnv* env, jclass jcls, jint keycode)
{
    Java_org_libsdl_app_SDLActivity_onNativeKeyUp(env, jcls, keycode);
}

// Touch
extern "C" void Java_com_mdqinc_dp_SDLActivity_onNativeTouch(
                                    JNIEnv* env, jclass jcls,
                                    jint touch_device_id_in, jint pointer_finger_id_in,
                                    jint action, jfloat x, jfloat y, jfloat p)
{
    Java_org_libsdl_app_SDLActivity_onNativeTouch(env, jcls, touch_device_id_in, pointer_finger_id_in, action, x, y, p);
}

// Accelerometer
extern "C" void Java_com_mdqinc_dp_SDLActivity_onNativeAccel(
                                    JNIEnv* env, jclass jcls,
                                    jfloat x, jfloat y, jfloat z)
{
     Java_org_libsdl_app_SDLActivity_onNativeAccel(env, jcls, x, y, z);
}

// Quit
extern "C" void Java_com_mdqinc_dp_SDLActivity_nativeQuit(
                                    JNIEnv* env, jclass cls)
{
    Java_org_libsdl_app_SDLActivity_nativeQuit(env, cls);
}

extern "C" void Java_com_mdqinc_dp_SDLActivity_nativeRunAudioThread(
                                    JNIEnv* env, jclass cls)
{
    Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(env, cls);
}

extern "C" void Java_com_mdqinc_dp_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject obj) {
    Java_org_libsdl_app_SDLActivity_nativeInit(env, cls, obj);
}

This glue code should be paired with a Java activity file based on the one SDL ships, but you need to modify the line that says “package org.libsdl.app;” for a name that suits your needs (and that matches the glue code function names posted above). The same modification needs to be applied to the AndroidManifest.xml.

Besides these changes you need to take care of a final minor adjustment, you have to place your activity file in the right location. It will no longer go in src/org/libsdl/app/SDLActivity.java but rather (for example, in my case) in src/org/mdqinc/dp/SDLActivity.java.

You can certainly achieve the same result by modifying the SDL source code directly, but you’ll find that updating it later is a hassle. This approach I’m describing stands separate from the actual SDL code, so you can update it without worries. The only thing you may want to keep an eye on is changes in the SDLActivity.java file, but in case that’s updated you only need to change a single line with the package name at the top.

Leave a Reply