android4.0下使用multiDexEnabled屬性的踩坑日記

由於公司需要需使用android4.0的設備進行開發,所以代碼屬性需針對android做適當調整。

首先添加 multiDexEnabled true 這條屬性是爲了解決方法數大於65k的問題。

defaultConfig {
        minSdkVersion 15
        targetSdkVersion 26
        multiDexEnabled true
    }

dependencies {
    compile 'com.android.support:multidex:1.0.0'
}

添加完這段代碼在android高版本下是完全沒問題的,但是在android4.0上會出錯。解決辦法是在繼承了Application的類下添加如下代碼:

public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

運行成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章