Building my Android app takes about 90 seconds ("fast"), up to 3 minutes for each update to my code. It's a complete waste of time as it really and I assume a solution must be within reach. I tried investigating the issue and found different blog-posts and SO answers with suggestions, most of which I've tried.
The process that seems to take long, about 85% of total build time is :app:transformClassesWithDexForDebug
What is that process actually doing? I can find people who have crashes on it, but it works fine for me, except for the fact that it takes a lot of time. And do I need it, since I don't really need Dex at this point?
Also, I have 13 dependencies and 3 testCompile dependencies. I already point to specific play packages, so I'm not compiling stuff I don't need. If I understand things correctly, gradle is building all those libraries each project build as well. If that is correct, is there a way to skip that? Can I build them myself as wrapped-up libraries and include them without the need for them to be processed each time? That might make me lose some flexibility for future changes to dependencies, but at this point I feel like I'm losing over an hour a day easily on waiting for gradle. I'm not sure if flexibility is worth that much to me.
I'm looking forward to get any pointers on how I can improve my build process. Thank you in advance.
This happens due to the fact that the module needs to be built from the scratch every time. Enable gradle Offline Work from Preferences-> Build, Execution, Deployment-> Build Tools-> Gradle. This will not allow the gradle to access the network during build and force it to resolve the dependencies from the cache itself.
Parallel executionBy using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects.
Right click on the deploy or any other task and select "Open Gradle Run Configuration..." Then navigate to "Java Home" and paste your desired java path.
Edit: At this point, I recommend running Android Studio 2.x side by side with your 1.5 installation. You get access to instant-run, which really helps as well as all the updated tools. If you are staying on 1.5 read on...
I have managed to speed up Android Studio 1.5 debug builds from 2 minutes to 30 seconds. This might not work with your command-line execution, but might be faster.
Using this config, your first IDE build takes the same amount of time, but Incremental builds are faster, even if you modify classes. You lose some gain if you modify attached libraries.
Step 1. (If you're fortunate enough to target minSdkVersion of >= 21 already, skip this.)
@vanomart 's answer of having a minSdkVersion debug flavour of >= 21 is not wrong, but the only part necessary is adding the following to module (app) build.gradle and ensuring you target dev while debugging in your Build Variants tab:
android { [...] productFlavors { dev { minSdkVersion 21 //The important bit. } prod { minSdkVersion 15 //Example. Set to your real requirement. } }
Step 2. Pre-dexing libs.
In your module (app) build.gradle set the following config. This is faster for IDE builds, not so much for builder-servers who start from scratch every build.
android { [...] dexOptions { preDexLibraries true javaMaxHeapSize "2g" // Use gig increments depending on needs }
Source, doing (partly) inverse of "Improving Build Server performance": http://tools.android.com/tech-docs/new-build-system/tips#TOC-Improving-Build-Server-performance
Step 3. Make sure you're using latest buildToolsVersion in module (app) build.gradle.
android { buildToolsVersion "23.0.2" [...]
"... update the build tools version in all your modules to the latest (23.0.2). [...] it will use a new faster version of dex, which helps both instant run and a full build be a bit faster."
Source: http://tools.android.com/tech-docs/instant-run
Step 4. Use latest Gradle build tools
In your project build.gradle, set to latest (currently 2.0.0-alpha6)
buildscript { dependencies { classpath 'com.android.tools.build:gradle:2.0.0-alpha6'
Updates list: http://tools.android.com/tech-docs/new-build-system
Step 5. Use latest Gradle wrapper. Modify gradle-wrapper.properties, update this line to use 2.10:
distributionUrl=https\://downloads.gradle.org/distributions/gradle-2.10-all.zip #Alternative url if the above does not work: #distributionUrl=https://services.gradle.org/distributions/gradle-2.10-all.zip
In Android Studio preferences, ensure you have "Use default Gradle wrapper" selected. I recommend restarting Android Studio to ensure the Gradle daemon restarts.
"In many cases, Gradle 2.9 is much faster than Gradle 2.8 when performing incremental builds."
Source: docs.gradle.org/current/release-notes
Upgrading to Android Studio 2.1 and the Android Gradle Plugin v2.1.0 has largely fixed this problem for me. After installing the updated IDE, you should be prompted to update your Gradle plugin as well. You'll know you have the right version if your root build.gradle file has the following line:
classpath 'com.android.tools.build:gradle:2.1.0'
IMPORTANT: In addition to upgrading, you also need to increase the amount of memory allocated to the Gradle daemon to 2048mb, so it can perform this expensive dex-ing step in process. To do so, add the following to your root gradle.properties file:
org.gradle.jvmargs = -Xmx2048m
I had similarly slow build times as experienced in the question above, but after upgrading my build speeds increased dramatically. See the release notes for the Android Gradle Plugin v2.1.0 here for more info:
http://developer.android.com/tools/revisions/gradle-plugin.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With