问题描述:
此问题在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
...
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);
}