Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle - Android Studio build too slow multidex application

Tags:

When I add to my project the multidex:true, and make an Application class that extends from the MultiDexApplication, my project build time passed from 20 sec to around 90 sec.How to do some faster?

like image 434
Александр Шевчук Avatar asked May 11 '15 20:05

Александр Шевчук


People also ask

Why is my Gradle build so slow?

Resources take up a sizable amount of space in your APK, and packaging all these resources slows down your build. For development builds, you can tell Gradle only to package the resources you care about, for the device you are developing on.

How much time does Gradle build takes?

gradle, if you have minifyEnabled for debug, remove it. I had it in my project and it took ~2-3 minutes to build.

What is multidex enabled in Android?

This number represents the total number of references that can be invoked by the code within a single Dalvik Executable (DEX) bytecode file. This page explains how to move past this limitation by enabling an app configuration known as multidex, which allows your app to build and read multiple DEX files.

Is multidex needed?

Multidex support library is enabled by default in API level higher than 21 (Android 5 and higher). Therefore, you do not need to add the Multidex support library.


2 Answers

If you are like me who already tried Vic Vu's solution but still can't avoid enabling multiDex then you can try this (as long as your are using a device that has Android 5.0 and above).

Note This will only speed up your development build. Your production build will still be slow.

Basically you need to introduce 2 product flavors one for dev and one for prod.

Add multiDexEnabled true

android {     productFlavors {         // Define separate dev and prod product flavors.         dev {             // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin             // to pre-dex each module and produce an APK that can be tested on             // Android Lollipop without time consuming dex merging processes.             minSdkVersion 21         }         prod {             // The actual minSdkVersion for the application.             minSdkVersion 14         }     }           ...     buildTypes {         release {             runProguard true             proguardFiles getDefaultProguardFile('proguard-android.txt'),                                                  'proguard-rules.pro'         }         defaultConfig {             applicationId "com.something.something"             targetSdkVersion 23             versionCode 1             versionName "1.0.0"              multiDexEnabled true         }     } dependencies {   compile 'com.android.support:multidex:1.0.1' } 

And I have a class which extends Application so I had to override attachBaseContext()

@Override protected void attachBaseContext(Context base) {     super.attachBaseContext(base);     MultiDex.install(this); } 

If you are not extending Application simply use MultiDexApplication in your AndroidManifest.xml application tag.

Ensure that in your Android Studio Build Variants you are pointing to devDebug.

Read the complete instructions here https://developer.android.com/studio/build/multidex.html#dev-build

like image 125
Mark Pazon Avatar answered Sep 28 '22 09:09

Mark Pazon


Supplying as an answer because this is better fit with the formatting.

To simply answer your question: No, there is no way. Multidex is a process meant to help lift the burden of the 65k method limit. This process is complicated and will simply make your build times longer.

The best you can can do is lower your method count.

In your build.gradle (supplied here) you're using:

`compile 'com.google.android.gms:play-services:8.3.0'` 

But if you look at the most recent play services api you can pick and choose what services you actually need.

Look at Table 1 on this page.

Only use the ones you need. Google play services as a whole is somewhere around 30k methods.

That should help.

like image 24
VicVu Avatar answered Sep 28 '22 09:09

VicVu