Android : app loading library at runtime on Lollipop but not IceCreamSandwich

跟風遠走 提交于 2019-11-27 20:41:20

1) Add multidex support to your app:

compile 'com.android.support:multidex:1.0.1'

2) Set your application as a MultiDexApplication. Select one of the options below:

This is the source code of android's MultiDexApplication class:

public class MultiDexApplication extends Application {
    public MultiDexApplication() {
    }

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

Option 1) You can extend your custom application from MultiDexApplication:

public class MyApplication extends MultiDexApplication {
    // Your application implementation here
}

Option 2) You can extend your custom application from default Application class, then you need to call MultiDex.install at attachBaseContext(Context base) method:

public class MyApplication extends Application {

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

    // Your application implementation here
}

Option 3) If you don't have any custom application and you don't want any, simply set your application name (at AndroidManifest.xml) to MultiDexApplication:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

Note: If you are using a custom application class (see Option1 and Option2), application tag's android:name must already be set to your custom application class.

3) Add multiDexEnabled true setting to your build.gradle file:

defaultConfig {
    // Other settings here
    multiDexEnabled true
}

More information.

I forgot to add this line right after the super call in the onCreate() method of my custom application class :

 MultiDex.install(getBaseContext());

Thanks Kane for the solution !

On some low-end devices adding MultiDex.install(getBaseContext()); to MyApplication onCreate() did not work? causing java.lang.NoClassDefFoundError:

What did work was adding MultiDex.install(getBaseContext());to MyApplication attachBaseContext(Context base)

public class MyApplication extends Application {
...
   @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }
...
rajnish Verma

You can add this in Application tag of manifest file.

android:name="android.support.multidex.MultiDexApplication"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!