首页 > 解决方案 > React Native 0.60 - 无法使用 react-native run-android 运行应用程序:java.lang.NoClassDefFoundError

问题描述

在长周末之前的星期五很晚,我在运行react-native应用程序时遇到了问题。当前版本是0.60(最新),并运行命令

react-native run-android

导致debug构建成功安装在我连接的应用程序上,但在打开时崩溃并出现以下错误:

致命异常:主
进程:com.myApp,PID:XXXX
java.lang.NoClassDefFoundError:解析失败: Lcom
/google/android/gms/common/internal/zzbq

谷歌搜索这个神秘的错误会导致一些结果表明MultiDex是罪魁祸首,以及如何处理这个问题。为了研究,我将链接一些线程:

Android 3.1.1 - 解析失败:Lcom/google/android/gms/common/internal/zzbq;

在路径上找不到类“com.google.android.gms.common.internal.zzbq”:DexPathList
(链接到以前的结果)

解决方案之一,即覆盖if name contains to versionuse version适用buildscom.google.android.gmsmultidex12.0.1 debug

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"
            }
        }
    }
}

但是,这会导致production构建完全不同的问题:

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

所有这些都说“XXX:17.0.1 需要”,所以我尝试details.useVersion "17.0.1"了,但这导致了类似的问题:

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

其中一些模块具有 version 17.0.1,而其他模块位于17.0.2or 17.0.0,因此严格use version X.Y.Z不适用于release构建。

如果我删除此subProjects { ... }声明并尝试MultiDex按照其他答案中的建议启用:

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)

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?

Note: There are a couple plugins that are using these com.google.android.gms in my project, namely:

标签: androidreact-nativegoogle-play-servicesandroid-multidex

解决方案


Solution is to use the new bundle/build process for Android .aab and .apk files, as shown on React Native documentation. 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. 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:

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. Information about generating Signed APK included since previous implementation.

Using this approach, .aab and .apk files are generated and installed on device, but another issue arises in lack of ic_launcher_rounded. See React Native 0.60 - ic_launcher_round missing for Release Bundle/Build for details. Marking question as closed when allowed.


推荐阅读