com.android.tools.r8.errors.CompilationError: Program type already present: androidx.annotation.AnimRes

After struggling and struggling and looking for help here and there, I discovered that ./gradlew app:dependencies command was providing important output to solve the error.

First of all, the error is Program type already present: androidx.annotation.AnimRes

Program type already present means there is a naming conflict, and in this case the androidx.annotation library, which is use by several libraries.That is other libraries contain androidx.annotation library as a transitive dependency. To see this libraries, i opened the terminal from my project's root folder(my OS is Ubuntu) and run ./gradlew app:dependencies command. It produced a long list of configurations and their dependences. below is an extract.

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

The above extract shows some of dependencies for debugCompileClasspath configuration.We can see that androidx.databinding:databinding-runtime:3.3.0-alpha07 contains androidx.annotation:annotation:1.0.0-rc02 as a transitive dependency.

It also contains androidx.lifecycle:lifecycle-runtime:2.0.0-alpha1 which in turns contain androidx.annotation:annotation:1.0.0-rc01.

androidx.annotation:annotation:1.0.0-rc01 -> 1.0.0-rc02 means that version 1.0.0-rc02 will be used instead of version 1.0.0-rc01 .

Below is another extract from the same long output produced by ./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

From the above, we can see that androidx.lifecycle:lifecycle-compiler:2.0.0-alpha1 contains version 1.0.0-alpha1 of androidx.annotation:annotation

This means at the end, my module app will have two versions 1.0.0-alpha1 and 1.0.0-rc02 which makes build to fail.

To solve this error, i just removed the transitive dependencies by adding the below block of code in my app's build.gradle

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

So my app's build.gradle will look like this

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
}

After that, i just synced,cleaned and rebuild my project.


To solve this problem I write the command ./gradlew clean in Android Studio terminal and voilà !


In my case I just clean the project and rebuild it and it worked.

  • Build -> Clean project
  • Build -> Rebuild project