Android studio报错Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'

Android studio报错Error:Execution failed for task

最近做基于android平台的人脸识别签到系统时,导入第三方的lib包后,android studio报错如下:

Error:Execution failed for task ‘:app:transformResourcesWithMergeJavaResForDebug’.
com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
File1: E:\Workspaces\Swface\app\libs\httpcore-4.4.4.jar
File2: E:\Workspaces\Swface\app\libs\httpclient-4.5.2.jar

查阅资料后发现是因为jar包中META-INF目录下有重复内容加载,打开对应的jar包jar包点开图
果然两个jar包都有LICENSE,进一步点开发现里面内容完全一样,不过话说过来,旁边三个文件貌似也是重复的(只是没有在报错中明确提示),那就一并解决好了
既然知道了是因为重复加载文件导致的,解决那就是在编译时去除重复加载的文件,具体操作如下:

  1. 打开app\build.gradle文件
    2.修改配置如下:
buildTypes {
          ......(省略部分代码)
          //在这里加入如下代码
          packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/ASL2.0'
            exclude 'META-INF/notice.txt'
        }
 }       
``

3.ReBuild Project搞定

附上原文参考链接:

http://stackoverflow.com/questions/34031395/errorexecution-failed-for-task-apptransformresourceswithmergejavaresfordebug

这个错误通常是由于dex方法数超出限制引起的。你可以尝试以下方法解决: 1. 在app的build.gradle文件中添加下面的代码: ```groovy android { defaultConfig { ... multiDexEnabled true } ... } dependencies { implementation 'com.android.support:multidex:1.0.3' } ``` 然后在你的Application类中添加下面的代码: ```java public class MyApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } ... } ``` 2. 如果你使用了很多第三方库,可以尝试使用ProGuard来优化代码。在app的build.gradle文件中添加下面的代码: ```groovy android { ... buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } ``` 然后在app的根目录下创建一个proguard-rules.pro文件,添加下面的代码: ``` -dontwarn okio.** -dontwarn javax.annotation.** -dontwarn com.squareup.okhttp.** -dontwarn retrofit2.Platform$Java8 -dontwarn retrofit2.Platform$Java8$.* -dontwarn retrofit2.Platform$Java9 -dontwarn retrofit2.Platform$Java9$.* -keep class com.google.gson.** { *; } -keep class org.apache.commons.io.** { *; } -keep class retrofit2.** { *; } -keepattributes Signature -keepattributes Exceptions -keepattributes InnerClasses ``` 注意:ProGuard可能会导致某些库出现问题,因此需要进行适当的测试和调整。 3. 如果以上方法都无法解决问题,可以尝试升级Gradle版本或者在项目中使用AndroidX。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值