Android Duplicate Class Found in Modules (Exclude packages)

Oct 16, 2019

It usually happens when you import libraries which uses java packages which conflict with android packages of the same name.

Duplicate class com.google.api.client.http.apache.ApacheHttpRequest found in modules google-http-client-1.29.1.jar (com.google.http-client:google-http-client:1.29.1) and google-http-client-apache-2.0.0.jar (com.google.http-client:google-http-client-apache:2.0.0)

Usually the solution involve removing packages from libraries which uses the conflicting java library

Edit Module:app build.gradle

dependencies {
    implementation('com.google.api-client:google-api-client-android:1.23.0') {
        exclude group: 'org.apache.httpcomponents'
        exclude module: 'guava-jdk5'
    }
}

If you are unsure which libraries is causing the problem

android {
    configurations {
        implementation.exclude group: 'org.apache.httpcomponents'
        exclude module: 'guava-jdk5'
    }
}

Sometimes the Android implementation and Java implementation differs greatly, such as protobuf-lite and protobuf-java. Excluding either one packages could solve compilation error, but you will bump into runtime error when the underlying implementation is not found.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.