如何解决“ NoClassDefFoundError:无法解决以下问题:Landroidx / core / animation / AnimatorCompatHelper;”

时间:2019-04-01 19:30:10

标签: java android android-gradle

摘要

我想将我的旧应用程序迁移到新的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”是否有任何更新。我使用的版本仍使用旧的支持库。我想避免更新lib,因为迁移到新版本似乎是一项艰巨的任务。我希望在应用程序再次运行后进行迁移。

这是我的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配置错误,还是与过时的库有关?还是其他?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

如果缺少,请添加此依赖项

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