com.android.tools.r8.errors.CompilationError:程序类型已经存在:androidx.annotation.AnimRes

Posted

技术标签:

【中文标题】com.android.tools.r8.errors.CompilationError:程序类型已经存在:androidx.annotation.AnimRes【英文标题】:com.android.tools.r8.errors.CompilationError: Program type already present: androidx.annotation.AnimRes 【发布时间】:2019-01-30 19:12:36 【问题描述】:

我正在尝试重建我用 kotlin 编写的项目,但它一直因错误而失败

Program type already present: androidx.annotation.AnimRes

我试过了

缓存失效

清理和重建

使用./gradlew build --stacktrace --info 命令

但没有成功。

例外是

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesWithMultidexlistForDebug'

堆栈跟踪是

Caused by: java.lang.RuntimeException: com.android.build.api.transform.TransformException: Error while generating the main dex list.
        at com.android.builder.profile.Recorder$Block.handleException(Recorder.java:55)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:104)
        at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:230)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
        at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:131)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:300)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:292)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:174)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
        at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:120)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:99)
        ... 111 more
Caused by: com.android.build.api.transform.TransformException: Error while generating the main dex list.
        at com.android.build.gradle.internal.transforms.D8MainDexListTransform.transform(D8MainDexListTransform.kt:143)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:239)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:235)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
        ... 124 more
Caused by: com.android.builder.multidex.D8MainDexList$MainDexListException: com.android.tools.r8.errors.CompilationError: Program type already present: androidx.annotation.AnimRes
        at com.android.builder.multidex.D8MainDexList.generate(D8MainDexList.java:87)
        at com.android.build.gradle.internal.transforms.D8MainDexListTransform.transform(D8MainDexListTransform.kt:128)
        ... 127 more
Caused by: com.android.tools.r8.errors.CompilationError: Program type already present: androidx.annotation.AnimRes
        at com.android.tools.r8.utils.ProgramClassCollection.resolveClassConflictImpl(ProgramClassCollection.java:64)
        at com.android.tools.r8.utils.ProgramClassCollection.lambda$create$0(ProgramClassCollection.java:25)
        at com.android.tools.r8.utils.ProgramClassCollection.create(ProgramClassCollection.java:24)
        at com.android.tools.r8.graph.LazyLoadedDexApplication$Builder.build(LazyLoadedDexApplication.java:124)
        at com.android.tools.r8.dex.ApplicationReader.read(ApplicationReader.java:123)
        at com.android.tools.r8.dex.ApplicationReader.read(ApplicationReader.java:86)
        at com.android.tools.r8.GenerateMainDexList.run(GenerateMainDexList.java:41)
        at com.android.tools.r8.GenerateMainDexList.run(GenerateMainDexList.java:111)
        at com.android.builder.multidex.D8MainDexList.generate(D8MainDexList.java:83)
        ... 128 more

我的应用的 build.gradle 是

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

android 
    compileSdkVersion 28
    defaultConfig 
        applicationId "com.my.app.id"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    
    dataBinding 
        enabled true
    
    buildToolsVersion '28.0.2'
    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    


dependencies 
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    //kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    //support
    implementation "androidx.legacy:legacy-support-v4:$androidX"
    implementation "androidx.appcompat:appcompat:$androidX"
    implementation "androidx.constraintlayout:constraintlayout:1.1.2"
    implementation "androidx.multidex:multidex:2.0.0"
    implementation "androidx.recyclerview:recyclerview:$androidX"
    implementation "androidx.vectordrawable:vectordrawable:$androidX"
    implementation "androidx.cardview:cardview:$androidX"
    implementation "androidx.palette:palette:$androidX"
    implementation "com.google.android.material:material:$androidX"

    //architecture
    implementation "androidx.lifecycle:lifecycle-runtime:$architectureVersion"
    implementation "androidx.lifecycle:lifecycle-extensions:$architectureVersionExt"
    implementation "androidx.legacy:legacy-support-v4:$androidX"
    kapt "androidx.lifecycle:lifecycle-compiler:$architectureVersion"

    //data binding
    //kapt "androidx.databinding:databinding-compiler:3.3.0-alpha04"

    //glide
    //kapt "com.github.bumptech.glide:compiler:$glideVersion"
    //implementation "com.github.bumptech.glide:glide:$glideVersion"
    implementation 'com.github.bumptech.glide:glide:4.8.0-SNAPSHOT'
    implementation 'com.github.bumptech.glide:okhttp-integration:4.8.0-SNAPSHOT'

    //dagger
    implementation "com.google.dagger:dagger:$daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    implementation "com.google.dagger:dagger-android:$daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$daggerVersion"

    // if you use the support libraries
    kapt "com.google.dagger:dagger-android-processor:$daggerVersion"

    //reactiveX
    implementation "io.reactivex.rxjava2:rxandroid:$rxandroidVersion"
    implementation "io.reactivex.rxjava2:rxjava:$rxjavaVersion"
    implementation "io.reactivex.rxjava2:rxkotlin:$rxKotlinVersion"

    //
    implementation "com.jakewharton.rxbinding2:rxbinding:$rxBinding"
    implementation "com.tbruyelle.rxpermissions2:rxpermissions:$rxPermissions"

    //ucrop
    implementation "com.github.yalantis:ucrop:$uCrop"

    //other libraries
    implementation "com.google.code.gson:gson:$gson"
    implementation "com.googlecode.libphonenumber:libphonenumber:$phoneLibVersion"

    //map and places
    implementation "com.google.android.gms:play-services-places:$mapVersion"
    implementation "com.google.android.gms:play-services-maps:$mapVersion"

    //
    implementation 'com.budiyev.android:circular-progress-bar:1.2.0'

    //retrofit
    implementation "com.squareup.retrofit2:retrofit:$retrofit"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit"
    debugImplementation "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation "androidx.test:runner:$androidX"
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

项目的 build.gradle

buildscript 
    ext.kotlin_version = '1.2.61'
    ext.realmVersion ='5.4.2'
    repositories 
        jcenter()
        maven 
            name 'glide-snapshot'
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        
        google()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:3.3.0-alpha07'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.realm:realm-gradle-plugin:$realmVersion"
        //classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    


allprojects 
    repositories 
        jcenter()
        maven 
            url "https://maven.google.com"
        
        maven  url "https://jitpack.io" 
        maven 
            name 'glide-snapshot'
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        
        google()
    

    gradle.projectsEvaluated 
        tasks.withType(JavaCompile) 
            options.compilerArgs << "-Xmaxerrs" << "1000"
        
    


/**
 * Configure common tasks on all the submodules
 */
allprojects  project ->

    // Get versions, this is where we store things
    // like the list of submodules or the version
    project.apply from: "$rootDir/versions.gradle"

task clean(type: Delete) 
    delete rootProject.buildDir

【问题讨论】:

看到与@An​​yRes 相同的情况,这可能是一个错误吗?尝试关闭 multidex 以及您尝试过的所有内容 + 更多... @fattire 我设法解决了这个错误。请看下面我的回答 【参考方案1】:

我遇到了同样的错误,通过在 Gradle 中添加这些行来解决。

repositories 
    google()
    jcenter()
    repositories 
        maven 
            url "http://storage.googleapis.com/r8-releases/raw/master"
        
    


dependencies 
    classpath 'com.android.tools:r8:1.6.84'  // Must be before the Gradle Plugin for Android.
    classpath 'com.android.tools.build:gradle:3.6.3'

【讨论】:

【参考方案2】:

我在 CI 环境中遇到此错误,因为我使用的是来自 docker 的 jdk8-openj9 jdk。

切换到openjdk-8 docker 镜像解决了这个问题。

【讨论】:

【参考方案3】:

就我而言,我只是清理项目并重新构建它,它就可以工作了。

构建 -> 清理项目 构建 -> 重建项目

【讨论】:

【参考方案4】:

在 app gradle 文件中添加multiDexEnabled true

【讨论】:

【参考方案5】:

为了解决这个问题,我在 Android Studio 终端中编写了命令./gradlew clean,瞧!

【讨论】:

或构建 -> 清理项目(在 Android Studio 中) @AITAALI :不幸的是,我不使用“Android Studio”,我只使用 Unity 2019。请告诉我如何解决这个问题? @Moos Trafic : 很遗憾,我没有使用“Android Studio”,我只使用 Unity 2019。请告诉我如何解决这个问题?【参考方案6】:

在苦苦挣扎并四处寻求帮助后,我发现./gradlew app:dependencies 命令提供了重要的输出来解决错误。

首先报错是Program type already present: androidx.annotation.AnimRes

Program type already present 表示存在命名冲突,在这种情况下,androidx.annotation 库被多个库使用。即其他库包含androidx.annotation library 作为传递依赖。要查看这个库,我从项目的根文件夹(我的操作系统是 Ubuntu)打开终端并运行 ./gradlew app:dependencies 命令。它产生了一长串配置及其依赖关系。以下是摘录。

debugCompileClasspath - Resolved configuration for compilation for variant: debug

+--- androidx.databinding:databinding-common:3.3.0-alpha07
+--- androidx.databinding:databinding-runtime:3.3.0-alpha07
|    +--- androidx.lifecycle:lifecycle-runtime:2.0.0-alpha1 -> 2.0.0-rc01
|    |    +--- androidx.lifecycle:lifecycle-common:2.0.0-rc01
|    |    |    \--- androidx.annotation:annotation:1.0.0-rc01 -> 1.0.0-rc02
|    |    +--- androidx.arch.core:core-common:2.0.0-rc01
|    |    |    \--- androidx.annotation:annotation:1.0.0-rc01 -> 1.0.0-rc02
|    |    \--- androidx.annotation:annotation:1.0.0-rc01 -> 1.0.0-rc02
|    +--- androidx.collection:collection:1.0.0-alpha1 -> 1.0.0-rc02
|    |    \--- androidx.annotation:annotation:1.0.0-rc02
|    \--- androidx.databinding:databinding-common:3.3.0-alpha07
+--- androidx.databinding:databinding-adapters:3.3.0-alpha07
|    +--- androidx.databinding:databinding-common:3.3.0-alpha07
|    \--- androidx.databinding:databinding-runtime:3.3.0-alpha07 (*)

上面的摘录显示了debugCompileClasspath 配置的一些依赖项。我们可以看到androidx.databinding:databinding-runtime:3.3.0-alpha07 包含androidx.annotation:annotation:1.0.0-rc02 作为传递依赖项。

它还包含androidx.lifecycle:lifecycle-runtime:2.0.0-alpha1,后者又包含androidx.annotation:annotation:1.0.0-rc01

androidx.annotation:annotation:1.0.0-rc01 -&gt; 1.0.0-rc02 表示将使用版本1.0.0-rc02 而不是版本1.0.0-rc01

下面是./gradlew app:dependencies产生的相同长输出的另一个摘录

kapt
+--- androidx.lifecycle:lifecycle-compiler:2.0.0-alpha1
|    +--- androidx.lifecycle:lifecycle-common:2.0.0-alpha1
|    |    \--- androidx.annotation:annotation:1.0.0-alpha1
|    +--- org.jetbrains.kotlin:kotlin-stdlib:1.2.41
|    |    \--- org.jetbrains:annotations:13.0
|    +--- com.google.auto:auto-common:0.6
|    |    \--- com.google.guava:guava:18.0 -> 23.3-jre
|    |         +--- com.google.code.findbugs:jsr305:1.3.9
|    |         +--- com.google.errorprone:error_prone_annotations:2.0.18
|    |         +--- com.google.j2objc:j2objc-annotations:1.1
|    |         \--- org.codehaus.mojo:animal-sniffer-annotations:1.14
|    \--- com.squareup:javapoet:1.8.0

从上面我们可以看出androidx.lifecycle:lifecycle-compiler:2.0.0-alpha1包含1.0.0-alpha1的版本androidx.annotation:annotation

这意味着最后,我的模块app 将有两个版本1.0.0-alpha11.0.0-rc02,这会导致构建失败。

为了解决这个错误,我只是通过在我的应用程序的 build.gradle 中添加以下代码块来删除传递依赖项

configurations 
    compile.exclude group: 'androidx.annotation', module: 'annotation'

所以我的应用程序的 build.gradle 将如下所示

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

android 
    //bla bla bla


configurations 
    compile.exclude group: 'androidx.annotation', module: 'annotation'


dependencies 
// bla bla bla

在那之后,我刚刚同步、清理并重建了我的项目。

【讨论】:

哇- 我曾尝试检查依赖项(如错误消息中所建议的那样),在我的情况下,每个实例,androidx.annotations 要么是 rc02 要么 androidx.annotations:annotations:1.0.0-rc01 -&gt; 1.0.0-rc02。我只是再次检查了一遍,除非它非常微妙并且我只是想念它,否则我不知道它在哪里被绊倒了。无论如何,我试了一下您的配置线,清理了项目,然后……构建并安装了它!很高兴找到解决方案。我只是希望我知道为什么我一开始就遇到这个问题......嗯:) 不幸的是,如果我再次清理,它会清除 两个 的注释副本......请参阅 this bug tho。似乎预建的渲染脚本支持库有一些错误,希望能尽快修复。 @fattire 我在 issuetracker 上看到了发布的错误。至于我,我使用的是implementation androidx.appcompat:appcompat:1.0.0-rc02 而不是implementation androidx.appcompat:appcompat:1.0.0-rc01,但即使在再次清理和重建之后,我也从未遇到过上述错误 你是英雄! 不幸的是,搜索此类错误类似于使用 Android Studio 和 Gradle 进行的“神秘之旅”。为什么编译器不声明这样的内容:“错误;模块 A 正在使用 lib x.x.x,而模块 B 正在使用 lib x.x.y。建议更正:……”毕竟这是 2019 年。这些类型的错误是多么的偷工减料。

以上是关于com.android.tools.r8.errors.CompilationError:程序类型已经存在:androidx.annotation.AnimRes的主要内容,如果未能解决你的问题,请参考以下文章