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