如何解决“ NoClassDefFoundError: Failed resolution of: Landroidx/core/animation/AnimatorCompatHelper;”问题。

5

摘要

我想将我的一个旧应用程序迁移到新的Android Studio中,更新构建工具并使用AndroidX。

它甚至算是有点成功。 应用程序启动并进入主屏幕......但很快就会崩溃。

我尝试过的内容

我已经查看了以下问题:

NoClassDefFoundError android/support/v4/animation/AnimatorCompatHelper

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/app/ActivityManagerCompat

第一个问题的解决方案已经存在于我的gradle文件中,而第二个问题的解决方案并不适合。

我认为问题在于multidex配置,因为我使用dexcount gradle插件来计算apk中的方法数,它的方法数量略多于单个dex文件所能包含的数量。 我查阅了相关文档:https://developer.android.com/studio/build/multidex Multidex已经启用,但我将最小SDK版本设置为21并增加了一个multidex-config.txt。 txt包含:

Landroidx/core/animation/AnimatorCompatHelper

然而,这对于修复错误没有任何作用。

我还检查了导入的库“FlexibleAdapter”是否有更新。 我使用的版本仍在使用旧的support libraries。 我希望避免更新该库,因为迁移到新版本看起来像是一项更大的任务。 我更喜欢在应用程序再次正常工作后执行迁移。

因此,这是我的gradle文件的一部分:

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "funnyName"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            multiDexKeepProguard file('multidex-config.txt')
        }

        debug {
            shrinkResources false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            multiDexKeepProguard file('multidex-config.txt')
        }
    }
    packagingOptions {
        exclude('META-INF/notice.txt')
        exclude('META-INF/NOTICE')
        exclude('META-INF/license.txt')
        exclude('META-INF/LICENSE')
        exclude('META-INF/LICENSE.txt')
        exclude('META-INF/NOTICE.txt')
        exclude 'META-INF/ASL2.0'
    }
    lintOptions {
        abortOnError false
    }

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.0'
            }
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    final BUTTERKNIFE_VERSION = '10.1.0'
    final ANDROID_PERMISSION_MANAGER_VERSION = '1.0.0'
    final DAGGER_VERSION = '2.5'
    final SUPPORT_LIBRARY_VERSION_LEGACY = '1.0.0'
    final HAMCREST_VERSION = '1.3'
    final MOCKITO_VERSION = '1.10.19'
    final ESPRESSO_VERSION = '3.1.0-alpha4'
    final UI_AUTOMATOR_VERSION = '2.1.2'
    final JUNIT_VERSION = '4.12'
    final RUNNER_VERSION = '1.1.0-alpha4'
    final ROBOLECTRIC_VERSION = '3.2.2'
    final MULTIDEX_VERSION = '2.0.0'
    final MOCKSERVER_VERSION = '3.5.0'
    final RETROFIT_VERSION = '2.5.0'
    final RXANDROID_VERSION = '2.0.1'
    final RXJAVA_VERSION = '2.0.4'
    def daggerCompiler = "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
    implementation "com.jakewharton:butterknife:$BUTTERKNIFE_VERSION"
    annotationProcessor "com.jakewharton:butterknife-compiler:$BUTTERKNIFE_VERSION"
    implementation "com.github.buchandersenn:android-permission-manager:$ANDROID_PERMISSION_MANAGER_VERSION"
    implementation "androidx.legacy:legacy-support-core-utils:$SUPPORT_LIBRARY_VERSION_LEGACY"
    implementation "androidx.appcompat:appcompat:$SUPPORT_LIBRARY_VERSION_LEGACY"
    implementation "com.google.android.material:material:$SUPPORT_LIBRARY_VERSION_LEGACY"
    implementation "androidx.legacy:legacy-support-v13:$SUPPORT_LIBRARY_VERSION_LEGACY"
    implementation "androidx.legacy:legacy-support-v4:$SUPPORT_LIBRARY_VERSION_LEGACY"
    implementation "androidx.core:core:$SUPPORT_LIBRARY_VERSION_LEGACY"
    implementation "androidx.recyclerview:recyclerview:$SUPPORT_LIBRARY_VERSION_LEGACY"
    implementation "androidx.cardview:cardview:$SUPPORT_LIBRARY_VERSION_LEGACY"
    androidTestImplementation "androidx.appcompat:appcompat:$SUPPORT_LIBRARY_VERSION_LEGACY"

以下是 Stacktrace 的部分内容:

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/animation/AnimatorCompatHelper;
        at eu.davidea.flexibleadapter.common.FlexibleItemAnimator.resetAnimation(FlexibleItemAnimator.java:646)
        at eu.davidea.flexibleadapter.common.FlexibleItemAnimator.animateMove(FlexibleItemAnimator.java:385)
        at androidx.recyclerview.widget.SimpleItemAnimator.animatePersistence(SimpleItemAnimator.java:138)
        at androidx.recyclerview.widget.RecyclerView$4.processPersistent(RecyclerView.java:632)
        at androidx.recyclerview.widget.ViewInfoStore.process(ViewInfoStore.java:237)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3994)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3652)
        at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
        at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5044)

这是一个 multidex 配置错误还是与过时的库有关?还是其他原因导致的?


我也遇到了这个问题.. 需要一个解决方案。 - Sourav Emon
2个回答

0

在将FlexibleAdapter迁移到最新版本(使用AndroidX)后,应用程序编译正确并正常运行,除了一些与迁移相关的问题外。


0

请检查您的 build.gradle 文件(应用程序级别),看看 androidx.core 库是否缺失

如果缺失,请添加以下依赖项

  implementation 'androidx.core:core-ktx:1.2.0'

已经尝试添加了这个,但对我没有起作用。 - Sourav Emon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接