• android原生集成Rn项目


    一、新工程集成步骤

    1、新建目录RnDemo

    2、在RnDemo里创建android目录

    3、将现有的android项目代码拷贝进android目录里

    4、创建package.json文件,内容如下:

    1. {
    2. "name": "MyReactNativeApp",
    3. "version": "0.0.1",
    4. "private": true,
    5. "scripts": {
    6. "start": "yarn react-native start"
    7. }
    8. }

    5、添加react-native,执行如下命令

    yarn add react-native
    

     命令执行完毕后,目录下自动创建node-module目录,里面的内容如下:

     

     

     

     执行命令过程中,有如下提示:

    warning " > react-native@0.70.1" has unmet peer dependency "react@18.1.0".

    6、添加react

    yarn add react@18.1.0

     注意必须严格匹配警告信息中所列出的版本,高了或者低了都不可以。

    7、配置原生

    在app的build.gradle文件中添加React Native和JSC引擎依赖

    1. implementation "com.facebook.react:react-native:+" // From node_modules
    2. implementation "org.webkit:android-jsc:+"

    在项目的 build.gradle 文件中为 React Native 和 JSC 引擎添加 maven 源的路径,必须写在 "allprojects" 代码块中

    1. allprojects {
    2. repositories {
    3. maven {
    4. // All of React Native (JS, Android binaries) is installed from npm
    5. url "$rootDir/../node_modules/react-native/android"
    6. }
    7. maven {
    8. // Android JSC is installed from npm
    9. url("$rootDir/../node_modules/jsc-android/dist")
    10. }
    11. ...
    12. }
    13. ...
    14. }

    8、启动原生模块的自动连接

     要使用自动链接的功能,我们必须将其应用于几个地方。首先,将以下内容添加到settings.gradle:

    apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)

    接下来,在app/build.gradle的最底部添加以下内容:

    apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

    9、在项目中创建一个index.js文件,内容如下:

    1. import React from 'react';
    2. import {
    3. AppRegistry,
    4. StyleSheet,
    5. Text,
    6. View
    7. } from 'react-native';
    8. class HelloWorld extends React.Component {
    9. render() {
    10. return (
    11. <View style={styles.container}>
    12. <Text style={styles.hello}>Hello, WorldText>
    13. View>
    14. );
    15. }
    16. }
    17. var styles = StyleSheet.create({
    18. container: {
    19. flex: 1,
    20. justifyContent: 'center'
    21. },
    22. hello: {
    23. fontSize: 20,
    24. textAlign: 'center',
    25. margin: 10
    26. }
    27. });
    28. AppRegistry.registerComponent(
    29. 'MyReactNativeApp',
    30. () => HelloWorld
    31. );

    10、在原生的activity中添加ReactRootView,用于加载rn的东西

    参考如下:

    1. public class MyReactActivity extends Activity implements DefaultHardwareBackBtnHandler {
    2. private ReactRootView mReactRootView;
    3. private ReactInstanceManager mReactInstanceManager;
    4. @Override
    5. protected void onCreate(Bundle savedInstanceState) {
    6. super.onCreate(savedInstanceState);
    7. SoLoader.init(this, false);
    8. mReactRootView = new ReactRootView(this);
    9. List<ReactPackage> packages = new PackageList(getApplication()).getPackages();
    10. // 有一些第三方可能不能自动链接,对于这些包我们可以用下面的方式手动添加进来:
    11. // packages.add(new MyReactNativePackage());
    12. // 同时需要手动把他们添加到`settings.gradle`和 `app/build.gradle`配置文件中。
    13. mReactInstanceManager = ReactInstanceManager.builder()
    14. .setApplication(getApplication())
    15. .setCurrentActivity(this)
    16. .setBundleAssetName("index.android.bundle")
    17. .setJSMainModulePath("index")
    18. .addPackages(packages)
    19. .setUseDeveloperSupport(BuildConfig.DEBUG)
    20. .setInitialLifecycleState(LifecycleState.RESUMED)
    21. .build();
    22. // 注意这里的MyReactNativeApp 必须对应"index.js"中的
    23. // "AppRegistry.registerComponent()"的第一个参数
    24. mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null);
    25. setContentView(mReactRootView);
    26. }
    27. @Override
    28. public void invokeDefaultOnBackPressed() {
    29. super.onBackPressed();
    30. }
    31. }

    把activity的主题设为如下:

    Theme.AppCompat.Light.NoActionBar

    注意:

    一个ReactInstanceManager可以在多个 activities 或 fragments 间共享。你将需要创建自己的ReactFragment或ReactActivity,并拥有一个保存ReactInstanceManager的单例持有者。当你需要ReactInstanceManager(例如,将ReactInstanceManager连接到这些 Activities 或 Fragments 的生命周期)时,请使用单例提供的那个。

    下一步我们需要把一些 activity 的生命周期回调传递给ReactInstanceManager

    1. @Override
    2. protected void onPause() {
    3. super.onPause();
    4. if (mReactInstanceManager != null) {
    5. mReactInstanceManager.onHostPause(this);
    6. }
    7. }
    8. @Override
    9. protected void onResume() {
    10. super.onResume();
    11. if (mReactInstanceManager != null) {
    12. mReactInstanceManager.onHostResume(this, this);
    13. }
    14. }
    15. @Override
    16. protected void onDestroy() {
    17. super.onDestroy();
    18. if (mReactInstanceManager != null) {
    19. mReactInstanceManager.onHostDestroy(this);
    20. }
    21. if (mReactRootView != null) {
    22. mReactRootView.unmountReactApplication();
    23. }
    24. }

    我们还需要把后退按钮事件传递给 React Native:

    1. @Override
    2. public void onBackPressed() {
    3. if (mReactInstanceManager != null) {
    4. mReactInstanceManager.onBackPressed();
    5. } else {
    6. super.onBackPressed();
    7. }
    8. }

    二、拆包集成参考文章

    https://gitee.com/chenbaoxiang/subpackage_rn

    React Native 启动流程简析 (bbsmax.com) 

  • 相关阅读:
    Vue3+Vite+ElementPlus管理系统常见问题
    【图像处理】使用各向异性滤波器和分割图像处理从MRI图像检测脑肿瘤(Matlab代码实现)
    基于SSM红色教育系统的设计与实现毕业设计源码211757
    数据加密标准DES安全性
    计算机毕业设计选什么题目好?springboot 个人健康信息管理系统
    STM32G030F6P6点灯闪烁
    Spark杂谈
    UTF-16究竟如何编码
    工程伦理--9.3 工程师的能力标准
    seata的AT模式分析
  • 原文地址:https://blog.csdn.net/zhizhuodewo6/article/details/127118640