有时我们添加了外部jar包时会碰到如下错误导致不能编译:
1. Error:Execution failed for task':app:transformResourcesWithMergeJavaResForDebug'.
>com.android.build.api.transform.TransformException:com.android.builder.packaging.DuplicateFileException: Duplicate files copied inAPK META-INF/NOTICE
File1: F:\AndroidStudioProjects\study\AsynctaskDownloadPic2\app\libs\httpcore-4.4.4.jar
File2:F:\AndroidStudioProjects\study\AsynctaskDownloadPic2\app\libs\httpclient-4.5.1.jar
解决方式如下:
在build.gradle文件里加上如下代码:
android {
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
//...
}
2.百度地图开发时碰到的坑:
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK assets/Icon_walk_route.png
File1: D:\AndroidStudioProjects\BaiduMapDemo\app\libs\BaiduLBS_Android.jar
File2: D:\AndroidStudioProjects\BaiduMapDemo\app\libs\baidumapapi_map_v3_7_3.jar
问题:有重复的文件,需要删除一个包。
解决方案:因为BaiduLBS_Android.jar是整个百度地图所有功能的全部包,因此删除baidumapapi_map_v3_7_3.jar,就可以了。
3. 添加了jar包之后打包apk失败报错:
Error:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
或者
Error:Execution failed for task ':app:mergeDebugResources'.
> Error: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
解决方案:
在gradle的android{ ... } 中加入这两句就可以了
- android {
- ......
- aaptOptions.cruncherEnabled = false
- aaptOptions.useNewCruncher = false
- ......
- }
亦或者出现如下报错:
Error:Execution failed for task ':app:clean'.
> Unable to delete directory: D:\AndroidStudioProjects\ns360_server2\app\build\intermediates\res\merged\debug
解决方案:
执行Tools菜单下的Android->Sync Project with Gradle Files .就OK了