Error : Gradle: Duplicate files during packaging of APK - gradle

In my gradle android application when I run the application I got below error.
Error:Gradle: duplicate files during packaging of APK /home/WorkSpace/MyProject/app/build/outputs/apk/app-debug-unaligned.apk
Error:Gradle: Execution failed for task ':app:packageDebug'.
Duplicate files copied in APK META-INF/license.txt
File 1: /home/.gradle/caches/modules-2/files-2.1/org.springframework.android/spring-android-rest-template/1.0.1.RELEASE/e132d929bd181941f79b0d63edafb8a86ae6fd33/spring-android-rest-template-1.0.1.RELEASE.jar
File 2: /home/.gradle/caches/modules-2/files-2.1/org.springframework.android/spring-android-core/1.0.1.RELEASE/e68f0e8e4b636ee30c4de58953be38d9b72a5e3b/spring-android-core-1.0.1.RELEASE.jar
Below is my gradle file.
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.myproject.app"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/notice.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:gridlayout-v7:23.2.0'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:support-v4:23.2.0'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile 'org.apache.commons:commons-io:1.3.2'
compile 'joda-time:joda-time:2.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.2'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.android.support:design:23.2.0'
}
How I can solve this error? Please help me.

Add below lines in the packagingOptions and dependencies.
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
And in dependencies use as below.
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:cardview-v7:23.2.0'
compile 'com.android.support:recyclerview-v7:23.2.0'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile('org.springframework.android:spring-android-auth:1.0.1.RELEASE') {
exclude module: 'spring-core'
}
compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
compile 'org.apache.commons:commons-io:1.3.2'
compile 'joda-time:joda-time:2.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.2'
compile 'com.google.code.gson:gson:2.6.2'
}
I think this will solve your issues.

You already have this in your build file:
packagingOptions {
exclude 'META-INF/notice.txt'
}
You can just add license.txt there as well.

Related

use android gradle plugin 3.0.0 is slower than gradle plugin 2.3.3

when update the com.android.tools.build:gradle:2.2.3 to com.android.tools.build:gradle:3.0.0 , and I find the build-time is longer than 2.3.3 .
the root build.gradle is
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
the app/build.gradle is here , and all my dependencies use compile
apply plugin: 'com.android.application'
android {
sourceSets {
main {
...
}
}
dexOptions {
preDexLibraries = false
incremental true
javaMaxHeapSize "8g"
keepRuntimeAnnotatedClasses false
}
lintOptions {
abortOnError false
checkReleaseBuilds false
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'org/apache/commons/codec/language/bm/*.txt'
}
compileSdkVersion 24
buildToolsVersion "27.0.1"
}
defaultConfig {
applicationId "com.gg.kk"
minSdkVersion 15
targetSdkVersion 23
versionCode 512654
versionName "version"
multiDexEnabled true
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:multidex:1.0.1'
.... many dependencies ....
}
can anyone tell me why my build become slow?
Many different things can effect gradle build times.
To decrease this time you may want to try at least one of this solutions:
Check if Gradle Deamon is enabled. It should be enabled by default.
Keep your Gradle up-to-date (You are already good at this though)
you can go /gradle/wrapper/gradle-wrapper.properties and upgrade gradle version by changing distributionUrl
Increase your heapsize
org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Enable parallelism in your gradle.properties by adding org.gradle.parallel=true
Don't use dynamic dependencies like compile 'com.android.support:appcompat-v7:27.+'
File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle
Check the 'Offline work' under 'Global Gradle settings' Short term solution.
Check this https://developer.android.com/studio/build/optimize-your-build.html

Error:Failed to resolve: :HERE-sdk:

I was trying to create a new project which has two flavor, and the sdk is independent of app.
So I modified these build.gradle files like below:
For the android lib module: sdk:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
en
zh
}
publishNonDefault true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
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:25.3.1'
testCompile 'junit:junit:4.12'
compile(name: 'HERE-sdk', ext: 'aar')
}
repositories {
flatDir {//HERE SDK
dirs 'libs'
}
mavenCentral()
}
For the android app module: app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "cn.hudplay.testgradle"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
en
zh
}
}
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.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
enCompile project(path:":sdk",configuration:"enRelease")
zhCompile project(path:":sdk",configuration:"zhRelease")
//compile(name: 'HERE-sdk', ext: 'aar')//this seems to be workaround if un-blocked
}
repositories {
flatDir {//HERE SDK
dirs 'libs'
}
mavenCentral()
}
Then it told me Failed to resolve ::HERE-sdk:, and no other reasons..
I tried to move Here-sdk related gradle code to app's build.gradle, no more wrong.But I do need it in the sdk module..
What should I do..Anyone could help me?
Workaround used temporarily :
compile HERE-sdk for both app and sdk, no more wrong again. But I still feel something not right there...
Hi HERE updated the documentation maybe that help you with the issue. I had the same problem too and it worked out following these steps here:
https://github.com/heremaps/here-android-sdk-examples
What worked for me using the code above as an example (and if someone looks for a solution that works) was this approach:
copy the "\HERE-sdk\libsHERE-sdk.aar" file (dowloaded from your account on Here) to your \App\libs folder, then:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar','*.aar'])
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:25.3.1'
testCompile 'junit:junit:4.12'
//compile(name: 'HERE-sdk', ext: 'aar')
}
NOTE: what changes was only 2 lines:
COMMENTED -> //compile(name: 'HERE-sdk', ext: 'aar') and ADDED -> '.aar' extension to array scope here -> compile fileTree(dir: 'libs', include: ['.jar','*.aar']).
OBS: I know thi's an old question and maybe someone out there in this outside world must have already answered it, but I leave here my humble contribution. If someone wants to employ me, I'm looking for a job hehehe
I got the aar compile without error by adding by adding it from
Open Module Settings
Step 1 , add the aar into app/libs folder
Step 2, Right Click App and negative to Open Module Settings
Step 3, Click on dependencies on the left and then click on the + under Declared Dependencies
Step 4, Select the libs/HERE-sdk.aar from the drop down and then click OK

Gradle error :app:transformClassesWithJarMergingForDebug FAILED

i tring to run my app but the gradle give me this error
:app:transformClassesWithJarMergingForDebug FAILED Error:Execution
failed for task ':app:transformClassesWithJarMergingForDebug'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: javax/inject/Inject.class
Information:BUILD FAILED
this my AndroidMainifist.xml
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
defaultConfig {
applicationId "com.example.mohamed.movieapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'org.glassfish.jersey.core:jersey-client:2.22.1'
compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.22.1'
}
repositories {
maven { url 'java.lang.android.support.v7.app.WindowDecoreActionBar' }
}
can any one help please
The duplication comes from jersey client. It depends on both org.glassfish.hk2.external:javax.inject and javax.inject:javax.inject.
So, you need to exclude one of them:
dependencies {
...
compile ('org.glassfish.jersey.core:jersey-client:2.22.1') {
exclude group: 'javax.inject', module: 'javax.inject'
}
...
}
I try to use org.glassfish.jersey.media:jersey-media-moxy in my project and had to apply the same exclusion for that dependency too - likely it will be the same for org.glassfish.jersey.media:jersey-media-json-jackson.

Problen add dependency android studio

I posted my library in Artifactory
https://imagizer.imageshack.us/v2/806x253q90/631/kkK1Yn.png
this is my Gradle
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle',
version: '3.0.1')
}
}
repositories {
jcenter()
mavenCentral()
mavenLocal()
maven {
url 'http://myartifactory:8081/artifactory/gradle-local'
}
}
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'com.android.library'
apply plugin: 'android-apt'
def AAVersion = '3.2'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
}
}
dependencies {
compile fileTree(include: ['*.jar', '*.so'], dir: 'libs')
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.android.support:support-v4:21.0.+'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.github.chrisbanes.photoview:library:1.2.3'
compile 'it.sephiroth.android.exif:library:+'
compile 'com.joanzapata.android:android-iconify:1.0.8'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.afollestad:material-dialogs:0.6.0'
compile 'com.github.lzyzsd:circleprogress:1.0.1#aar'
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
compile(group: 'com.mylibrary.android.common', name: 'android.common', version: '1.0.0', ext: 'aar')
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'com.mypackage.library'
}
}
I get this error
https://imagizer.imageshack.us/v2/519x72q90/540/8ojerK.png
Gradle I found that running the route using the url wrong use "/" instead of "."
Try to find
...8081/artifactory/repo/com/mypackage/android/common/android.common/1.0.0/android.common-1.0.0.aar
but should be
...8081/artifactory/repo/com.mypackage.android.common/android.common/1.0.0/android.common-1.0.0.aar
I'm doing wrong, wrong as was published in the artefactory
Your artifact is not complaint to the standard Maven layout, in which the groupId should be separated by /, not by ..
It's not a big deal, Gradle can handle it easly, you just can't declare the repository as maven, but as ivy instead.
Actually, it will be easier to use the artifactory plugin for resolution, it also supports both Maven and Ivy layouts.
Also, please remember to set the repository in Artifactory to be with correct layout (not Maven2, probably Ivy).
And frankly, I think maybe it worth redeploying the artifact under Maven layout, it will make your life easier.

Android Studio - Gradle Errors NoClassDefFound when including libraries

This has been happening too often and I am not sure about the reason. Would really love to know the reason behind it.
My app's gradle looks like this
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':Q-municate_core')
compile "info.hoang8f:fbutton:${rootProject.FButtonVersion}"
compile "com.google.android.gms:play-services:${rootProject.googlePlayServicesVersion}"
compile "com.soundcloud.android:android-crop:${rootProject.cropperVersion}#aar"
compile group: 'com.squareup.picasso', name: 'picasso', version: "${rootProject.picassoVersion}"
compile "com.google.guava:guava:${rootProject.guavaVersion}"
compile "com.makeramen:roundedimageview-apklib:${rootProject.roundedImageViewVersion}"
compile "se.emilsjolander:stickylistheaders:${rootProject.stickyListHeadersVersion}"
compile ('de.keyboardsurfer.android.widget:crouton:1.8.4#aar'){
exclude group: 'com.google.android', module: 'support-v4'
}
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.easing:library:1.0.1#aar'
compile 'com.daimajia.androidanimations:library:1.1.3#aar'
compile ('it.sephiroth.android.library:pagerslidingtabstrip:1.1.1'){
exclude group: 'com.google.android', module: 'support-v4'
exclude group: 'com.google.android', module: 'support-v7'
}
}
This builds well, but my app crashes with " java.lang.NoClassDefFoundError: com.mface.mcall.utils.Consts"and its on a completely different class. I have not even used the library that I have included. Just the "compile" statement in the app gradle and the app starts crashing.
For example this:
607-5607/mcall.mface.com.mcallandroidstudio E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.mface.mcall.utils.Consts
at com.mface.mcall.utils.ImageUtils.getImageLoaderConfiguration(ImageUtils.java:60)
at com.mface.mcall.App.initImageLoader(App.java:32)
at com.mface.mcall.App.initApplication(App.java:42)
at com.mface.mcall.App.onCreate(App.java:27)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1070)
at com.lbe.security.service.core.client.b.x.callApplicationOnCreate(Unknown Source)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4708)
at android.app.ActivityThread.access$1300(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5469)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:857)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
If I remove the following lines all is well. I have no idea why; its good to know the reason so i can work around it. This has been happening often with different libraries for example aviary.
compile ('it.sephiroth.android.library:pagerslidingtabstrip:1.1.1'){
exclude group: 'com.google.android', module: 'support-v4'
exclude group: 'com.google.android', module: 'support-v7'
}
I have tried the following combinations too
compile 'it.sephiroth.android.library:pagerslidingtabstrip:1.1.1'
compile ('it.sephiroth.android.library:pagerslidingtabstrip:1.1.1'){
exclude group: 'com.google.android', module: 'support-v4'
}
There are no gradle errors but the app crashes.
May be am missing something here?
The full app gradle
apply plugin: 'com.android.application'
repositories {
jcenter()
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "mcall.mface.com.mcallandroidstudio"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
signingConfig signingConfigs.debug
zipAlignEnabled true
}
dexOptions{
// jumboMode = true
preDexLibraries = false
incremental true
javaMaxHeapSize "4g"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':Q-municate_core')
compile "info.hoang8f:fbutton:${rootProject.FButtonVersion}"
compile "com.google.android.gms:play-services:${rootProject.googlePlayServicesVersion}"
compile "com.soundcloud.android:android-crop:${rootProject.cropperVersion}#aar"
compile "com.nostra13.universalimageloader:universal-image-loader:${rootProject.universalImageLoaderVersion}"
compile group: 'com.squareup.picasso', name: 'picasso', version: "${rootProject.picassoVersion}"
compile "com.google.guava:guava:${rootProject.guavaVersion}"
compile "com.makeramen:roundedimageview-apklib:${rootProject.roundedImageViewVersion}"
compile "se.emilsjolander:stickylistheaders:${rootProject.stickyListHeadersVersion}"
compile ('de.keyboardsurfer.android.widget:crouton:1.8.4#aar'){
exclude group: 'com.google.android', module: 'support-v4'
}
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.easing:library:1.0.1#aar'
compile 'com.daimajia.androidanimations:library:1.1.3#aar'
compile ('it.sephiroth.android.library:pagerslidingtabstrip:1.1.1'){
exclude group: 'com.google.android', module: 'support-v4'
exclude group: 'com.google.android', module: 'support-v7'
}
}

Resources