49

I got this error Duplicate files during packaging of APK app-debug-unaligned.apk when put 2 jar files :

  • httpclient-4.3.5.jar

  • httpmime-4.3.5.jar

    into the libs folder after Sync with Gradle and Run.

If user 1 jar file - httpmime-4.3.5.jar, I will not get this error.

Please help me how to avoid this error & still can use 2 jar files in above also,

Thanks,

p/s : I use Android Studio version 0.8.6.

Error Detail

Error:duplicate files during packaging of APK ...\app\build\outputs\apk\app-debug-unaligned.apk Path in archive: META-INF/DEPENDENCIES Origin 1: ...\app\libs\httpclient-4.3.5.jar Origin 2: ...\app\libs\httpmime-4.3.5.jar

build.gradle

android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
    applicationId 'com.app'
    minSdkVersion 9
    targetSdkVersion 20
    versionCode 1
    versionName '1.0'
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
}
packagingOptions {
    exclude 'META-INF/LICENSE.txt'
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:20.0.0'
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.google.android.gms:play-services:5.2.08'
compile 'com.viewpagerindicator:library:2.4.1@aar'
compile 'de.hdodenhof:circleimageview:1.2.0'
compile files('libs/httpmime-4.3.5.jar')
}

UPDATE I changed from compile files('libs/httpmime-4.3.5.jar') to use Maven Link. I got same error again after put 2 maven link together:

    compile 'org.apache.httpcomponents:httpmime:4.4-alpha1'
compile 'org.apache.httpcomponents:httpcore:4.4-alpha1'

This is the warning

Warning:Dependency org.apache.httpcomponents:httpclient:4.4-alpha1 is ignored for debug as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages

Warning:Dependency org.apache.httpcomponents:httpclient:4.4-alpha1 is ignored for release as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jar to change the class packages

Please help me fix.

SOULITION I know good answer now by addding these lines will fix Duplicate files error :

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'
}
Huy Tower
  • 7,769
  • 16
  • 61
  • 86
  • You can replace `compile files('libs/httpmime-4.3.5.jar')` with this `compile 'org.apache.httpcomponents:httpmime:4.3.5'`. What version of Android studio are you using? What Version of gradle are you using? – hoomi Aug 25 '14 at 10:14
  • 1
    @hoomi version 0.8.6. let me check your maven – Huy Tower Aug 25 '14 at 10:16
  • I know why you are duplicating the dependencies `compile fileTree(include: ['*.jar'], dir: 'libs')` already includes `compile files('libs/httpmime-4.3.5.jar')` – hoomi Aug 25 '14 at 10:19
  • Oh so u mean I should remove `compile files('libs/httpmime-4.3.5.jar')` right? Anyway, if I used your maven link. It is okay now. Thanks you – Huy Tower Aug 25 '14 at 10:22
  • I am not so deep into packaging, but I don't think `exclude 'META-INF/DEPENDENCIES'` will fix your dependencies problem. It will make the warning go away because you exclude the info from the JAR's meta info, but it won't solve your problem. To me, this looks like covering the oil lamp on you car's dashboard when it comes on, so it doesn't bother you anymore. – Oliver Hausler Mar 22 '15 at 19:43
  • @OliverHausler You mean you have the solution, post here ok? – Huy Tower Mar 23 '15 at 00:39
  • 1
    I don't have the solution to your problem, but as far as I can see this (if I'm wrong, correct me please), excluding dependencies from META-INF means you still have the dependencies in your Jar with a potential problem of conflict, but you don't tell the Jar what's inside. So in turn, the compiler can't detect the problem anymore and compiles without warning. This is not what you want. You want to exclude the duplicate dependency entirely. This can be done by using something like compile `compile('com.foo:foo-v4:20.0.0') { exclude module: 'fuu' }`. You will need to look at your libraries... – Oliver Hausler Mar 23 '15 at 15:53
  • 1
    ... to find out which library is duplicated, and then exclude it. Look here: https://www.gradle.org/docs/current/userguide/dependency_management.html - and as @hoomi said, you may be adding dependencies twice. – Oliver Hausler Mar 23 '15 at 15:53
  • @OliverHausler : Yes, I think you are correct. These lines just compile skip warning. – Huy Tower Mar 24 '15 at 01:17
  • after all your solution worked for me...thnx life saver – Himanshu Mori Jul 12 '16 at 20:34

5 Answers5

10

You can replace compile files('libs/httpmime-4.3.5.jar') with this compile 'org.apache.httpcomponents:httpmime:4.3.5'.

Also you are duplicating the dependencies compile fileTree(include: ['*.jar'], dir: 'libs') already includes compile files('libs/httpmime-4.3.5.jar')

hoomi
  • 1,882
  • 2
  • 16
  • 17
  • I get same error again after put 2 maven link, pls help me how to fix this error. If put 1 maven link, it is okay. I will unaccept your question since I want many people tell me how to fix. Thank you. – Huy Tower Aug 25 '14 at 23:53
  • What are the two maven links that you put? – hoomi Aug 26 '14 at 05:52
  • The problem is that Android is already using apache httpclient. Is there a reason that u want to reimport it? – hoomi Aug 26 '14 at 05:54
  • I already update my solution in my question. I don't know why. You know? – Huy Tower Aug 26 '14 at 06:59
  • just remove the httpclient maven line and also remove the jar file. Unless you need a new function which is not available in the older versions you do need to reimport it – hoomi Aug 26 '14 at 07:36
  • No, I should use http-client, http-core ... and so the other *.jar files. – Huy Tower Aug 26 '14 at 08:43
  • Have a look at this http://developer.android.com/reference/org/apache/http/package-summary.html. If you have all the classes that you need then you can stop importing them – hoomi Aug 26 '14 at 08:45
  • Ah, u see. I need use the other class had not had in Android SDK. ex. HTTPMuptile class ... u see, I want to upload multiple files ... – Huy Tower Aug 26 '14 at 09:12
  • I am not sure if you can override android libraries. Maybe you could find the source to the classes that you need and then import them directly – hoomi Aug 26 '14 at 09:19
  • You should clarify your answer again, its not easy to follow... thats why there are so many commant though – user1767754 Jul 08 '15 at 05:58
8

update your build.gradle and add the following lines

android{
    .
    .
    .
    packagingOptions {
        exclude 'META-INF/license.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/NOTICE'
    }
}

this will fix this error. I got the same error, doing this fixed it.

Gaurav Sharma
  • 1,983
  • 18
  • 18
0

Please update this to your build.gradle file.

    packagingOptions { 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'META-INF/NOTICE.txt' 
 }
Sabodh
  • 59
  • 5
0

I updated gradle now it's working

packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' }

-3

add the below code to dependencies

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5' compile (group: 'org.apache.httpcomponents' , name: 'httpmime' , version: '4.3.5') { exclude module: 'org.apache.httpcomponents:httpclient' }

now if you run that it will show you why it is telling duplicate may be because of META-INF/NOTICE, META-INF/LICENSE, add that first again run it may show other type. add all like below under android section packagingOptions { exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' }

definitely it will solve your problem.

remove all the dependancy file from the system before build.

Nirmal Dhara
  • 2,121
  • 18
  • 27
  • ~"remove all the dependancy file from the system". How is that applicable to gradle builds? – IgorGanapolsky Mar 19 '15 at 15:50
  • no not using the gradle build. gradle download the duplicated jars in your system. we need to physically remove the folder of the jar file from your system, before you build the project again. – Nirmal Dhara Mar 19 '15 at 16:04