|
| 1 | +package com.reactnativeglobalkeyevent; |
| 2 | + |
| 3 | +import android.app.Application; |
| 4 | +import android.content.Context; |
| 5 | +import com.facebook.react.PackageList; |
| 6 | +import com.facebook.react.ReactApplication; |
| 7 | +import com.facebook.react.ReactInstanceManager; |
| 8 | +import com.facebook.react.ReactNativeHost; |
| 9 | +import com.facebook.react.ReactPackage; |
| 10 | +import com.facebook.soloader.SoLoader; |
| 11 | +import java.lang.reflect.InvocationTargetException; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class MainApplication extends Application implements ReactApplication { |
| 15 | + |
| 16 | + private final ReactNativeHost mReactNativeHost = |
| 17 | + new ReactNativeHost(this) { |
| 18 | + @Override |
| 19 | + public boolean getUseDeveloperSupport() { |
| 20 | + return BuildConfig.DEBUG; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + protected List<ReactPackage> getPackages() { |
| 25 | + @SuppressWarnings("UnnecessaryLocalVariable") |
| 26 | + List<ReactPackage> packages = new PackageList(this).getPackages(); |
| 27 | + // Packages that cannot be autolinked yet can be added manually here, for example: |
| 28 | + // packages.add(new MyReactNativePackage()); |
| 29 | + return packages; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + protected String getJSMainModuleName() { |
| 34 | + return "index"; |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + @Override |
| 39 | + public ReactNativeHost getReactNativeHost() { |
| 40 | + return mReactNativeHost; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onCreate() { |
| 45 | + super.onCreate(); |
| 46 | + SoLoader.init(this, /* native exopackage */ false); |
| 47 | + initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Loads Flipper in React Native templates. Call this in the onCreate method with something like |
| 52 | + * initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); |
| 53 | + * |
| 54 | + * @param context |
| 55 | + * @param reactInstanceManager |
| 56 | + */ |
| 57 | + private static void initializeFlipper( |
| 58 | + Context context, ReactInstanceManager reactInstanceManager) { |
| 59 | + if (BuildConfig.DEBUG) { |
| 60 | + try { |
| 61 | + /* |
| 62 | + We use reflection here to pick up the class that initializes Flipper, |
| 63 | + since Flipper library is not available in release mode |
| 64 | + */ |
| 65 | + Class<?> aClass = Class.forName("com.reactnativeglobalkeyevent.ReactNativeFlipper"); |
| 66 | + aClass |
| 67 | + .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class) |
| 68 | + .invoke(null, context, reactInstanceManager); |
| 69 | + } catch (ClassNotFoundException e) { |
| 70 | + e.printStackTrace(); |
| 71 | + } catch (NoSuchMethodException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } catch (IllegalAccessException e) { |
| 74 | + e.printStackTrace(); |
| 75 | + } catch (InvocationTargetException e) { |
| 76 | + e.printStackTrace(); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments