在defaultConfigz中已經聲明了multiDexEnabled true,當方法數超過65535時便會自動打出兩個Dex包命名為classes.dexclasses2.dex,一些方法被打入了第二個dex包,即classes2.dex中,導致了5.0以下機型無法運行應用報錯。
下面是解決方案:
1.在defaultConfig中已經聲明multiDexEnabled true用于啟用MultiDex
2.在依賴中添加compile 'com.android.support:multidex:1.0.1'支持包用于5.0以下系統
3.如果你的工程中已經含有Application類,那么讓它繼承android.support.multidex.MultiDexApplication類,如果你的Application已經繼承了其他類并且不想做改動,那么還有另外一種使用方式,覆寫attachBaseContext()方法:
publicclassMyApplicationextendsFooApplication{@OverrideprotectedvoidattachBaseContext(Context base) {super.attachBaseContext(base);
MultiDex.install(this);
}
}