web-dev-qa-db-fra.com

com.Android.tools.r8.errors.CompilationError: Type de programme déjà présent: androidx.annotation.AnimRes

J'essaie de reconstruire mon projet écrit en kotlin mais il échoue constamment avec l'erreur

Program type already present: androidx.annotation.AnimRes

J'ai essayé

  • Cache invalide

  • nettoyage et reconstruction

  • en utilisant ./gradlew build --stacktrace --info commande

mais sans succès.

L'exception est

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

La trace de pile est

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

Le build.gradle de mon application est

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 du projet

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
}
17
Edijae Crusar

Après avoir lutté et cherché de l'aide ici et là, j'ai découvert que ./gradlew app:dependencies La commande fournissait une sortie importante pour résoudre l’erreur.

Tout d’abord, l’erreur est Program type already present: androidx.annotation.AnimRes

Program type already present _ signifie qu'il existe un conflit de noms, et dans ce cas la bibliothèque androidx.annotation, utilisée par plusieurs bibliothèques. C’est-à-dire que les autres bibliothèques contiennent androidx.annotation library comme dépendance transitive. Pour voir ces bibliothèques, j'ai ouvert le terminal à partir du dossier racine de mon projet (mon système d'exploitation est Ubuntu) et j'ai exécuté ./gradlew app:dependencies commande. Cela a produit une longue liste de configurations et leurs dépendances. ci-dessous est un extrait.

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 (*)

L'extrait ci-dessus montre certaines des dépendances pour debugCompileClasspath configuration.Nous pouvons voir que androidx.databinding:databinding-runtime:3.3.0-alpha07 contient androidx.annotation:annotation:1.0.0-rc02 comme dépendance transitive.

Il contient également androidx.lifecycle:lifecycle-runtime:2.0.0-alpha1 qui contient à son tour androidx.annotation:annotation:1.0.0-rc01.

androidx.annotation:annotation:1.0.0-rc01 -> 1.0.0-rc02 signifie que la version 1.0.0-rc02 sera utilisé à la place de la version 1.0.0-rc01.

Ci-dessous, un autre extrait de la même sortie longue produite par ./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

De ce qui précède, nous pouvons voir que androidx.lifecycle:lifecycle-compiler:2.0.0-alpha1 contient la version 1.0.0-alpha1 de androidx.annotation:annotation

Cela signifie qu'à la fin, mon module app aura deux versions 1.0.0-alpha1 et 1.0.0-rc02 qui rend la construction vouée à l’échec.

Pour résoudre cette erreur, je viens de supprimer les dépendances transitives en ajoutant le bloc de code ci-dessous dans le build.gradle de mon application.

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

Donc build.gradle de mon application va ressembler à ceci

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
}

Après cela, je viens de synchroniser, de nettoyer et de reconstruire mon projet.

30
Edijae Crusar