android studio 中 java.lang.NoClassDefFoundError:retrofit2.Retrofit$Builder错误解决

此问题在android5.0以下不会出现,在android4.0会出现。

    引入的包也没有问题:(搞了好久才解决,真感觉奇了怪了)

/*retrofit*/
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

    android4.+时的问题如下图:

    

    引起该问题的原因是: 

defaultConfig {

    ...
    multiDexEnabled true
}

    多dex打包 multiDexEnabled 打开了

     第一想法是关闭它,改为

multiDexEnabled false

但在android4.+版本上还会出现另一个问题:如下



error:The number of method references in a .dex file cannot exceed 64K.
Error:Execution failed for task ':app:transformClassesWithDexForRelease'.
针对这个问题,还需用分包。
最终还是回到第一个问题了,哈哈,废话少说看下边方案。
 
解决方法: 
 


(1)在app的 build.gradle 中的dependencies 中添加


       compile 'com.Android.support:multidex:1.0.1'


      dependencies {
             compile fileTree(include: ['*.jar'], dir: 'libs')
             compile 'com.android.support:multidex:1.0.1'
       }
(2)在app的 build.gradle 中的 defaultConfig 中添加
           multiDexEnabled true
         defaultConfig {
 ...
 multiDexEnabled true
         }
(3)在 AndroidManifest.xml 中的  application 标签中添加 :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package=".test">
     <application
        ...

提示:你的Application叫什么名字就写什么名字
         android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>
提示:如果你的应用重写了Application,则你需要继承MultiDexApplication而不再是Application啦


在重写的Application中重写下边方法
/**
 * 分割 Dex 支持
* @param base
*/
@Override
protected void attachBaseContext(Context base) {
         super.attachBaseContext(base);
          MultiDex.install(this);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值