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);
    }
}

运行成功。

猜你喜欢

转载自blog.csdn.net/qq_28670711/article/details/83270141
今日推荐