关于javascript:RN项目调试过程中遇到的坑

57次阅读

共计 4121 个字符,预计需要花费 11 分钟才能阅读完成。

一、开发过程中遇到短少 .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.

报上方的错是对应的库版本太老了,库的公布形式和以后环境对应不上了,把对应的库降级一下就好了。

正文完
 0