This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields and exactly what they do.
Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file.
Press ^C at any time to quit. name: (ReactNativeDemo) reactnativedemo version: (1.0.0) 1.0.0 description: react native app demo entry point: (index.js) index.android.js test command: git repository: keywords: author: hq license: (ISC) About to write to /home/heqiang/react-native-workspace/ReactNativeDemo/package.json:
/** * Returns the name of the main component registered from JavaScript. * This is used to schedule rendering of the component. */ @Nullable @Override protected String getMainComponentName() { return"HelloWorld"; //这个是在AppRegistry.registerComponent里面注册的。 } }
但这个方法需要我们自定义一个Application否则 运行时会报错:
1
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.hq.reactnativedemo/com.android.hq.reactnativedemo.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.facebook.react.ReactApplication
@Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; } }
还要记得修改AndroidManifest.xml。
运行
根目录运行下面命令:
1 2
adb reverse tcp:8081 tcp:8081 npm start
启动server。 然后点击Android Studio的运行按钮: 如果运行时报下面的错误:
1
02-22 19:18:59.050 24413 24413 E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.android.hq.reactnativedemo/lib-main/libgnustl_shared.so" is 32-bit instead of 64-bit