Getting a DuplicateFileException when adding a module to my Android app - java

I am trying to add the Paho MQTT service (and client) to my Android app, but I am running into a com.android.builder.packaging.DuplicateFileException runtime exception, when running my app.
It appears to me that everything is setup right, and when I clean/build, I get no errors at all.
My app build.gradle file looks like:
dependencies {
compile fileTree(dir: '../org.eclipse.paho.android/service/libs', excludes: ["org.eclipse.paho.client.mqttv3-1.1.0.jar"], include: '')
compile fileTree(include: ['*.jar'], dir: 'libs', excludes: ["*org.eclipse.paho.client.mqttv3-1.1.0.jar"])
compile (project(':org.eclipse.paho.android.service')){
// exclude module: "org.eclipse.paho.client.mqttv3"
transitive=true
}
// Uncommenting the below (when commenting out the above) WORKS.
// However, I need to be able to alter the source, to I do need to be able
// to import the Paho MQTT service as a module to my app.
// compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
// compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
...
However, when I run, I receive a DuplicateFileException exception:
* What went wrong:
Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK org/eclipse/paho/client/mqttv3/internal/nls/messages_zh_TW.properties
File1: /Users/me/.gradle/caches/modules-2/files-2.1/org.eclipse.paho/org.eclipse.paho.client.mqttv3/1.1.0/89d827ffa47c241f2627421ef1d6c7a8c207a341/org.eclipse.paho.client.mqttv3-1.1.0.jar
File2: /Users/me/myapp/code/myapp-android/org.eclipse.paho.android.service/build/intermediates/bundles/default/classes.jar
My file hierarchy looks like:
which appears ok to me.
Does anyone have any pointers on how to resolve this? The error claims that org.eclipse.paho.client.mqttv3-1.1.0.jar is clashing with classes.jar, but my gradle file explicitly excludes the compiling of org.eclipse.paho.client.mqttv3-1.1.0.jar (or so I think it does).
I'm at a loss here; any pointers are welcomed.

This happens because two of your dependencies are using Eclipse Paho MQTT library.
Following should fix the issue
Add following to your app level gradle file
packagingOptions {
exclude 'META-INF/ECLIPSE_.SF'
exclude 'META-INF/ECLIPSE_.RSA'
}
compile("one-of-your-dependency:1.0.0"){
exclude group: 'org.eclipse.paho'
}

Related

Error: json defines classes that conflict with classes now provided by Android

I got the following error when doing a release build on Android Studio 3
Error:Error: json defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar. [DuplicatePlatformClasses]
The following is my dependencies:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.1'
compile 'com.android.support:support-v4:26.0.1'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
compile 'com.evernote:android-job:1.2.0'
compile 'com.amitshekhar.android:android-networking:1.0.0'
compile 'com.facebook.stetho:stetho:1.5.0'
compile "me.tatarka.redux:redux-core:0.10"
compile "me.tatarka.redux:redux-android:0.10"
compile "me.tatarka.redux:redux-android-lifecycle:0.10"
compile "me.tatarka.redux:redux-thunk:0.10"
annotationProcessor "org.immutables:value:2.5.5" // <-- for annotation processor
provided "org.immutables:value:2.5.5" // for annotations
provided "org.immutables:builder:2.5.5" // for annotations
compile "me.tatarka.redux:redux-monitor:0.10"
testCompile 'junit:junit:4.12'
}
It's saying json but I can't find which of the the above dependencies is causing the problem.
Here is what I get when I run
gradlew assembleRelease
Task :app:lintVitalRelease
/Users/kruyvanna/Projects/key/HappyKey_Android2/app/build.gradle: Error: json defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar. [DuplicatePlatformClasses]
Explanation for issues of type "DuplicatePlatformClasses":
There are a number of libraries that duplicate not just functionality of
the Android platform but using the exact same class names as the ones
provided in Android -- for example the apache http classes. This can lead
to unexpected crashes.
To solve this, you need to either find a newer version of the library which
no longer has this problem, or to repackage the library (and all of its
dependencies) using something like the jarjar tool, or finally, rewriting
the code to use different APIs (for example, for http code, consider using
HttpUrlConnection or a library like okhttp.)
1 errors, 0 warnings
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:lintVitalRelease'.
Lint found fatal errors while assembling a release target.
To proceed, either fix the issues identified by lint, or modify your build script as follows:
...
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
...
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Get more help at https://help.gradle.org
I fixed it by adding:
configurations {
all {
exclude group: 'org.json', module: 'json'
}
}
to module gradle file, no removal of dependencies was needed.
Add this in your app/build.gradle
configurations {
all {
exclude module: 'commons-logging'
}
}
Go to the commandline and look at the dependency tree. This will give you a list of everything that your app uses:
./gradlew dependencies
I added dependencies one by one and found this one causing the error
compile "me.tatarka.redux:redux-monitor:0.10"

Manifest Merger Error - Different versions of support library

I'm adding the stepper indicator library - https://github.com/badoualy/stepper-indicator - to my project. To do so, I added jitpack to my project gradle file and the stepper-indicator library to my app gradle file. However, I'm getting the following build error:
Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION#value value=(25.4.0) from [com.android.support:preference-v14:25.4.0] AndroidManifest.xml:25:13-35
is also present at [com.android.support:appcompat-v7:26.0.0-beta2] AndroidManifest.xml:28:13-41 value=(26.0.0-beta2).
Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:23:9-25:38 to override.
The other dependencies in my app include:
compile 'com.android.support:preference-v7:25.4.0'
compile 'com.android.support:preference-v14:25.4.0'
compile 'eu.davidea:flexible-adapter:5.0.0-rc2'
compile 'com.android.support:appcompat-v7:25.4.0'
compile 'com.android.support:recyclerview-v7:25.4.0'
compile 'com.android.support:cardview-v7:25.4.0'
compile 'com.android.support:support-v4:25.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.android.gms:play-services-gcm:11.0.2'
compile 'com.google.android.gms:play-services-auth:11.0.2'
compile 'com.google.android.gms:play-services-ads:11.0.2'
compile 'com.google.firebase:firebase-messaging:11.0.2'
compile 'com.google.firebase:firebase-auth:11.0.2'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
Without upgrading all of the android support libraries to an alpha version, is there a solution to this problem?
In build.gradle you can exclude conflicting dependencies. For example:
compile ('com.github.badoualy:stepper-indicator:1.0.7'){
exclude group: 'com.android.support', module: 'appcompat-v7'
}
To inspect dependencies, you can use Gradle toolbar in Android Studio -> application module -> tasks -> android -> androidDependencies
Update:

Yet another zip duplicate entry

I've use Kryo libs and it's pretty good. However, when I want to create a signed APK, it keeps failing to build because of the error:
Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
java.io.IOException: Can't write [C:\AndroidProjects\App\app\build\intermediates\transforms\proguard\release\jars\3\1f\main.jar] (Can't read [C:\AndroidProjects\App\app\importLibs\minlog-1.3.0.jar(;;;;;;**.class)] (Duplicate zip entry [minlog-1.3.0.jar:com/esotericsoftware/minlog/Log$Logger.class]))
build gradle (module app)
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-ads:9.4.0'
compile 'com.android.support:design:24.1.1'
compile 'com.google.android.gms:play-services:9.4.0'
compile 'com.google.android.gms:play-services-auth:9.4.0'
compile 'com.google.android.gms:play-services-gcm:9.4.0'
compile files('importLibs/kryo-2.23.0.jar')
compile files('importLibs/minlog-1.3.0.jar')
compile files('importLibs/objenesis-2.1.jar')
compile files('importLibs/reflectasm-1.10.1-shaded.jar')
}
proguard file
proguard-rules.pro
-dontwarn com.esotericsoftware.**
-dontwarn org.objenesis.**
-keep class com.esotericsoftware.**{*;}
What exactly do I need to write to make it work?
There can be case where a library is part of another library that you have imported in your project
Check the lib dependency tree using following command
For Android, use this line
gradle app:dependencies
or if you have a gradle wrapper:
./gradlew app:dependencies
Or else there is a less popular Android studio plugin called GradleView that can give you entire lib tree used in your application and also where it might be clashing with some other library.
I had similar problem, and I solved it with just including retrofit:
'com.squareup.retrofit:retrofit:1.9.0'
I hope it will help you too.

include gturri android xml rpc library in android project

i need to include this library but i ve got several problems.
project library on github
i tried to follow the instructions on the github project but they did not work for me. I have to include the library in android studio.
i tried to:
1) copy the whole code in my project but i had a lot of conflicts about package, and, once solved, i began to have problems about lacks of functions not defined
2) i tried to use mvn install command, but it did not work, something like 100 errors displayed
3) i tried to open that project with intelliJ and then i tried to export jar file, but intelliJ told that it s an android project
does anyone have any idea about the procedure to include this library?
Thanks a lot in advance
you could give Jitpack a try. At least it worked for me..
See https://jitpack.io/
In essence: add following code to the build.gradle file of your project:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
and add the following line to the dependencies section in the build.gradle file of your app:
`compile 'com.github.gturri:aXMLRPC:master-SNAPSHOT'`
here is what the dependency section looks like for me:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.github.gturri:aXMLRPC:master-SNAPSHOT'
compile 'com.android.support:support-v4:23.4.0'
}

Cannot resolve symbol in Android

I am working on integrating twitter with my android application, I am using twitter4j library to do this.
I have added the twitter4j library files and put it in a folder libs and added compile dependency in build.gradle inside app folder
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.google.android.gms:play-services:6.5.+'
compile 'org.twitter4j:twitter4j-core:4.0.2'
}
but I am still not able to use this library
In the Gradle drawer to the right, could you hit the refresh button to see if that helps? Also, the dependencies block that you've pasted above doesn't contain a directive for twitter4j - did you happen to simply miss it out in the question, or is it really missing from the gradle.build file?
Try to add this at "repositories" on your buildscript.
repositories { mavenCentral() }

Categories