1 package org.libsdl.app;
3 import android.content.Context;
5 import java.lang.reflect.*;
8 SDL library initialization
12 // This function should be called first and sets up the native code
13 // so it can call into the Java classes
14 public static void setupJNI() {
15 SDLActivity.nativeSetupJNI();
16 SDLAudioManager.nativeSetupJNI();
17 SDLControllerManager.nativeSetupJNI();
20 // This function should be called each time the activity is started
21 public static void initialize() {
24 SDLActivity.initialize();
25 SDLAudioManager.initialize();
26 SDLControllerManager.initialize();
29 // This function stores the current activity (SDL or not)
30 public static void setContext(Context context) {
34 public static Context getContext() {
38 public static void loadLibrary(String libraryName) throws UnsatisfiedLinkError, SecurityException, NullPointerException {
40 if (libraryName == null) {
41 throw new NullPointerException("No library name provided.");
45 // Let's see if we have ReLinker available in the project. This is necessary for
46 // some projects that have huge numbers of local libraries bundled, and thus may
47 // trip a bug in Android's native library loader which ReLinker works around. (If
48 // loadLibrary works properly, ReLinker will simply use the normal Android method
51 // To use ReLinker, just add it as a dependency. For more information, see
52 // https://github.com/KeepSafe/ReLinker for ReLinker's repository.
54 Class relinkClass = mContext.getClassLoader().loadClass("com.getkeepsafe.relinker.ReLinker");
55 Class relinkListenerClass = mContext.getClassLoader().loadClass("com.getkeepsafe.relinker.ReLinker$LoadListener");
56 Class contextClass = mContext.getClassLoader().loadClass("android.content.Context");
57 Class stringClass = mContext.getClassLoader().loadClass("java.lang.String");
59 // Get a 'force' instance of the ReLinker, so we can ensure libraries are reinstalled if
60 // they've changed during updates.
61 Method forceMethod = relinkClass.getDeclaredMethod("force");
62 Object relinkInstance = forceMethod.invoke(null);
63 Class relinkInstanceClass = relinkInstance.getClass();
65 // Actually load the library!
66 Method loadMethod = relinkInstanceClass.getDeclaredMethod("loadLibrary", contextClass, stringClass, stringClass, relinkListenerClass);
67 loadMethod.invoke(relinkInstance, mContext, libraryName, null, null);
69 catch (final Throwable e) {
72 System.loadLibrary(libraryName);
74 catch (final UnsatisfiedLinkError ule) {
77 catch (final SecurityException se) {
83 protected static Context mContext;