2016年1月21日 星期四

Android 實戰記錄 (21) - multiDexEnabled 造成的 NoClassDefFoundError 問題

因為啟用了multiDexEnabled之後
反而在特定的手機(ASUS),出現了 NoClassDefFoundError 問題

瞭解之前版本與之後版本差異就差在我因為加了
multiDexEnabled

因為加入了
compile 'org.apache.commons:commons-lang3:3.4'
的關係

超過了65K method

後來終於解決問題,是查看了
以下這篇文章
Building Apps with Over 65K Methods
https://developer.android.com/intl/zh-tw/tools/building/multidex.html

調整了一些gradle的設定

啟用了(應該是改下面幾個設定就可以了)
dependencies {
    compile 'com.android.support:multidex:1.0.0'}


buildToolsVersion "21.1.0"

defaultConfig {
    multiDexEnabled true}

然後改寫原本繼承的Application
https://developer.android.com/intl/zh-tw/reference/android/support/multidex/MultiDexApplication.html

複寫方法
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

則可以解決問題。
ASUS,就可以運行正常。