I\'m currently developping an Android app using Android Studio. Currently, the app is launching perfectly on Lollipop devices, but crashes at launch due to a ClassNotF
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);
}
...
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.
You can add this in Application tag of manifest file.
android:name="android.support.multidex.MultiDexApplication"
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 !