美文网首页
RN项目调试过程中遇到的坑

RN项目调试过程中遇到的坑

作者: 无神 | 来源:发表于2023-07-12 09:23 被阅读0次

    一、开发过程中遇到缺少 *.jsbundle或者 *.jsbundle没有及时更新的情况

    解决方法:

    1. ios端:yarn react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios

    2. android端:yarn react-native bundle --entry-file index.js --platform android --dev false --bundle-output ./android/app/src/main/assets/index.android.bundle --assets-dest ./android/app/src/main/res/

    温馨提示,每次发布release包之前,最好是使用上述命令生成对应平台的最新bundle包。以保证发布的包是最新的。

    二、开发过程中遇到依赖加载重复

    报错信息如下:

    error React Native CLI uses autolinking for native dependencies, but the following modules are linked manually:

    • react-native-doc-viewer (to unlink run: "react-native unlink react-native-doc-viewer")
    • react-native-document-picker (to unlink run: "react-native unlink react-native-document-picker")
    • react-native-fs (to unlink run: "react-native unlink react-native-fs")

    解决方法:

    在项目的根目录中,添加一个 react-native.config.js。文件中如下配置:

    // react-native.config.js
    module.exports = {
      dependencies: {
        'react-native-doc-viewer': {
          platforms: {
            // android: null,
            ios: null,
            // add more platform to disable auto-linking for them too
          },
        },
        'react-native-document-picker': {
          platforms: {
            // android: null,
            ios: null,
            // add more platform to disable auto-linking for them too
          },
        },
        'react-native-fs': {
          platforms: {
            // android: null,
            ios: null,
            //ios: null,
            // add more platform to disable auto-linking for them too
          },
        },
      }
    }
    

    此处只作为参考,具体需要根据您重复的依赖进行对应的配置。

    三、启动ios模拟器时,指定自己需要的模拟器

    RN 启动iOS端时指定模拟器:yarn ios --simulator 'iPhone 14 Pro'

    如上所示就是在 yarn ios 后跟上参数--simulator 和对应模拟器的名字。

    通用格式:yarn ios --simulator 'iPhone XXX'

    四、TypeError: undefined is not an object (evaluating '_reactNative.TabBarIOS.Item')

    解决方法:

    进入到项目的 node_modules > antd-mobile-rn > lib > tab-bar > tabbar.ios.js 文件,注释掉文件中的

    // TabBar.Item = _reactNative.TabBarIOS.Item; 这一行就OK啦。

    五、react-native-webview报错

    报错信息:Invariant Violation: WebView has been removed from React Native. It can now be installed and imported from 'react-native-webview' instead of 'react-native'. See https://github.com/react-native-community/react-native-webview

    解决方式:
    https://stackoverflow.com/questions/60740507/invariant-violation-webview-has-been-removed-from-react-native

    此处其实就是 webview的引入方式发生了改变了,之前是从 react-native 中引入的,现在变成从 react-native-webview中引入了。新的引入方式:

    import { WebView } from 'react-native-webview'
    

    六、react-native-baidu-map安装时报错

    1. http://repo.lovebing.net/repository/maven-public/ 找不到的问题。此处是因为android的安全机制升级,默认不允许访问http的链接了,如果要使http链接能够继续访问,需要打开非安全访问(allowInsecureProtocol)的开关。如下所示:

      maven() {
                  allowInsecureProtocol true
                  url 'http://repo.lovebing.net/repository/maven-public/'
              }
      
    2. 报错信息:

      What went wrong:
      A problem occurred configuring project ':react-native-baidu-map'.

      Could not resolve all files for configuration ':react-native-baidu-map:classpath'.
      Could not find aapt2-proto-0.3.1.jar (com.android.tools.build:aapt2-proto:0.3.1).
      Searched in the following locations:
      https://maven.aliyun.com/repository/gradle-plugin/com/android/tools/build/aapt2-proto/0.3.1/aapt2-proto-0.3.1.jar

      此处是因为 com.android.tools.build:gradle的版本不一致,将其换成对应的版本就可以解决了。(此处报错信息已经提示0.3.1)

      dependencies {
              classpath 'com.android.tools.build:gradle:3.1.0'
          }
      

    七、android的依赖加载的时候,默认的资源链接可能需要翻墙,建议换成国内的

    系统默认的:
    
    google()
    jcenter()
    

    建议换成国内阿里的:

    maven { url 'https://maven.aliyun.com/repository/google' }
    maven { url 'https://maven.aliyun.com/repository/jcenter' }
    

    八、打包发布之前的检测命令

    android 打release包之前的检测命令:

    npx react-native run-android --variant=release

    九、安装包的时候报错

    报错信息:

    java.util.concurrent.ExecutionException: com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException: INSTALL_FAILED_UPDATE_INCOMPATIBLE: Existing package XXX signatures do not match newer version; ignoring!

    看报错信息,是签名不匹配,已经存在一个包,删除旧的安装包,重新安装就好了。

    十、android项目运行时报错

    报错信息:

    Check the render method of AnimatedComponent

    进入到 node_modules > react-native-scrollable-tab-view 下,把const ViewPager = require('@react-native-community/viewpager');的引入方式修改为:

    import ViewPager from '@react-native-community/viewpager'
    

    上方的报错就可以解决了。

    十一、Could not find method mavenDeployer() for arguments

    报错信息:

    Could not determine the dependencies of task ':app:mergeDebugAssets'.

    Could not resolve all dependencies for configuration ':app:debugRuntimeClasspath'.
    A problem occurred configuring project ':react-native-screens'.
    > Could not find method mavenDeployer() for arguments [build_60fl66f2yqce3foadlpzk3c4_run_closure4_closure17$_closure19@15a2077d] on repository container of type org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.

    报上方的错是对应的库版本太老了,库的发布方式和当前环境对应不上了,把对应的库升级一下就好了。

    相关文章

      网友评论

          本文标题:RN项目调试过程中遇到的坑

          本文链接:https://www.haomeiwen.com/subject/hnzuudtx.html