How do I fix this "Duplicate class error" in Android Studio - java
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?
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"
}
}
}
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"
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.
Best way to fix this and prepare your project for later is to migrate completely to AndroidX. First check if your project is ready, Refactor > Migrate to AndroidX. Once your project is independent of jcenter or any other older library/repo and compatible with AndroidX you won't need Jetifier anymore, so either comment it out or remove it. This should possibly fix the problem.
...
#android.enableJetifier=false
...
In my case this wasn't enough to solve the problem, if you still receive the same error but from a different library, it's possibly because of other existing dependencies, avoid using bundled libraries or wildcard ones (in which system is left to guess) make sure you update them all and clean rebuild the project.
If you still have to keep some conflicting library, try excluding individual classes to avoid the error like this from build.gradle:
android {
...
defaultConfig {
...
}
buildTypes {
...
}
configurations {
all { // You should exclude one of them not both of them
exclude group: "com.android.support", module: "support-core-ui"
exclude group: "com.android.support", module: "support-compat"
}
}
}
Official docs on module exclusion - Excluding transitive dependencies
I also tried few more things before figuring the solution, like deleting .gradle from local machine, incase some old libraries got cached and causing the error, or resetting Android Studio, it just won't work.
Repo I tested the solution here, as you can see in this line I had to comment out the bundle module and specify latest updates to individual dependencies.
You can read more on module dependency errors for reference.
Add this line android.enableJetifier=true to your gradle.properties
Related
How to solve error: package org.springframework.context.annotation does not exist import org.springframework.context.annotation.Bean?
After updating the gradle wrapper to version 7.3.2 I get the following error when trying to build my spring boot project: error: package org.springframework.context.annotation does not exist import org.springframework.context.annotation.Bean; Why is this happening? Update: Found the problem, but do not know how to fix yet. I have multiple modules in my project. I have a "common" module which is used by other modules. With the old gradle version the libs of the common project were added to the compileClasspath of other modules, with the new version it adds them only to the runtimeClasspath. The dependencies used to be declared as compile, now with gradle 7+ I had to change them to implementation. Any suggestions? In other module: dependencies { implementation project(':common') ... } In common module: dependencies { implementation spring.web ... }
I think you have an issue with transitivity , if you use java-library instead of java plugin plugins { id 'java-library' } https://docs.gradle.org/current/userguide/java_library_plugin.html#java_library_plugin then you can use the api instead of compile which can help you can see a good explanation What's the difference between implementation, api and compile in Gradle?
Duplicate class found in modules (exclude packages)
I have added 2 modules in my project but I got the following error Duplicate class com.bumptech.glide.GeneratedAppGlideModuleImpl found in modules jetified-XXX-XXX-XXX_XXX_0.2-runtime (com.github.YYY:YYY:YYYY_0.2) and jetified-XXX-XXX-XXX-1.0.24-runtime (com.github.XXX:XXX-XXX-XXX:1.0.24) Duplicate class com.bumptech.glide.GeneratedRequestManagerFactory found in modules jetified-YYY-YYY-YYY_0.2-runtime (com.github.nithraedu:YYYY-YYY:YYY_0.2) and jetified-XXX-XXX-XXX-1.0.24-runtime (com.github.XXX:XXX-XXX-XXX:1.0.24) how do I solve this problem?
If you have duplicate classes, it shows that you have added dependencies that use the same modules and classes so you must find these dependencies and exclude the duplicates. In your case I think you want to use glide library so you can simply add: implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' In your app level build.gradle file. This is an issue for some of the dependencies that used same classes before AndroidX. The other option for you is to simply migrate to AndroidX which solves these kinds of problems. if you want to see your dependency_tree and identify what's causing the duplication you can use this command: gradle -q dependencies yourProject:dependencies --configuration compile or you can find your dependency_tree in view -> tool-windows -> gradle -> YourAppName -> tasks -> android -> androidDependencies after that you must find the duplicated module and exclude it from the library dependency to avoid duplication for example: implementation('com.example.m:m:1.0') { exclude group: 'org.unwanted', module: 'x }
https://bumptech.github.io/glide/doc/configuration.html#avoid-appglidemodule-in-libraries The libraries which you are using glide and it extends the AppGlideModule instead of LibraryGlideModule.
Replace android.support.v4.util.ArraySet with androidx.collection.ArraySet in Maven library
I have a 3rd party library that depends on: import android.support.v4.util.ArraySet; And I want to use the latest Android libraries in the rest of my project: import androidx.collection.ArraySet; However, since I'm getting the library via Maven, I can't seem to force it to replace the v4 dependency for the androidx one and have it refer to the right class. I've already tried: implementation 'androidx.collection:collection:1.1.0' implementation('some-library:v1') { exclude group: 'com.android.support', module: 'support-v4' } And: configurations.all { resolutionStrategy.dependencySubstitution { substitute module('com.android.support:support-v4') with module('androidx.collection:collection:1.1.0') } } I've even tried a few more things including the force=true for the dependencies I want. However I'm still getting: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArraySet; How can I tell Gradle to resolve the imports for android.support.v4.util.ArraySet to androidx.collection.ArraySet when I don't have access to the source code?
As per the Migrate to AndroidX guide: android.enableJetifier=true The Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries. So this rewriting of Maven dependencies is exactly what Jetifier does for you. Make sure you've added the android.enableJetifier=true to your gradle.properties file to enable Jetifier.
How do I work out what dependency I need for Spring Boot MultiValueMap in Gradle?
I have some code which needs access to org.springframework.util.MultiValueMap. I am running IntelliJ IDEA on a Windows machine. I am getting a compilation error: "Cannot access org.springframework.util.MultiValueMap". This error occurs on the following line of code (request is an object of type ClientHttpRequest), which is in a junit test file: String authorization = request.getHeaders().getFirst("Authorization"); I have tried the following two import statements, based on the documentation for MultiValueMap (https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/MultiValueMap.html): import org.springframework.util; import org.springframework.util.MultiValueMap; However these do not work - latter parts of those statements are highlighted in red. I believe this is because I don't have the correct dependencies in build.gradle. These are the Spring Framework dependencies I currently have: compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.ws:spring-ws-core") compile('org.springframework.security:spring-security-web:5.0.2.BUILD-SNAPSHOT') compile('org.springframework.security:spring-security-config:5.0.2.BUILD-SNAPSHOT') compile('org.springframework.security:spring-security-config:5.0.2.BUILD-SNAPSHOT') compile("org.springframework.security.oauth:spring-security-oauth2:2.0.8.RELEASE") testCompile('org.springframework.boot:spring-boot-starter-test') testCompile('org.springframework.ws:spring-ws-test')
I have found a fix! The answer was on this page here: https://spring.io/blog/2015/02/23/better-dependency-management-for-gradle It appears that using gradle for dependency management for Spring-Boot can be problematic because you need so many lines in your dependencies section, like the ones I listed above. I followed the instructions on that page, inserting the following lines into my build.gradle: apply plugin: "io.spring.dependency-management" dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:1.1.1.RELEASE' } } My code now compiles. I still don't know where I would have been able to find the correct individual dependency for build.gradle, but using this plugin it is no longer an issue.
You may be doing a bit too much work here. Alternatively, you run the risk of having your dependencies slightly out-of-sync. The only dependency you need is spring-boot-starter, which has a transitive dependency to the correct spring-core version you want. You can use this in your build.gradle file: compile'org.springframework.boot:spring-boot-starter:1.5.10.RELEASE' If you want to use the Gradle plugin for Spring Boot (which can be found here), place the version of Spring Boot you want into the plugin section and omit the versions on your dependencies.
This line would go at the top of your class import org.springframework.util.MultiValueMap; And while I'm not necessarily familiar with this exact library, looking on mvnrepository tells me that you would need something like this in your build.gradle file repositories { mavenCentral() } dependencies { compile group: 'org.springframework', name: 'spring-core', version: '5.0.3.RELEASE' }
Gradle - Error Could not find method implementation() for arguments [com.android.support:appcompat-v7:26.0.0]
I am trying to open existing android project in android studio and it gradle cannot build the app without the error Error android studio keeps on throwing Error:(74, 1) A problem occurred evaluating project ':app'. > Could not find method implementation() for arguments [com.android.support:appcompat-v7:26.0.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. My Code in build.gradle Which can help to understand my issue My dependencies dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') // google & support implementation "com.android.support:appcompat-v7:$supportVersion" implementation "com.android.support:cardview-v7:$supportVersion" implementation "com.android.support:recyclerview-v7:$supportVersion" implementation "com.android.support:design:$supportVersion" implementation "com.android.support:palette-v7:$supportVersion" implementation "com.android.support:customtabs:$supportVersion" implementation "com.android.support:support-v4:$supportVersion" implementation 'com.google.android.exoplayer:exoplayer:r2.0.4' // utils implementation 'com.github.bumptech.glide:glide:4.0.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0' implementation 'com.koushikdutta.ion:ion:2.1.7' implementation 'com.github.Commit451:bypasses:1.0.4' implementation 'com.jakewharton:butterknife:8.8.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.0' implementation 'com.drewnoakes:metadata-extractor:2.9.1' implementation "com.orhanobut:hawk:2.0.1" } Please help to solve the issue
Make sure you're adding these dependencies in android/app/build.gradle, not android/build.gradle.
Replace compile with implementation. compile was recently deprecated and replaced by implementation or api
Make sure your Gradle version is 3.*.* or higher before using "implementation". Open the project level Gradle file under dependencies: dependencies{ classpath 'com.android.tools.build:gradle:3.1.2' } Open the 'gradle-wrapper.properties' file and set the distributionUrl: distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip or latest version. Sync the project. I Hope this solves your problem.
You need to use at least Gradle 3.4 or newer to be able to use implementation. It is not recommended to keep using the deprecated compile since this can result in slower build times. For more details see the official android developer guide: When your module configures an implementation dependency, it's letting Gradle know that the module does not want to leak the dependency to other modules at compile time. That is, the dependency is available to other modules only at runtime. Using this dependency configuration instead of api or compile can result in significant build time improvements because it reduces the amount of projects that the build system needs to recompile. For example, if an implementation dependency changes its API, Gradle recompiles only that dependency and the modules that directly depend on it. Most app and test modules should use this configuration. https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations Update: compile will be removed by end of 2018, so make sure that you use only implementation now: Warning:Configuration 'compile' is obsolete and has been replaced with 'implementation'. It will be removed at the end of 2018
change apply plugin: 'java' to apply plugin: 'java-library' java-library-plugin
For me I put my dependencies in the wrong spot. buildscript { dependencies { //Don't put dependencies here. } } dependencies { //Put them here }
Solved this by adding my dependancies in the app folder Go to app < Gradle Scripts < gradle.build(Module: app) and add the depandancies in that file and not the global build.gradle file
So ridiculous, but I still wanna share my experience in case of that someone falls into the situation like me. Please check if you changed: compileSdkVersion --> implementationSdkVersion by mistake
I moved implementation to module-level build.gradle from root-level build.gradle. It solves the issue.
Your Code dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') Replace it By dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs')
Replace your implementation with classpath. That should work.
As mentioned here, https://stackoverflow.com/a/50941562/2186220, use gradle plugin version 3 or higher while using 'implementation'. Also, use the google() repository in buildscript. buildscript { repositories { google() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' } } These changes should solve the issue.
As suggested in official docs you need add these : buildscript { repositories { // Gradle 4.1 and higher include support for Google's Maven repo using // the google() method. And you need to include this repo to download // Android Gradle plugin 3.0.0 or higher. google() ... } dependencies { classpath 'com.android.tools.build:gradle:4.2.0' } } adding these remove my error. Also use implementation instead of compile.
In my case it was because I used Implementation instead of implementation.
For me the problem was that I wrote: android { compileSdk 31 … Instead of: android { compileSdkVersion 31 So make sure your build.gradle is correct.
If implementation is not defined, you are writing on a wrong file. On Unity 2019+ the correct file is main template grandle and not some of the others.