简体   繁体   English

React Native 0.60-无法运行带有react-native run-android的应用程序:java.lang.NoClassDefFoundError

[英]React Native 0.60 - Unable to Run App with react-native run-android: java.lang.NoClassDefFoundError

It's late on Friday before a long weekend, and I'm having issues running an react-native app. 在一个漫长的周末之前的周五晚,我在运行react-native应用程序时遇到了问题。 Current version is 0.60 (latest), and running the command 当前版本是0.60 (最新),并运行命令

react-native run-android

Results in a debug build that successfully installs on my connected app, but crashes upon opening with the following error: 生成一个debug版本,该debug版本已成功安装在我连接的应用程序上,但在打开时崩溃并显示以下错误:

FATAL EXCEPTION: main 致命异常:主要
Process: com.myApp, PID: XXXX 流程:com.myApp,PID:XXXX
java.lang.NoClassDefFoundError: Failed resolution of: java.lang.NoClassDefFoundError:无法解决以下问题:
Lcom/google/android/gms/common/internal/zzbq Lcom / google / android / gms / common / internal / zzbq

Googling this cryptic error results in a number of results suggesting MultiDex to be the culprit, and how to handle this. 仔细检查此隐秘错误会导致许多结果,表明MultiDex是罪魁祸首,以及如何处理此问题。 I'll link some threads for research's sake: 为了研究的缘故,我将链接一些线程:

Android 3.1.1 - Failed resolution of: Lcom/google/android/gms/common/internal/zzbq; Android 3.1.1-无法解决以下问题:Lcom / google / android / gms / common / internal / zzbq;

Didn't find class "com.google.android.gms.common.internal.zzbq" on path: DexPathList 在路径:DexPathList上找不到类“ com.google.android.gms.common.internal.zzbq”
(links to previous result) (链接到以前的结果)

One of the solutions, namely overriding use version for com.google.android.gms if name contains multidex to version 12.0.1 works for debug builds : 解决方案之一,即如果名称包含12.0.1版的multidex ,则覆盖com.google.android.gms use version 可用于debug版本

In android/build.gradle : android/build.gradle

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.google.android.gms'
            && !details.requested.name.contains('multidex') ) {
                details.useVersion "12.0.1"
            }
        }
    }
}

However, this causes completely different issues for production builds: 但是,这会导致production版本完全不同的问题:

Could not find com.google.android.gms:play-services-vision-image-label:12.0.1. 找不到com.google.android.gms:play-services-vision-image-label:12.0.1。
Could not find com.google.android.gms:play-services-clearcut:12.0.1 找不到com.google.android.gms:play-services-clearcut:12.0.1
Could not find com.google.android.gms:play-services-phenotype:12.0.1 找不到com.google.android.gms:play-services-表型:12.0.1
Could not find com.google.android.gms:play-services-stats:12.0.1 找不到com.google.android.gms:play-services-stats:12.0.1

All of these are saying "Required by XXX:17.0.1", so I tried details.useVersion "17.0.1" , but that resulted in a similar issue: 所有这些都说“ XXX:17.0.1必需”,所以我尝试了details.useVersion "17.0.1" ,但是导致了类似的问题:

Could not find com.google.android.gms:play-services-location:17.0.1 找不到com.google.android.gms:play-services-location:17.0.1
Could not find com.google.android.gms:play-services-base:17.0.1 找不到com.google.android.gms:play-services-base:17.0.1
Could not find com.google.android.gms:play-services-basement:17.0.1 找不到com.google.android.gms:play-services-basement:17.0.1
Could not find com.google.android.gms:play-services-tasks:17.0.1 找不到com.google.android.gms:play-services-tasks:17.0.1

Some of these modules have version 17.0.1 , while others are at 17.0.2 or 17.0.0 , so a strict use version XYZ will not work for release build. 其中一些模块的版本为17.0.1 ,而其他模块的版本为17.0.217.0.0 ,因此严格use version XYZ不适用于release版本。

If I remove this subProjects { ... } declaration and attempt to enable MultiDex as suggested in other answers: 如果我删除此subProjects { ... }声明并尝试按照其他答案中的建议启用MultiDex

In android/app/build.gradle : android/app/build.gradle

android {
    defaultConfig {
        ...
        multiDexEnabled true
    }
}
dependencies {
    ...
    implementation 'com.android.support:multidex:1.0.3'
}

This results in the same error for both debug and release builds, and a little bit of extra Googling uncovers that MultiDex is not required for SDK version > 27.0.0 (looks to be using 28.0.0 / 28.0.3 ) 这导致了同样的错误两种debugrelease版本,以及额外的谷歌搜索一点点揭露说MultiDex不需要SDK版本> 27.0.0 (看起来是使用28.0.0 / 28.0.3

I've been banging my head against this for the entire day and haven't been able to make any progress. 我整天都在为此烦恼,没有取得任何进展。 Has anyone seen this issue as it pertains to React Native 0.60 ? 有人看到过与React Native 0.60相关的问题吗?

Note: There are a couple plugins that are using these com.google.android.gms in my project, namely: 注意:在我的项目中,有几个使用这些com.google.android.gms插件,即:

  • react-native-background-geolocation
    • implementation "com.google.android.gms:play-services-location:$playServicesLocationVersion"
  • react-native-camera
    • generalImplementation "com.google.android.gms:play-services-vision:$googlePlayServicesVisionVersion"
  • react-native-device-info
    • implementation "com.google.android.gms:play-services-gcm:${safeExtGet('googlePlayServicesVersion', '16.1.0')}"

Solution is to use the new bundle/build process for Android .aab and .apk files, as shown on React Native documentation. 解决方案是对Android .aab.apk文件使用新的包/构建过程,如React Native文档所示。 Current process that is throwing errors is as follows: 当前引发错误的过程如下:

cd android
./gradlew clean
cd ..
bundleAPK (`react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/`)
buildReleaseAPK (`cd ./android && ./gradlew assembleRelease && cd ..`
installReleaseAPKAlt (`adb install -r ./android/app/build/outputs/apk/release/app-release.apk`)

When successful, this generates the .apk file and installs it on device, similar to downloading/installing directly from the Play Store. 成功后,它将生成.apk文件并将其安装在设备上,类似于直接从Play商店下载/安装。 Various bugs in build process are preventing this, as detailed above. 如上所述,构建过程中的各种错误阻止了这种情况。

React Native documentation suggest to use a different set of commands to generate .aab file, and from that the .apk : React Native文档建议使用一组不同的命令来生成.aab文件,然后从该文件中生成.apk

cd android
./gradlew clean
./gradlew bundleRelease
cd .. && react-native run-android --variant=release

Complete details can be found at https://facebook.github.io/react-native/docs/signed-apk-android#generating-the-release-apk . 完整的详细信息可以在https://facebook.github.io/react-native/docs/signed-apk-android#generating-the-release-apk中找到。 Information about generating Signed APK included since previous implementation. 自先前实施以来,已包含有关生成Signed APK的信息。

Using this approach, .aab and .apk files are generated and installed on device, but another issue arises in lack of ic_launcher_rounded . 使用这种方法,可以生成.aab.apk文件并将其安装在设备上,但是由于缺少ic_launcher_rounded出现了另一个问题。 See React Native 0.60 - ic_launcher_round missing for Release Bundle/Build for details. 有关详细信息,请参见Release Bundle / Build缺少React Native 0.60-ic_launcher_round Marking question as closed when allowed. 允许时将问题标记为已关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM