完美解决Error:Execution failed for task ':APP:transformClassesWithDexForDebug'...问题

相信大伙在Android开发过程中都避免不了要集成第三方的项目、集成完之后有时就会碰到这个令人灰常厌恶的transformClassesWithDexForDebug、详细的Log如下:


[java]  view plain  copy
  1. Error:Execution failed for task ':APP:transformClassesWithDexForDebug'.  
  2. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException  

在此就记录一下困扰我几天的问题的解决方案、分享出来希望大家少走弯路、这个问题主要的原因就是引入的Libary与现有的工程中的某些Libs重复了、请详细检查并确保所有使用的lib只有一份、比如v4、v7、utdid.jar等等、
另外如果使用android的官方支持库请参见、http://developer.android.com/tools/support-library/features.html

如果以上情况检查完毕之后项目发布还是有问题就可以尝试使用如下配置来解决问题

[java]  view plain  copy
  1. defaultConfig {  
  2.     ...  
  3.     minSdkVersion 14  
  4.     targetSdkVersion 21  
  5.     ...  
  6.   
  7.     //Enabling multidex support.  
  8.     multiDexEnabled true  
  9. }  
  10. dependencies {  
  11.     compile ´com.android.support:multidex:1.0.1´  
  12. }  


然后在清单manifest里面这样引入、如果有自定义的AppApplication、就让你自定的AppApplication就继承这个类就好了

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.android.multidex.myapplication">  
  4.     <application  
  5.         ...  
  6.         android:name="android.support.multidex.MultiDexApplication">  
  7.         ...  
  8.     </application>  
  9. </manifest>  

另外还有可能就是JDK 1.8 版本问题、这并不像是偶然现象,于是怀疑 Gradle 与 JDK 1.8 存在兼容性问题、尝试将工程依赖的 JDK 版本降到 1.7

[java]  view plain  copy
  1. compileOptions {  
  2.     sourceCompatibility JavaVersion.VERSION_1_7  
  3.     targetCompatibility JavaVersion.VERSION_1_7  
  4. }  

以上配置配置完成之后运行可以还是会有错误、那么我们还可以在app.bulid里面加上这句、再Rebuild一下、之后再运行就行了、具体是4g还是多少可以看电脑配置修改(2g、3g、6g、8g)

[java]  view plain  copy
  1. dexOptions {  
  2.     javaMaxHeapSize "4g"  
  3. }  

以上就是我在遇到Error:Execution failed for task ':APP:transformClassesWithDexForDebug' 的所有解决方案、分享出来希望对大家有帮助。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常是由于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、付费专栏及课程。

余额充值