項目中用到了Retrofit在android4.4以下版本發生的問題
因為項目的build.gradle文件沿用於一個項目的,在defaultConfig
z中已經聲明了 multiDexEnabled true
,當方法數超過65535時便會自動打出兩個Dex包命名為classes.dex
classes2.dex
,一些方法被打入了第二個dex包,即classes2.dex
中,導致了5.0以下機型無法運行應用報錯。
解決方法:
(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類,那么讓它繼承android.support.multidex.MultiDexApplication類,如果你的Application已經繼承了其他類並且不想做改動,那么還有另外一種使用方式,覆寫attachBaseContext()方法:
/**
* 分割 Dex 支持
* @param base
*/
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}