Sunday 11 October 2015

What is 65K Method limit in Android? How 65k Method limit handled by different build Systems of Android.

65k Method limit, well i recently faced this problem in one my android application. Trust me this is one of the worst problem you want to get into while development of your application.

So what is 65k method limit in Android? Firstly we should know this then only we can solve this.

65k Method Limit:

Apk i.e  Android Application contains DEX (dalvik executables i.e executable bytecode) as we know. So app contains a Single DEX i.e One Dex per app.

Single DEX file refers to 65,536 methods(including Android framework methods, library methods, and methods in your own code). So keep this thing in mind if your application is going to be big.

That's how error looks in Console =>
Earlier build versions :

Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536

Recent build versions :

trouble writing output:
Too many field references: 131000; max is 65536.
You may try using --multi-dex option.

So first thing we never want to get into this stitution. And if we're into this stitution then how can we get rid of this is as follows:

We need to generate more then One Dex file for our app which is known as a multidex configuration. In order to achieve this we need to configure our Build app.

This problem mostly occurs on version prior to 5.0. Beacuse earlier versions uses Dalvik runtime for executing code, where as 5+ uses ART(Android runtime).

So how these android runtime works when this 65k problem occurs.ART is present in 5+ versions which supports loading multi dex files from an apk. So we need to configure our build system for earlier version in order to implement multi dex concept for handling 65k error.
Solution to this problem is "Multidex Support Library".

We need to modify our gradle file firstly as follows :

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21

    // Enabling multidex support.
        multiDexEnabled true
 
}

}


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


In your manifest add the MultiDexApplication class from the multidex support library .

<?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>

So when These things are done Android build Tools will generate :

1.  Primary dex (classes.dex)
2.  Supporting (classes2.dex, classes3.dex) as needed.

The build system will then package them into an APK file for distribution.

So this how you can overcome 65k method problem. But best practice would be reducing code. you can try following things that i would recommend :

1. Remove large libs if included in your project. Beacuse you will be using few methods for your purpose rather then whole lib. So try avoid such things if you're facing such an issue.

2.Remove unused code via proguard.


References : https://developer.android.com/


Happy coding!!

No comments:

Post a Comment