Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes? - gradle

I am developing a news app but I am getting following errors in from gradle console
(androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.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-compat:28.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-compat:28.0.0)
Duplicate class android.support.v4.graphics.drawable.IconCompatParcelizer found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.core.graphics.drawable.IconCompatParcelizer found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.core.internal.package-info found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.versionedparcelable.CustomVersionedParcelable found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.NonParcelField found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelField found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelImpl found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelImpl$1 found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelUtils found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel$1 found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel$ParcelException found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelParcel found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream$FieldBuffer found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream$InputBuffer found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelable found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelize found in modules class
below my app.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.myapplication"
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 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
implementation 'com.github.bassaer:chatmessageview:2.0.1'
implementation 'com.google.code.gson:gson:2.8.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

You can add below 2 lines into your gradle.properties file:
android.useAndroidX=true
android.enableJetifier=true
Note to check, to not repeat any line that already exists (and ensure existing are true).
Details:
If you want to use androidx-namespaced libraries in a new project, you need to set the compile SDK to Android 9.0 (API level 28) or higher but below "API level 31", and set both of the mentioned Android Gradle plugin flags to true.
android.useAndroidX: When this flag is set to true, the Android plugin uses the appropriate AndroidX library instead of a Support Library. The flag is false by default if it is not specified.
android.enableJetifier: When this flag is set to true, the Android plugin automatically migrates existing third-party libraries to use AndroidX dependencies by rewriting their binaries. The flag is false by default if it is not specified.

Adding these lines to gradle.properties file solves "Duplicate Class" errors:
android.useAndroidX=true
android.enableJetifier=true
However, this generated new errors in my project:
"package android.support.annotation does not exist"
"cannot find symbol class Fragment"
"package android.support.v4.content does not exist"
"Program type already present"
etc.
However, the refactoring worked for me:
Migrate your project to AndroidX by selecting Refactor >
"Migrate to AndroidX" from the menu bar.
Select Build > Clean project
Restore Android Studio
Now, my project (Match4app) works with AndroidX and I was able to publish it in the PlayStore without any issues.
Comment: This task also allowed me to upgrade all other libraries that depend on AndroidX (i.e. com.firebaseui:firebase-ui-auth:6.0.2, com.google.android.gms:play-services-games:18.0.1, com.google.android.gms:play-services-auth:17.0.0, com.google.android.gms:play-services-ads:18.2.0, etc.).

I run into something like this, and below is based on my other answer:
Your project (or one of its sub-projects) is referring to a dependency using the + plus-sign at its end, like com.google.firebase:firebase-auth:+, which means, use any higher version when possible, and that newer version is no longer using android.support libraries and instead is using androidx; to fix this issue follow the below steps.
Steps:
Ensure the ANDROID_HOME environment-variable is set, and then, open a console window (like git-bash, because it keeps the whole command output), and cd into your project's android directory (for Ionic projects it should be platforms/android).
First, List all dependencies by running below (in git-bash):
./gradlew :app:dependencies
Copy the result into your preferred text-editor, and search for androidx.
If you found something follow below steps, else you are done! (and you do not need to repeat these steps).
Scroll up until you see -> at the end of any line, like for example 16.0.8 -> 19.0.0 or + -> 19.0.0, which both mean that the version was auto-resolved (to something higher than specified by you because of +).
So, set the version down manually:
When possible, in your project (or sub-project) find and replace the + sign with a specific version.
Or, force a specific version of the dependencies like mentioned below (after copying dependency id, which you found in previous-step, of course).
At last, repeat above steps (but instead of step one just clear the console).
To Force specific version of the dependencies add to your root build.gradle file something like below (which is what worked for me) but of course edit below and add your own rules (because these might not work for your case):
allprojects {
// ...
configurations.all {
resolutionStrategy {
force 'com.google.firebase:firebase-common:17.0.0'
force 'com.google.android.gms:play-services-basement:16.2.0'
force 'com.google.firebase:firebase-iid:16.0.0'
force 'com.google.firebase:firebase-auth:17.0.0'
}
}
}

I was having this same issue and this is how i resolved it
Expand Graddle Scripts and then Go to graddle.properties and add these 2 lines
Note: Maybe the first line android.useAndroidX=true is already there so do not repeat it.
android.useAndroidX=true
android.enableJetifier=true

Just set your Gradle properties this way

Just add these 2 lines in gradle.properties file,
android.useAndroidX=true
android.enableJetifier=true
Now your project will not have any duplicate class error and will work fine,
Thank you

Taken from here. AndroidX is the open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack.

Like #Ahmed says, the solution is to implement AndroidX, it works for me. However, it isn´t an easy way and it requires a bit of pacience... These are the steps that I did:
First, is very important that you do all this changes in a different branch or you make a backup of your project.
You need to have the Android Gradle Plugin Version 3.5.1. So, in build.gradle set: dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
...
Migrate to AndroidX using Android Studio Tool : Refactor --> Migrate to AndroidX...
When it finishes, it has done all pertinents modification, but posibly you can´t deploy the project correctly because you find any errors. These are the problems that I found and the solutions:
If you use Kotlin, in build.gradle set: buildscript {
ext.kotlin_version = '1.3.10'
...
and compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
If you use destination method, add "file" parameter: destination file("$reportsDir/checkstyle/checkstyle.xml")
If you use Butterknife, use 10.0.0 version

I was having this same issue and this is how i resolved it
android.enableJetifier=true

Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
...
}
dependencies {
implementation "androidx.multidex:multidex:2.0.1"
}

Top-Master's answer only work if you can downgrade to a specific version. In my case, I have a React Native app and one of the libraries I was using had migrated to AndroidX. The previous version had issues with iOS, so I had to use the most recent version. What I had to do was:
Migrate the Android project to AndroidX (Refactor > Migrate to AndroidX).
Use jetify to convert node_module dependencies to AndroidX. For React Native 0.60 and above, jetify is run automatically, so you don't have to install this library.

android.nonTransitiveRClass=true
add this on gradle properties

Related

Gradle bundle import to module

I am trying to import base to a project, but I get an error.
settings.gradle
enableFeaturePreview('VERSION_CATALOGS')
dependencyResolutionManagement {
versionCatalogs {
libs {
// https://mvnrepository.com/artifact/org.springframework/spring-core
version('spring', '5.3.14')
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter
version('spring-boot', '2.6.2')
alias('spring-core').to('org.springframework', 'spring-core').versionRef('spring')
alias('spring-context').to('org.springframework', 'spring-context').versionRef('spring')
alias('spring-boot').to('org.springframework.boot', 'spring-boot-starter').versionRef('spring-boot')
bundle('base', ['spring-core', 'spring-context'])
}
}
}
module's build.gradle
dependencies {
implementation(libs.base)
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
refreshing gradle will produce the following error
Could not get unknown property 'base' for extension 'libs' of type org.gradle.accessors.dm.LibrariesForLibs.
How to import libs.base correctly?
The correct way of referencing a bundle from a version catalog is
libs.bundles.base
Similarly for plugins use:
libs.plugins.yourplugin
It is also possible to reference versions, but this works slightly different. For example:
implementation group: "org.springframework.boot", name: "spring-boot-starter-web-services", version: libs.versions.spring.boot.get()
This assumes a version definition in your catalog which looks like:
version('spring-boot', '2.6.7')
Note that bundles, versions and plugins are all spelled as plural.
Also note that as of Gradle 7 version catalogs are no longer a feature preview. They are available by default. Also, the notation for libraries has changed slightly. You have to use library instead of alias.
See Gradle's support site for more information.

Problem with connecting robovm-rt library

When I connect the new library robovm-rt, gradle swears that I already have several such classes in another library:
Duplicate class org.apache.commons.logging.Log found in modules robovm-rt-2.3.5 (com.mobidevelop.robovm:robovm-rt:2.3.5) and spring-jcl-5.1.2.RELEASE (org.springframework:spring-jcl:5.1.2.RELEASE)
Duplicate class org.apache.commons.logging.LogFactory found in modules robovm-rt-2.3.5 (com.mobidevelop.robovm:robovm-rt:2.3.5) and spring-jcl-5.1.2.RELEASE (org.springframework:spring-jcl:5.1.2.RELEASE)
Duplicate class org.apache.commons.logging.LogFactory$1 found in modules robovm-rt-2.3.5 (com.mobidevelop.robovm:robovm-rt:2.3.5) and spring-jcl-5.1.2.RELEASE (org.springframework:spring-jcl:5.1.2.RELEASE)
Duplicate class org.apache.commons.logging.impl.NoOpLog found in modules robovm-rt-2.3.5 (com.mobidevelop.robovm:robovm-rt:2.3.5) and spring-jcl-5.1.2.RELEASE (org.springframework:spring-jcl:5.1.2.RELEASE)
Duplicate class org.apache.commons.logging.impl.SimpleLog found in modules robovm-rt-2.3.5 (com.mobidevelop.robovm:robovm-rt:2.3.5) and spring-jcl-5.1.2.RELEASE (org.springframework:spring-jcl:5.1.2.RELEASE)
I tried to exclude these Apache classes from library in different ways, but none of them work, the problem remains. Not working options:
Option 1:
implementation (group: 'com.mobidevelop.robovm', name: 'robovm-rt', version: '2.3.5') {
exclude group: 'commons-logging', module: 'commons-logging'
}
Option 2:
implementation (group: 'com.mobidevelop.robovm', name: 'robovm-rt', version: '2.3.5') {
exclude group: 'org.apache.commons', module: 'commons-logging'
}
What is the correct way to exclude these classes so that they are not duplicated?
Answering my question, I just excluded the module not from the first library, but from the second one - spring-boot-starter-websocket
implementation('org.springframework.boot:spring-boot-starter-websocket') {
exclude group: 'org.springframework', module: 'spring-jcl'
}

Duplicates class found in modules

Duplicate class javax.el.ArrayELResolver found in modules jsp-api-2.1-6.1.14.jar (org.mortbay.jetty:jsp-api-2.1:6.1.14) and jsp-api-2.1.jar (javax.servlet.jsp:jsp-api:2.1)
Duplicate class javax.el.BeanELResolver found in modules jsp-api-2.1-6.1.14.jar (org.mortbay.jetty:jsp-api-2.1:6.1.14) and jsp-api-2.1.jar (javax.servlet.jsp:jsp-api:2.1)
Duplicate class javax.el.BeanELResolver$BeanProperties found in modules jsp-api-2.1-6.1.14.jar (org.mortbay.jetty:jsp-api-2.1:6.1.14) and jsp-api-2.1.jar (javax.servlet.jsp:jsp-api:2.1)
Duplicate class javax.el.BeanELResolver$BeanProperty found in modules jsp-api-2.1-6.1.14.jar (org.mortbay.jetty:jsp-api-2.1:6.1.14) and jsp-api-2.1.jar (javax.servlet.jsp:jsp-api:2.1)
build.gradle:
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//The dependencies below are for a library i'm including after which i get the error
compile "org.bouncycastle:bcprov-jdk15on:1.54"
compile "org.apache.pdfbox:pdfbox:[2.0.15,)"
compile "org.mapdb:mapdb:3.0.4"
compile "com.google.guava:guava:19.0"
compile "com.carrotsearch:java-sizeof:0.0.5"
compile "org.apache.hadoop:hadoop-common:2.7.1"
compile "com.amazonaws:aws-java-sdk:1.11.19"
compile "org.apache.hadoop:hadoop-core:1.2.1"
compile "org.apache.lucene:lucene-analyzers-common:6.1.0"
compile "org.apache.poi:poi-scratchpad:3.15-beta1"
compile "org.apache.poi:poi:3.15-beta1"
compile "org.apache.poi:poi-ooxml:3.15-beta1"
I've read on other threads on stack overflow about using the exclude:group in your build.gradle it's just that i seem to be doing something wrong. Can someone please tell me (i) how to add the exlude statement to the fix the error above instead of giving some generic example (ii) how would i manually remove the modules causing the duplication? (i've tried by removing them from the gradle cache folder even deleted the .gradle folder but it keeps rebuilding it). Any suggestion would be appreciated
Firstly compile should be replaced with implementation as it is deprecated. (i) this link provides a good explanation: https://discuss.gradle.org/t/how-to-exclude-transitive-dependency/2119/2
(ii) I searched a lot to find a workable way to go about doing this and most of the suggested methods failed. What actually worked was the following example:
Note: duplicate definition of program class
Problem: hadoop-common and hadoop-core shared numerous common classes that were causing conflict
Solution:
I managed to solve it by manually removing the duplicate .class files in the jar file. (i.e.
(i) changing the extension from jar to zip
(ii) extract it
(iii) remove the .class files or the entire folder if it contains all the duplicate files
(iv) change the extension from zip back to jar
Note do not extract the jar files with Winrar and try to zip them back, for some odd reason that doesn't work. I had to
navigate to the Gradle cache: C->Users-> Username -> .gradle -> caches -> modules-2 -> files-2 -> org.apache.hadoop ->
find either hadoop-common or hadoop-core and apply the above to delete the folders with duplicate classes

Having issues with setting up LeakCanary

LeakCanary setup issues
I am trying to setup via https://github.com/square/leakcanary.
in gradle, I updated
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-2'
but getting 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-compat:28.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-compat:28.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-compat:28.0.0)
Duplicate class android.support.v4.graphics.drawable.IconCompatParcelizer found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Per stackoverflow
Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes?
updated gradle.properties but now getting a error where nothing related to Android is compiling
org.gradle.execution.MultipleBuildFailures: Build completed with 1 failures.
at org.gradle.initialization.DefaultGradleLauncher$ExecuteTasks.run(DefaultGradleLauncher.java:386)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:301)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:293)
at
This is a known issue that was fixed in the beta release, you should update.

how do you set up log4j for a gradle project with modules?

how do you set up log4j for a gradle project with modules?
I have a project set up like the following:
project root
build.gradle
gradle.properties
settings.gradle
// this root project does:
// include CommonModule
// includeBuild <all composite modules within module folder>
---CommonModule
------build.gradle
------gradle.properties
------settings.gradle
------src/main/groovy/...<common-code>
------src/main/resources/log4j2.xml
Modules
---OtherModule-1
// this is a gradle composite module
// it also includes the common module
------build.gradle
------gradle.properties
------settings.gradle
------src/main/groovy/...module1-code
------src/main/resources/log4j2.xml
---OtherModule-2
// this is a gradle composite module
// it also includes the common module
------build.gradle
------gradle.properties
------settings.gradle
------src/main/groovy/...module2-code
------src/main/resources/log4j2.xml
As shown above, we have a common gradle module and a module folder that contains module related composite module, each which depends on the included CommonModule
(core common code goes in common, the composite modules each contain code that extends common stuff
My question is hopefully simple:
where do I configure my log4j module?
e.g. can I put it in the common include module ?
or does each composite module need to have their own log4j xml?
I went and asked this same question on the gradle forums https://discuss.gradle.org/t/how-do-you-set-up-log4j-for-a-gradle-project-with-modules/28262 and got an answer -
Common module doesn’t include a log4j config Individual library
modules don’t include a log4j config either Top-level application
module does include a log4j config, but doesn’t put it on the
classpath (I set aside a src/[configuration]/config directory just
for that)
Launcher for the application itself (whether from a release
build or from a JavaExec in the Gradle build) specifies where the
log4j config is loaded from, so that you can’t get accidentally
poisoned by other configs on the classpath
you can define the xml location by providing a -D flag in the gradle properties file e.g. -Dlog4j.configurationFile=
also -Dlog4j.debug might help to track down issues

Resources