Android Studio gradle takes too long to build
If you are using Google Play services, make sure you aren't using this in your Gradle build script:
compile 'com.google.android.gms:play-services:8.3.0'
Only use those Google APIs that your app is really using. If all you are using is Google Maps, you would use this:
com.google.android.gms:play-services-maps:8.3.0
When I did this, my compile time went from over 2 minutes to around 25 seconds. For a list of the Google apis that you can selectively compile against, see:
https://developers.google.com/android/guides/setup
Found an easy and best way to build gradle
go to your app >> open your gradle.properties and change few lines from it [remove #]
else you may copy paste my below snippet as well
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx2048m -XX:MaxPermSize=512m
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true
it worked for me earlier it take almost 2-3 minutes now it is taking only 5-6 second
in above scenario my mac machine have 8GB ram so i assigned 4gb for gradle
Update 2022; Google removed "Offline work
" option;
They added "Donwload external annotations for dependencies
" option, but of course unchecking that option does not provide the same speed improvements.
Answer
In Android Studio go to File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle
(if on mac) Android Studio -> preferences... -> Build, Execution, Deployment -> Build Tools -> Gradle
Check the 'Offline work' under 'Global Gradle settings'
Note: In newer version of Android studio, View->Tool Windows->Gradle->Toggle button of online/offline
It will reduce 90% gradle build time.
if you just added a new dependency in your gradle you will have to uncheck the offline work or gradle will not be able to resolve the dependencies. After the complete resolving then you you can check the offline work for a faster build