项目运行报错:com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
这是由于META-INF/LICENSE引用jar包重复。
错误内容如下:
- 1
- 2
- 3
- 4
出现这个错误,说明引入的jar包中出现了重复的META-INF文件导致无法合并。解决方法也很简单,在你报错的模块的build.gradle中添加如下配置即可解决这个错误:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
我们来自己看下上面报的这个错误:
- 1
方式一:
这个错误说的是META-INF/LICENSE中有重复导致无法打包,其他的情况类似,如果报错出现其他的文件,我们只需要在packagingOptions
中添加忽略文件即可。下面是一套常用的配置:
主要解决是忽略冲突,只采用其中一种引用。
方式二:
文件重复出的错,我们只需要在packagingOptions设置排除该文件即可。
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
packagingOptions{
exclude 'META-INF/DEPENDENCIES'
}
defaultConfig {
applicationId "com.myapp.test"
minSdkVersion 17
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:22.0.0'
compile files('libs/apache-mime4j-0.6.jar')
compile files('libs/httpmime-4.0.jar')
}
一般情况还会出现NOTICE,LICENSE文件也会重复。packagingOptions{ exclude 'META-INF/NOTICE' // will not include NOTICE file exclude 'META-INF/LICENSE' // will not include LICENSE file exclude 'META-INF/DEPENDENCIES' }
多Module jar重复
当我们创建多个Module并且都使用了某一个依赖如加入下面依赖
compile 'com.google.code.gson:gson:2.6.2'
当我们在Moudule 中都加入了依赖后就会出现jar重复,导致运行失败,此时我们可以将其中一个Moudle的依赖保留上面写法,其余更如下
provided 'com.google.code.gson:gson:2.6.2'
或者右键module,选择open module setting,选择要修改的module名,切换到dependencies页面,将要修改的jar的scope设置为provided模式。
使用第三方依赖 jar重复
当我们不同Module使用了同一个依赖时可以更改scope为provided模式,但是如果我们使用的的两个第三方依赖中包含有相同的jar的,我们就不能更改scope,。如我们项目中使用了下面两个依赖
compile'com.microsoft.projectoxford:vision:0.5.0' compile 'com.microsoft.projectoxford:emotion:1.0.0'
此时运行程序会出现错误提示
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForArmDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/maven/commons-io/commons-io/pom.xml File1: E:\Project\MyApp\app\build\intermediates\exploded-aar\com.microsoft.projectoxford\vision\0.5.0\jars\libs\commons-io-2.4.jar File2: E:\Project\MyApp\app\build\intermediates\exploded-aar\com.microsoft.projectoxford\emotion\1.0.0\jars\libs\commons-io-2.4.jar
通过提示我们看出这两个依赖中都使用了commons-io-2.4.jar,为了保证运行,我们根据提示慢慢修改吗,在packagingOptions中加入下面配置
exclude 'META-INF/maven/commons-io/commons-io/pom.xml'
加入后再次运行又出现错误
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForArmDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/maven/commons-io/commons-io/pom.properties File1: E:\Project\MyApp\app\build\intermediates\exploded-aar\com.microsoft.projectoxford\vision\0.5.0\jars\libs\commons-io-2.4.jar File2: E:\Project\MyApp\app\build\intermediates\exploded-aar\com.microsoft.projectoxford\emotion\1.0.0\jars\libs\commons-io-2.4.jar
根据提示继续在packagingOptions中加入下面配置
exclude 'META-INF/maven/commons-io/commons-io/pom.properties'
apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.0" defaultConfig { applicationId "com.myapp.test" minSdkVersion 16 targetSdkVersion 22 versionCode 6 versionName "1.0" } packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/maven/commons-io/commons-io/pom.xml' exclude 'META-INF/maven/commons-io/commons-io/pom.properties' } sourceSets { main { java.srcDirs = ['src/main/java'] jniLibs.srcDir 'libs' } debug.setRoot('build-types/debug') debug.res.srcDirs = ['src/debug/res'] release.setRoot('build-types/release') } } dependencies { compile('com.microsoft.projectoxford:vision:0.5.0'){ exclude group: 'org.apache.commons.io' } compile 'com.microsoft.projectoxford:emotion:1.0.0' }
希望这种方式可以帮助到您。