Home > OS >  What is MainApplication.java for in React Native?
What is MainApplication.java for in React Native?

Time:12-25

I was looking through my React Native project and noticed a file android/app/src/main/java/com/cheersfe/MainApplication.java that seems to have a lot of imports in it with the same name as some of the npm libraries I installed that are visual add-ons such as linear gradient. I'm just curious what is MainApplication.java for in React Native? The main conclusions I can draw about it are

public class MainApplication extends Application implements ReactApplication {
  private final ReactNativeHost mReactNativeHost =
      new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }
        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
          return packages;
        }
        @Override
        protected String getJSMainModuleName() {
          return "index";
        }
      };
  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }
  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  }
  /**
   * Loads Flipper in React Native templates. Call this in the onCreate method with something like
   * initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
   *
   * @param context
   * @param reactInstanceManager
   */
  private static void initializeFlipper(
      Context context, ReactInstanceManager reactInstanceManager) {
    if (BuildConfig.DEBUG) {
      try {
        /*
         We use reflection here to pick up the class that initializes Flipper,
        since Flipper library is not available in release mode
        */
        Class<?> aClass = Class.forName("com.cheersfe.ReactNativeFlipper");
        aClass
            .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
            .invoke(null, context, reactInstanceManager);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
}
  • It's specifically for when the OS is Android
  • It has these Java override functions I'm not familiar with or what they do in React Native

CodePudding user response:

I have coded with the Android SDK in native Java few months ago. So for me the purpose of MainApplication.java is to serve Android Native Modules. And that the class is declaring how the app works and how it is composed.

CodePudding user response:

This is an entry point of your android application. Whenever you open react native application on android 1st it goes to MainApplication.java then here javascript bundle is called.

  • Related