创建好一个 reac native 项目然后在 Android 端运行时究竟发生了什么, 首先我们来看下面的代码, 我们 MainActivity 继承 ReactActivity 这个类, 然后在 getMainComponentName 方法中会返回项目名称.
- public class MainActivity extends ReactActivity {
- /**
- * Returns the name of the main component registered from JavaScript.
- * This is used to schedule rendering of the component.
- */
- @Override
- protected String getMainComponentName() {
- return "sparrowAProject";
- }
- }
下面 Android 的 Application 文件 MainApplication 继承了 Application 并实现了 ReactApplication 这个接口, 同时创建了 ReactNativeHost 这类, 因为是抽象类所以我们需要实现一些方法关键是
getPackages 会返回我们应用所有加入 pack
getJSMainModuleName 会返回 react 的入口文件, 这是就是要加载 JavaScript 线程内的 JavaScript 文件.
这里在再解释一下 SoLoader 这是可以动态加载 os 库的 Facebook 提供的包
- 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() {
- return Arrays.<ReactPackage>asList(
- new MainReactPackage()
- );
- }
- @Override
- protected String getJSMainModuleName() {
- return "index";
- }
- };
- @Override
- public ReactNativeHost getReactNativeHost() {
- return mReactNativeHost;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- SoLoader.init(this, /* native exopackage */ false);
- }
- }
来源: http://www.jianshu.com/p/c10c98af9ead