Android enable multDex app with over 65K methods

There is a 65K methods limit when developing Android Application. When you included a lot of thrid party libraries or you’ve wrote a lot of Java methods yourself in your Android Application, you will have to enable the multi dex option in your android application, otherwise you will get errors like the following, complaining about that you have exceeded the 65K methods limits.

The first compile error you will see is com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 Error:Execution failed for task ‘:app:dexDebug’. This can be fixed by adding this multiDexEnabled true in the defaultConfig which resides in the android scope that you should see in your app build.gradle file

defaultConfig {
    multiDexEnabled true
}

The second error you will see isUncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded Error:Execution failed for task ‘:app:dexDebug’. This can be fixed by adding this in the android scope that you should see in your app build.gradle file.

dexOptions {
    javaMaxHeapSize "4g"
}

The thrid error you are likely to see is Could not find class ‘com.google.android.gms.measurement.internal.zzaa’, referenced from method com.google.android.gms.measurement.internal.zzw.zzaT. After you enabled the multiDex, you need to install it for your application. All you have to do is to add the attachBaseContext method shown below in your custom application class. This application class should be the name shown in your AndroidManifest’s application tag.

public class MyApp extends Application {
    @Overrides
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

To check how many methods you have exactly in your compiled android application, in your command shell, navigate to build/dex/debug/ and run the following command. Ref

cat classes.dex | head -c 92 | tail -c 4 | hexdump -e '1/4 "%d\n"'

Search within Codexpedia

Custom Search

Search the entire web

Custom Search