How to fix duplicate classes in gradle build for android studio - android

I am trying to add ads in the build.gradle. However, it doesn't seem to be working.
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "br.com.alessanderleite.catchtheballgame"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.gms:play-services-ads:9.4.0'
}
Error:
Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-v4:23.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-v4:23.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-v4:23.0.0)
Go to the documentation to learn how to Fix dependency resolution errors.

You are using
implementation 'com.google.android.gms:play-services-ads:9.4.0'
This version uses the support libraries and you can't use androidx and support libraries together.
Use an update version of ads migrated to androidx.
implementation 'com.google.android.gms:play-services-ads:18.2.0'

Related

Fix Duplicate class in Android Studio Gradle dependency

I upgraded Android studio and gradle to the latest version and also updated all project dependencies to the latest verison. Today, I discovered an error that 2 dupclicate classes exist in gradle dependencies.
I got this error:
Duplicate class androidx.lifecycle.ViewModelLazy found in modules jetified-lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1) and lifecycle-viewmodel-2.5.1-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.1)
Duplicate class androidx.lifecycle.ViewTreeViewModelKt found in modules jetified-lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1) and lifecycle-viewmodel-2.5.1-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.1)
I tried to fix the error as suggested on the Android developer's official webiste, but can't seem to fix the error. The fix at here didn't work
I also tried some stackoverflow answers but it didn't fix it for me, some didn't suite my case.
This is my grade file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 33
buildToolsVersion "31.0.0"
defaultConfig {
applicationId "com.astrro.timely"
minSdkVersion 16
targetSdkVersion 33
multiDexEnabled true
versionCode 5
versionName "1.3.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
// changing the default apk build name from app-debug to proper name
applicationVariants.all {
variant ->
variant.outputs.each {
output ->
output.outputFileName = "TimeLY_v${variant.versionName}.apk"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'org.jetbrains:annotations:15.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.appcompat:appcompat-resources:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'androidx.fragment:fragment:1.5.5'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.preference:preference:1.2.0'
implementation "androidx.multidex:multidex:2.0.1"
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'com.google.code.gson:gson:2.8.8'
implementation 'com.kevalpatel2106:ringtonepicker:1.2'
implementation 'net.cachapa.expandablelayout:expandablelayout:2.9.2'
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.github.chrisbanes:PhotoView:2.0.0'
implementation 'com.airbnb.android:lottie:3.7.0'
implementation 'com.github.javiersantos:AppUpdater:2.7'
implementation 'me.zhanghai.android.materialprogressbar:library:1.1.6'
implementation ('com.wdullaer:materialdatetimepicker:4.2.3') {
exclude group: 'androidx.appcompat'
exclude group: 'androidx.recyclerview'
}
// debugImplementation because all these dependencies should only run in debug builds.
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
}
}

Gradle: How to get rid of duplicate classes error

I am getting the following duplicate class error when I run my Android application (I didn't include all the errors, but the duplicate errors are all coming from org.apache.commons.lang3.* and org.apache.commons.logging.*):
Duplicate class org.apache.commons.lang3.AnnotationUtils found in modules commons-lang3-3.4.jar (org.apache.commons:commons-lang3:3.4) and commons-lang3-3.7.jar (commons-lang3-3.7.jar)
Duplicate class org.apache.commons.lang3.AnnotationUtils$1 found in modules commons-lang3-3.4.jar (org.apache.commons:commons-lang3:3.4) and commons-lang3-3.7.jar (commons-lang3-3.7.jar)
...........
Duplicate class org.apache.commons.logging.impl.WeakHashtable found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
Duplicate class org.apache.commons.logging.impl.WeakHashtable$1 found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
Duplicate class org.apache.commons.logging.impl.WeakHashtable$Entry found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
Duplicate class org.apache.commons.logging.impl.WeakHashtable$Referenced found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
Duplicate class org.apache.commons.logging.impl.WeakHashtable$WeakKey found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
The error stems from this line of code in the gradle (I need to use the docx4j library because my app has to read in the contents of MS Word files selected by the user):
implementation "org.docx4j:docx4j:3.3.0"
Below is the complete code of my gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.diffchecker"
minSdkVersion 21
targetSdkVersion 29
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'io.github.java-diff-utils:java-diff-utils:4.5'
// For developers using AndroidX in their applications
implementation 'pub.devrel:easypermissions:3.0.0'
// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
implementation 'com.google.android.gms:play-services-ads:18.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation files('libs/commons-text-1.2.jar')
//Thanks for using https://jar-download.com
implementation 'org.webjars.bowergithub.telecomsante:pdf-viewer:2.2.0'
implementation 'com.tom_roush:pdfbox-android:1.8.10.1'
implementation 'com.bskim:maxheightscrollview:1.0.0#aar'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation files('libs/commons-logging-1.2.jar')
implementation files('libs/commons-lang3-3.7.jar')
// Docx4j is the library used to read Word documents
// https://mvnrepository.com/artifact/org.docx4j/docx4j
implementation "org.docx4j:docx4j:3.3.0"
}
How do I use the docx4j library in my gradle without running into duplicate conflicts with the commons-lang3 and commons-logging libraries?
I think I found a similar way of what we did at the beginning but this time with both exclusions.
Try this:
implementation ('org.docx4j:docx4j:3.3.0') {
['org.apache.commons','commons-logging'].each {
exclude group: "$it"
}
}
Erase local lib and put this on the gradle
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'

Failed to resolve com.androidAffected Modules and com.loopjAffected Modules in android

Failed to resolve com.androidAffected Modules and com.loopjAffected Modules in android.
I have checked with different links but I am confused.
Below is my App build file
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.XXX.XXXX"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:25.3.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:cardview v7:25.3.1'
implementation 'com.github.bumptech.glide:glide:3.6.1'
implementation 'me.dm7.barcodescanner:zxing:1.8.4'
implementation 'com.loopj.android:androidasync http:1.4.9'
implementation 'com.google.code.gson:gson:2.6.2'
implementation project(':merchantsdk')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
MerchantSDK is the aar file which is imported into this project and then I build giving below exceptions
Failed to resolve com.androidAffected Modules and
Failed to resolve com.loopjAffected Modules
Please help me on this.Thanks in advance.
If it is an AAR file you can keep it "lib" folder. You can add dependancy for this AAR in build.gradle as follows: -
implementation "packagename of aar:aar filename:#aar"
Replace package name and filename of AAR.
In build.gradle file of main project add this: -
allprojects {
repositories {
flatDir { dirs 'libs' }
}
}

Android studio showing error for firebase

I added firebase to android. It used to work fine but after pulling from github it shows the errors below:
Duplicate class com.google.android.gms.measurement.AppMeasurement found in modules classes.jar (com.google.android.gms:play-services-measurement-impl:17.2.0) and classes.jar (com.google.firebase:firebase-analytics-impl:10.0.1)
Duplicate class com.google.firebase.analytics.FirebaseAnalytics found in modules classes.jar (com.google.android.gms:play-services-measurement-api:17.2.0) and classes.jar (com.google.firebase:firebase-analytics-impl:10.0.1)
Duplicate class com.google.firebase.analytics.FirebaseAnalytics$Event found in modules classes.jar (com.google.android.gms:play-services-measurement-api:17.2.0) and classes.jar (com.google.firebase:firebase-analytics-impl:10.0.1)
Duplicate class com.google.firebase.analytics.FirebaseAnalytics$Param found in modules classes.jar (com.google.android.gms:play-services-measurement-api:17.2.0) and classes.jar (com.google.firebase:firebase-analytics-impl:10.0.1)
Duplicate class com.google.firebase.analytics.FirebaseAnalytics$UserProperty found in modules classes.jar (com.google.android.gms:play-services-measurement-api:17.2.0) and classes.jar (com.google.firebase:firebase-analytics-impl:10.0.1)
I tried deleting a few lines in gradle file but it doesn't seem to work.
My gradle file is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.example.agrismart"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.firebaseui:firebase-ui-database:6.0.2'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation'com.google.firebase:firebase-auth:18.1.0'
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.google.firebase:firebase-database:19.1.0'
implementation 'com.google.firebase:firebase-core:15.0.0'
implementation 'com.firebase:firebase-client-android:2.4.0'
implementation 'com.google.android.gms:play-services:10.0.1'
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
How can I resolve these problems?
Update the following:
implementation 'com.google.firebase:firebase-core:15.0.0'
into this:
implementation 'com.google.firebase:firebase-core:17.2.0'
Remove the following dependency:
implementation 'com.firebase:firebase-client-android:2.4.0'
Since this is used in the old firebase version.
According to the docs:
Note: Don't use the combined play-services target. It brings in dozens of libraries, bloating your application. Instead, specify only the specific Google Play services APIs your app uses.
Therefore remove:
implementation 'com.google.android.gms:play-services:10.0.1'
And use the specific service that you need in the app (Also use the versions found in the following page):
https://developers.google.com/android/guides/setup

How to get dependencies right for WorkManager and LocationServices?

I've been unable to get the dependencies right for an app that uses a worker for obtaining device locations. I suspect that I'm using the wrong location service with androidx, but I haven't found anything else. Here's my build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.exelor.laytrax"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha1'
implementation 'androidx.work:work-runtime:2.1.0-alpha02'
implementation 'com.google.android.material:material:1.1.0-alpha06'
implementation ('com.google.android.gms:play-services-location:16.0.0') {
exclude group: 'com.android.support'
}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
I'm excluding com.android.support to avoid build exceptions, such as
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.1.0-beta01) and classes.jar (com.android.support:support-compat:26.1.0)
At runtime in worker doWork():
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context.applicationContext)
throws this exception:
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArraySet;
at com.google.android.gms.common.api.internal.GoogleApiManager.<init>(Unknown Source:29)
My problem was not having a gradle.properties file containing
android.enableJetifier=true
android.useAndroidX=true

Categories

Resources