Building Apps with Over 65K Methods

Overview

As the Android platform has continued to grow, so has the size of Android apps. When your application and the libraries it references reach a certain size, you encounter build errors that indicate your app has reached a limit of the Android app build architecture. Earlier versions of the build system report this error as follows:

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

Both these error conditions display a common number: 65,536. This number is significant in that it represents the total number of references that can be invoked by the code within a single Dalvik Executable (dex) bytecode file. If you have built an Android app and received this error, then congratulations, you have a lot of code! This document explains how to move past this limitation and continue building your app.

About the 65K Reference Limit

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.

How to implement

    • Import to build.gradle

      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'
      }
      


    • Create application file
public class SimplePrintsApp extends MultiDexApplication
    ...
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    ...
}
    • Import to AndroidManifest.xml
<application
        android:name="me.storytree.simpleprints.SimplePrintsApp"

In conclusion

This above solution will help us to create application that has many libraries and method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s