Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up gradle build in multidex application

Tags:

My application has a bunch of librarys that are essential that is why I was forced to use multidex support library and it works nicely. But where the problem shows is in the gradle buid speed. It takes on average 2minutes to build and when I am developing and testing this is quite annoying.

Is there a way to speed up my debug builds?

like image 957
Tadej Vengust Avatar asked Jun 11 '15 09:06

Tadej Vengust


People also ask

Why is my Gradle sync taking so long?

Check your Internet connection. If internet speed is very slow gradle build will also take long time to build. I check by change my wifi internet connection with another one good speed connection. Now build time is normal.

How much time Gradle build takes?

and their gradle is taking 2-3 minutes for the first time build and then less than 30 seconds for each build can you please tell me what is wrong with my android studio? or what kind of settings i can do to make it better? You could try gradle build --profile to see where the time is being spent.


1 Answers

You can speed-up your development builds by specifying the minimum SDK version = 21.
Official documentation includes a whole section about that.

Example (from documentation):

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'         }     } } dependencies {   compile 'com.android.support:multidex:1.0.0' } 

Once you added the product flavors, you can use the devDebug task (instead of default debug task) for your development builds:
- from command line: run ./gradlew installDevDebug
- from Android Studio: open Build Variants window and select the devDebug build variant.

You should, of course, work against a device whose SDK >= 21.


There's also a solution for those who don't want to use flavors. As suggested in this gist, dynamically calculate the minSdkVersion value:

int minSdk = hasProperty('devMinSdk') ? devMinSdk.toInteger() : 14 apply plugin: 'com.android.application'  android {     ...     defaultConfig {         minSdkVersion minSdk         ...     } } 

In this example, we're checking if devMinSdk property defined, and if true - we're using it. Otherwise, we default to 14.

How do we pass devMinSdk value to build script? Two options:

Using command line:

./gradlew installDebug -PdevMinSdk=21 

Using Android Studio preferences:

Go to Preferences (Settings on Windows) -> Build, Execution, Deployment -> Compiler -> put -PdevMinSdk=21 in Command-line Options text box.

Android Studio compiler options

like image 143
Alex Lipov Avatar answered Sep 21 '22 00:09

Alex Lipov