React Native 错误集合

本文章专门用于记录在React Native 开发中所遇到的问题,以便于以后查阅。

  1. java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

    解决方案react-native项目名/android/app/build.gradle中找到defaultConfig闭包, 在里面添加multiDexEnabled true 代码。然后在dependencies中新增依赖implementation 'com.android.support:multidex:1.0.1'。然后命令行切换到react-native项目名/android目录下面,运行命令./gradlew clean,清理项目。再运行react-native run-android即可

  2. Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT error: check logs for details 错误

    解决方案react-native项目名/android/gradle.properties 中,新增 android.enableAapt2=false 。然后命令行切换到react-native项目名/android目录下面,运行命令./gradlew clean,清理项目。再运行react-native run-android即可

  3. 在React Native 0.56 版本初始化项目运行react-native run-android

    problem occurred configuring project ':app'.
    > Could not resolve all dependencies for configuration ':app:_debugApkCopy'.
       > Could not resolve com.android.support:appcompat-v7:26.1.0.
         Required by:
             project :app
             project :app > com.facebook.react:react-native:0.56.0
          > Could not resolve com.android.support:appcompat-v7:26.1.0.
             > Could not get resource 'https://maven.google.com/com/android/support/appcompat-v7/26.1.0/appcompat-v7-26.1.0.pom'.
                > Could not HEAD 'https://maven.google.com/com/android/support/appcompat-v7/26.1.0/appcompat-v7-26.1.0.pom'.
                   > Remote host closed connection during handshake
    复制代码

    解决方案:在 react-native 项目名/android/build.gradle中修改maven 的远程地址,默认用的是google 的,在中国被墙了,一般是下载不出来的。这里建议换成阿里的镜像服务器地址。然后再次运行就可以了。修改如下:

    buildscript {
        repositories {
            jcenter()
            maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } //在此处添加阿里maven镜像服务器
        }
        ...
    }
    allprojects {
        repositories {
            mavenLocal()
            jcenter()
            maven {
                url "$rootDir/../node_modules/react-native/android"
            }
            maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } //在此处添加阿里maven镜像服务器
        }
    }
    ...
    
    复制代码
  4. 在React Native 中,使用react-navigation的tab 导航+FlatList 组件很卡,导致其他tab点击出现卡死的情况。

    解决方案: 优化FlatListonEndReachedThreshold属性,设置为onEndReachedThreshold=0.1,如果是异步加载数据的话可以把分页的数据长度设置小点,我的项目里面设置的是5。即每次FlatList滑动到底部的时候,再去请求后台数据,请求的数据长度为5条记录

  5. 在android中,项目里面使用了code-push,需要打个一个测试包出来进行测试,也就是code-push 里面的staging,按照code-push的文档在android/app/build.gradle里面进行配置

    buildTypes {
            debug {
                buildConfigField "String", "CODEPUSH_KEY", '""'
            }
            release {
                minifyEnabled enableProguardInReleaseBuilds
                proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
                signingConfig signingConfigs.release
                 buildConfigField "String", "CODEPUSH_KEY", '"CODE-PUSH-KEY"'
            }
            releaseStaging {
                minifyEnabled enableProguardInReleaseBuilds
                proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
                signingConfig signingConfigs.release
                 buildConfigField "String", "CODEPUSH_KEY", '"WaU6FbgL8FnipCODE-PUSH-KEY"'
            }
        }
    复制代码

    然后运行gradlew assembleReleaseStaging报错。错误如下

    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Could not determine the dependencies of task ':app:lintVitalReleaseStaging'.
    > Could not resolve all task dependencies for configuration ':app:releaseStagingRuntimeClasspath'.
       > Could not resolve project :react-native-picker.
         Required by:
             project :app
          > Unable to find a matching configuration of project :react-native-picker:
              - Configuration 'debugApiElements':
                  - Required com.android.build.api.attributes.BuildTypeAttr 'releaseStaging' and found incompatible value 'debug'.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
                  - Required org.gradle.api.attributes.Usage 'java-runtime' and found incompatible value 'java-api'.
              - Configuration 'debugRuntimeElements':
                  - Required com.android.build.api.attributes.BuildTypeAttr 'releaseStaging' and found incompatible value 'debug'.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
                  - Required org.gradle.api.attributes.Usage 'java-runtime' and found compatible value 'java-runtime'.
              - Configuration 'releaseApiElements':
                  - Required com.android.build.api.attributes.BuildTypeAttr 'releaseStaging' and found incompatible value 'release'.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
                  - Required org.gradle.api.attributes.Usage 'java-runtime' and found incompatible value 'java-api'.
              - Configuration 'releaseRuntimeElements':
                  - Required com.android.build.api.attributes.BuildTypeAttr 'releaseStaging' and found incompatible value 'release'.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
                  - Required org.gradle.api.attributes.Usage 'java-runtime' and found compatible value 'java-runtime'.
    复制代码

    解决方案:修改android/app/build.gradle里配置为如下:

        buildTypes {
                debug {
                    buildConfigField "String", "CODEPUSH_KEY", '""'
                }
                release {
                    minifyEnabled enableProguardInReleaseBuilds
                    proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
                    signingConfig signingConfigs.release
                     buildConfigField "String", "CODEPUSH_KEY", '"CODE-PUSH-KEY"'
                }
                releaseStaging {
                    minifyEnabled enableProguardInReleaseBuilds
                    proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
                    signingConfig signingConfigs.release
                     buildConfigField "String", "CODEPUSH_KEY", '"WaU6FbgL8FnipCODE-PUSH-KEY"'
                     //指定匹配失败匹配其他变体
                    matchingFallbacks = ['debug', 'qa', 'release'] // 添加这段代码
                }
            }
    复制代码

    然后运行gradlew assembleReleaseStaging 即可打包成功

  6. 在android中使用gif 图片,按照中文网里面说明,在android/app/build.gradle中加入了一下代码,

        dependencies {
          // 如果你需要支持Android4.0(API level 14)之前的版本
          compile 'com.facebook.fresco:animated-base-support:1.9.0'
        
          // 如果你需要支持GIF动图
          compile 'com.facebook.fresco:animated-gif:1.9.0'
        
          // 如果你需要支持WebP格式,包括WebP动图
          compile 'com.facebook.fresco:animated-webp:1.9.0'
          compile 'com.facebook.fresco:webpsupport:1.9.0'
        
          // 如果只需要支持WebP格式而不需要动图
          compile 'com.facebook.fresco:webpsupport:1.9.0'
        }
    复制代码

    重新生成debug.apk闪退问题。

    解决方案:这个问题是当前RN 的内置依赖有问题,cmd 进入android目录下,运行一下命令gradlew -q app:dependencies查看当前RN 的com.facebook.fresco:fresco:1.3.0依赖版本是多少,我这儿是1.3.0

            +--- com.android.support:appcompat-v7:23.0.1
            |    \--- com.android.support:support-v4:23.0.1
            |         \--- com.android.support:support-annotations:23.0.1
            \--- com.facebook.react:react-native:+ -> 0.53.3
                 +--- javax.inject:javax.inject:1
                 +--- com.android.support:appcompat-v7:23.0.1 (*)
                 +--- com.facebook.fbui.textlayoutbuilder:textlayoutbuilder:1.0.0
                 |    \--- com.facebook.fbui.textlayoutbuilder:staticlayout-proxy:1.0
                 +--- com.facebook.fresco:fresco:1.3.0 // 这儿是你的 fresco依赖版本
                 |    +--- com.facebook.fresco:drawee:1.3.0
                 |    |    \--- com.facebook.fresco:fbcore:1.3.0
                 |    +--- com.facebook.fresco:fbcore:1.3.0
                 |    \--- com.facebook.fresco:imagepipeline:1.3.0
                 |         +--- com.parse.bolts:bolts-tasks:1.4.0
                 |         +--- com.facebook.fresco:fbcore:1.3.0
                 |         \--- com.facebook.fresco:imagepipeline-base:1.3.0
                 |              +--- com.parse.bolts:bolts-tasks:1.4.0
                 |              \--- com.facebook.fresco:fbcore:1.3.0
                 +--- com.facebook.fresco:imagepipeline-okhttp3:1.3.0
                 |    +--- com.squareup.okhttp3:okhttp:3.6.0
                 |    |    \--- com.squareup.okio:okio:1.11.0 -> 1.13.0
                 |    +--- com.facebook.fresco:fbcore:1.3.0
                 |    \--- com.facebook.fresco:imagepipeline:1.3.0 (*)
                 +--- com.facebook.soloader:soloader:0.1.0
                 +--- com.google.code.findbugs:jsr305:3.0.0
                 +--- com.squareup.okhttp3:okhttp:3.6.0 (*)
                 +--- com.squareup.okhttp3:okhttp-urlconnection:3.6.0
                 |    \--- com.squareup.okhttp3:okhttp:3.6.0 (*)
                 +--- com.squareup.okio:okio:1.13.0
                 \--- org.webkit:android-jsc:r174650
            
    复制代码

    然后将上面的依赖对应修改,修改完成之后如下:

        dependencies {
          // 如果你需要支持Android4.0(API level 14)之前的版本
          compile 'com.facebook.fresco:animated-base-support:1.3.0' // 修改版本1.9.0 为1.3.0
        
          // 如果你需要支持GIF动图
          compile 'com.facebook.fresco:animated-gif:1.3.0'// 修改版本1.9.0 为1.3.0
        
          // 如果你需要支持WebP格式,包括WebP动图
          compile 'com.facebook.fresco:animated-webp:1.3.0'// 修改版本1.9.0 为1.3.0
          compile 'com.facebook.fresco:webpsupport:1.3.0'// 修改版本1.9.0 为1.3.0
        
          // 如果只需要支持WebP格式而不需要动图
          compile 'com.facebook.fresco:webpsupport:1.3.0'// 修改版本1.9.0 为1.3.0
        }
    复制代码

    重新运行即可,就不会出现闪退了

  7. 在React Native中使用mobx 报错,错误如下

    Can't find variable: Symbol 
    复制代码

    解决方案:出现这个问题一般来说是mobx的版本太高了,降低一些mobx的版本即可。 在package.json中使用如下的版本即可

    {
        dependencies:{
            ...
             "mobx": "^4.3.0",
             "mobx-react": "^5.2.5",
            ...
        }
    }
    复制代码
  8. 在React Native中 'config.h' file not found

    'config.h' file not found
    复制代码

    解决方案

    cd node_modules/react-native/third-party/glog-0.3.4
     ../../scripts/ios-configure-glog.sh
    复制代码
  9. 在React Native中 运行 react-native run-android 报如下错误

    Could not create service of type GeneralCompileCaches using GradleScopeCompileServices.createGeneralCompileCaches().
    复制代码

    解决方案

     cd android 
     ./gradlew compileDebugJavaWithJavac --stacktrace
    复制代码
  10. 在React Native中真机调试模式下报跨域错误的问题!

    解决方案

    open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=/Users/zhangxueyan/data
    复制代码
  11. 在React Native中react-native-code-pushreact-native-amap3d 函数名称冲突问题.

    duplicate symbol _aes_encrypt_key128 in:
        /Users/berry/Documents/webstrom/BerryApp/ios/Pods/AMap3DMap/MAMapKit.framework/MAMapKit(MAMapKit-x86_64-master.o)
        /Users/berry/Library/Developer/Xcode/DerivedData/BerryAPP-arhfppjvlmwpaofkjfpfcpotishq/Build/Products/Debug-iphonesimulator/SSZipArchive/libSSZipArchive.a(aeskey.o)
    duplicate symbol _aes_decrypt_key128 in:
        /Users/berry/Documents/webstrom/BerryApp/ios/Pods/AMap3DMap/MAMapKit.framework/MAMapKit(MAMapKit-x86_64-master.o)
        /Users/berry/Library/Developer/Xcode/DerivedData/BerryAPP-arhfppjvlmwpaofkjfpfcpotishq/Build/Products/Debug-iphonesimulator/SSZipArchive/libSSZipArchive.a(aeskey.o)
    ld: 2 duplicate symbols for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    复制代码

    解决方案: 找到../node_modules/react-native-code-push/ios,在里面进行搜索aes_encrypt_key128aes_decrypt_key128这两个关键字。将其替换成为自己定义的函数名称,这儿我定义的为cp_aes_encrypt_key128cp_aes_decrypt_key128这个。如果用了这种方式来解决上面的重复定义,就不能通过pod来安装react-native-code-push了。应该使用手动的方式来安装react-native-code-push。我把我改的react-native-code-push放到了GitHub上面了。可以直接通过yarn add git+https://github.com/25juan/react-native-code-push.git来安装修改之后的版本 详情参见

  12. react native打正式包报错如下:

    Execution failed for task ':jmrtc-react-native:verifyReleaseResources'.
        > java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed
          error: resource android:style/TextAppearance.Material.Widget.Button.Borderless.Colored not found.
          error: resource android:style/TextAppearance.Material.Widget.Button.Colored not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:7: error: resource android:attr/colorError not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:11: error: resource android:attr/colorError not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:15: error: style attribute 'android:attr/keyboardNavigationCluster' not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values-v28\values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values-v28\values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3036: error: resource android:attr/fontStyle not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3037: error: resource android:attr/font not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3038: error: resource android:attr/fontWeight not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3039: error: resource android:attr/fontVariationSettings not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3040: error: resource android:attr/ttcIndex not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3121: error: resource android:attr/startX not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3124: error: resource android:attr/startY not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3127: error: resource android:attr/endX not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3130: error: resource android:attr/endY not found.
          F:\react-native\JRCT\node_modules\jmrtc-react-native\android\build\intermediates\res\merged\release\values\values.xml:3138: error: resource android:attr/offset not found.
    复制代码

    解决方案:修改目标依赖的 build.gradle compileSdkVersionbuildToolsVersionminSdkVersiontargetSdkVersion 版本同项目一致即可解决

转载于:https://juejin.im/post/5b4d47e96fb9a04f844abde0

你可能感兴趣的:(React Native 错误集合)