一、在已有组件化项目工程中新建RN模块,并在RN模块根目录并创建一个package.json
包含以下内容:
{
"name": "MyReactNativeApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "yarn react-native start"
}
}
二、安装react
和react-native
软件包,打开终端或命令提示符,然后导航到包含package.json
文件的目录并运行:
yarn add react-native
yarn add react@version_printed_above
三、编写React Native代码,在您的React Native项目的根目录中创建一个index.js文件:
import React from 'react';
import {AppRegistry, StyleSheet, Text, View} from 'react-native';
class HelloWorld extends React.Component {
render() {
return (
Hello, World
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
hello: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
AppRegistry.registerComponent('MyReactNativeApp', () => HelloWorld);
四、在RN模块建立MyReactActivity呈现我们的JS组件:
/**
* 页面描述:
*
* @author gw
* @date 2019-10-14
*/
public class MyReactActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.bundle")
.setJSMainModulePath("index")
.addPackage(new MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null);
setContentView(mReactRootView);
}
@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}
@Override
protected void onPause() {
super.onPause();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostPause(this);
}
}
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostResume(this, this);
}
}
@Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
mReactInstanceManager.showDevOptionsDialog();
return true;
}
return super.onKeyUp(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostDestroy(this);
}
}
}
五、创建React Native捆绑包,在RN模块的src/main下新建assets文件件,并执行命令:
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/com/your-company-name/app-package-name/src/main/assets/index.bundle --assets-dest android/com/your-company-name/app-package-name/src/main/res/
六、在项目主目录的build.gradle中加入RN本地依赖路径:
allprojects {
repositories {
google()
jcenter()
maven {
// All of React Native (JS, Android binaries) is installed from npm
url "$rootDir/module_rn/node_modules/react-native/android"
}
}
}
七、在app工程及RN模块工程下的build.gradle中加入RN依赖:
implementation "com.facebook.react:react-native:0.59.10"
八、app工程中加入RN模块依赖:
if (!rootProject.ext.isModule) {
implementation project(':module_rn')
}
九、启动开发服务器,在React Native项目的根目录中运行以下命令:
react-native start
十、把app启动页设置为MyReactActivity,正常编译app工程,大功告成。