Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix this "Duplicate class error" in Android Studio

I updated implementation com.google.android.gms:play-services-ads:19.8.0 to implementation com.google.android.gms:play-services-ads:20.4.0 and now I get this error:

Duplicate class com.google.android.gms.internal.measurement.zzbs found in modules jetified-play-services-measurement-18.0.2-runtime (com.google.android.gms:play-services-measurement:18.0.2) and jetified-play-services-measurement-sdk-api-18.0.3-runtime (com.google.android.gms:play-services-measurement-sdk-api:18.0.3)
Duplicate class com.google.android.gms.internal.measurement.zzl found in modules jetified-play-services-measurement-impl-18.0.2-runtime (com.google.android.gms:play-services-measurement-impl:18.0.2) and jetified-play-services-measurement-sdk-api-18.0.3-runtime (com.google.android.gms:play-services-measurement-sdk-api:18.0.3)
Duplicate class com.google.android.gms.measurement.internal.zzfh found in modules jetified-play-services-measurement-base-18.0.3-runtime (com.google.android.gms:play-services-measurement-base:18.0.3) and jetified-play-services-measurement-impl-18.0.2-runtime (com.google.android.gms:play-services-measurement-impl:18.0.2)
Duplicate class com.google.android.gms.measurement.internal.zzgn found in modules jetified-play-services-measurement-base-18.0.3-runtime (com.google.android.gms:play-services-measurement-base:18.0.3) and jetified-play-services-measurement-impl-18.0.2-runtime (com.google.android.gms:play-services-measurement-impl:18.0.2)
Duplicate class com.google.android.gms.measurement.internal.zzgo found in modules jetified-play-services-measurement-base-18.0.3-runtime (com.google.android.gms:play-services-measurement-base:18.0.3) and jetified-play-services-measurement-impl-18.0.2-runtime (com.google.android.gms:play-services-measurement-impl:18.0.2)
Duplicate class com.google.android.gms.measurement.internal.zzgp found in modules jetified-play-services-measurement-base-18.0.3-runtime (com.google.android.gms:play-services-measurement-base:18.0.3) and jetified-play-services-measurement-impl-18.0.2-runtime (com.google.android.gms:play-services-measurement-impl:18.0.2)
Duplicate class com.google.android.gms.measurement.internal.zzgq found in modules jetified-play-services-measurement-base-18.0.3-runtime (com.google.android.gms:play-services-measurement-base:18.0.3) and jetified-play-services-measurement-impl-18.0.2-runtime (com.google.android.gms:play-services-measurement-impl:18.0.2)
Duplicate class com.google.android.gms.measurement.internal.zzhs found in modules jetified-play-services-measurement-18.0.2-runtime (com.google.android.gms:play-services-measurement:18.0.2) and jetified-play-services-measurement-base-18.0.3-runtime (com.google.android.gms:play-services-measurement-base:18.0.3)
Duplicate class com.google.android.gms.measurement.internal.zzhx found in modules jetified-play-services-measurement-base-18.0.3-runtime (com.google.android.gms:play-services-measurement-base:18.0.3) and jetified-play-services-measurement-impl-18.0.2-runtime (com.google.android.gms:play-services-measurement-impl:18.0.2)

How to fix this error?

like image 266
user1493628 Avatar asked Sep 23 '21 16:09

user1493628


People also ask

What is the error duplicate class?

This may be due to an unsuccessfull name change of the module. In such a case, the module is copied in the javasource folder in the app directory with a new name and the old folder is kept, resulting in two identical folders with different names.

What is duplicate resource error in Android Studio?

It seems like you have the same image resource in two files OR you added an additional, non-required file to the res folder. Rename or remove the other. You can check the required structure of the res folder and subfolders by downloading a fresh copy of WebViewGold from CodeCanyon.


3 Answers

I am having the same issue and I fixed it by downgrading play-services-ads dependency like this. So you can try.

def ads_version = "20.3.0"
implementation "com.google.android.gms:play-services-ads:$ads_version"
like image 170
Md. Shofiulla Avatar answered Oct 23 '22 11:10

Md. Shofiulla


I ran into the same issue today, and after many trial and errors, I finally found that it was caused by a firebase dependency. If you're using firebase in your project, make sure to use the latest BoM version in app/build.gradle:

dependencies{
   // implementation platform('com.google.firebase:firebase-bom:25.12.0')  <-- Produces errors
  implementation platform('com.google.firebase:firebase-bom:29.0.0')  //<-- This works
}

If you are not using firebase and still get this error, you can try excluding the offending classes directly in app/build.gradle:

android {
   defaultConfig {
    ...
   }
   buildTypes {
    ...
   }

   configurations {
     all {
       exclude group: "com.google.android.gms", module: "play-services-measurement-impl"
       exclude group: "com.google.android.gms", module: "play-services-measurement"
     }
   }
}
like image 20
Savithru Avatar answered Oct 23 '22 09:10

Savithru


The error is being produced due to the conflicts between firebase & google service dependency. So you have 2 options.

You can change the versions and keep doing that until you find the perfect working combination which is time consuming or you can find a solution that works every time so you don't have to play with the versions every time. You just use that solution wherever you get such error.

Here, you need to use the updated method of injecting the firebase dependency as shown in the official documentation.

Put this line in your dependency :

implementation platform('com.google.firebase:firebase-bom:28.1.0')

Then, You can use any firebase dependency without specifying the versions, and it will work flawlessly all the time!

The final code looks like this,

implementation platform('com.google.firebase:firebase-bom:28.1.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-config'
implementation 'com.google.firebase:firebase-crashlytics'

And it won't produce any duplication related errors with the play-services dependency.

like image 34
Dev4Life Avatar answered Oct 23 '22 09:10

Dev4Life