How to Configure Multidex in an App Android

Gonza F Picó
AndroidPub
Published in
2 min readJan 26, 2017

In this article I’m going to show how to configure Multidex in an Android application. If you need to use it, it means that your app has over 65k (65.536) methods. It is the “android 65k method limit“. Those references invokes the code within a single DEX (Dalvik EXecutable) bytecode file. It can happen because of you are using many libraries. So, you should consider that this is the sum of three elements. One, the methods of your code. Two, the methods of the libraries. And three, the Android framework libraries.

Configuring Your App for Multidex with Gradle

Change your Gradle build configuration to enable multidex

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

Change your manifest to reference the MultiDexApplication class

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

If you have a base class that represents your app, then the name of your class app should be written in the field android:name.

public class BaseApplication extends MultiDexApplication {android:name=".BaseApplication"

Another way to make the same process is like this:

public class BaseApplication extends Application {  @Override public void onCreate() {
MultiDex.install(this);
...

From now on you can configure Multidex whenever your app needs it. But it doesn’t mean you are doing the things well. Anyway, you should avoid the 65k limit. Do it before configuring multidex. There are two strategies:

  • Review your app’s direct and transitive dependencies

Running ./gradlew [app_name]:dependences you can see the tree of dependencies. If you reduce your app code dependencies you could avoid the dex reference limit. Any large library included in your app should add as less code as possible to the application.

  • Remove unused code with ProGuard

Configure the ProGuard settings for your app. Run ProGuard and ensure you have shrinking enabled for release builds. Enabling shrinking ensures you are not shipping unused code with your APKs.

#AndroidDev
You should avoid the 65k limit. There are 2 strategies: 1.Review your app’s direct and transitive dependencies 2.Remove unused code with ProGuard

Nowadays the Android development has become very popular. So, there are many helpful libraries that allow you don’t reinvent the wheel. Then many current apps developed are in the same situation. They need multidex configured to run. Have you found many times the android 65k limit? How did you solve it?

Thank you for your time reading this article. If you found it helpful, do not hesitase to click the ❤ below and share it.

--

--

Gonza F Picó
AndroidPub

Working on AppDermis | #Freelance in mobile industry | #Entrepreneur newbie | #GIG | ‘The more I learn, the more I realize how much I don’t know’