Android gradle build time exponential increase - android

My Gradle build time is usually around 12 seconds, but after around 10 - 20 builds it starts to increase at once, exponentially: 30 seconds, 50 seconds, 2 minutes, 5 minutes, etc. up to out of memory.
Healthy build:
:app:compileDev21DebugJavaWithJavac 4.783s
:app:transformClassesWithRealmTransformerForDev21Debug 3.011s
:app:transformClassesWithDexForDev21Debug 2.133s
:app:compileRetrolambdaDev21Debug 1.325s
:app:packageDev21Debug 0.997s
:app:processDev21DebugResources 0.703s
After 10 - 20 of those, it starts increasing:
:app:transformClassesWithDexForDev21Debug 39.172s
:app:compileDev21DebugJavaWithJavac 34.221s
:app:packageDev21Debug 9.922s
:app:transformClassesWithRealmTransformerForDev21Debug 8.353s
:app:compileRetrolambdaDev21Debug 3.120s
:app:fabricGenerateResourcesDev21Debug 1.376s
As you can see, every task increases in time.
I use Android Studio 2.2.2, but I had the problem since the last 3 versions or so. Gradle 2.14.1. Probably it’s related to our project. I use Mac, my colleague on Windows has the same on the same project. If I stop AS, kill Java and start AS again, that solves it for a while.
I am not using the Jack compiler (yet).
To be clear: my build time is NOT generally slow: 13 seconds is fine. This is not a duplicate of all the "slow build time" questions. Only after 10 - 20 builds the build time increases exponentially with every build.
Update: Make Gradle build "offline" didn't help. Using "gradle-3.1-all.zip" (distributionUrl in grade-wrapper.properties) also did not help.
Anyone had this problem and knows a solution?

I experienced(and somehow still facing) the same issue. There are some fixes that you can try.
1- Make Gradle build Offline.
2- Install Gradle on machine and use local installation alongside Offline work checked in Android Studio instead of Using wrapper which connects to proxy and takes more time with building cache of Studio.
I followed this and this article and it helped me not substantially but enough to get me nearly 6,7 hours of continuous development on 8GB memory system then i have to Invalidate and Restart.

It is fixed since the day that I removed Fabric (CrashLytics) and replaced it with FireBase Crashes. Fabric was most probably causing it, but I'm not 100% sure.

Related

Gradle Build Running since 57 Minutes?

I am sorry if this question is repeat but gradle is taking much longer time then before. It is running since 57 min as you can see in screenshot and in previous attemp it took only one or half minute and usually it builds app in 2-3 minutes maximum. But from 2,3 days it behaving strangely. Running forever without any reason. Any help?
In Android Studio go to File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle
Check 'Offline work' under 'Global Gradle settings'
It will reduce 90% gradle build time.
While offline builds and reinstall may be a solution, we still need to get down to the exact reason.
Is it possible for you to run the gradle command with a stacktrace or a debug info? Even if the build hangs sporadically, you lose nothing by putting out a debug log. At best, there will some information which can be analyzed further.

Android Studio inital gradle build takes a long time

I've been using android studio for a few weeks now and noticed that it's been getting slower when restarted every time. The actual problem is that it seems to be stuck with the initial "Gradle Build Running" and started taking a few minutes to complete but now takes about 30+ minutes before it lets me actually use it. When this initial build is done it builds/cleans the project in about 4-7 seconds so it's only a recurring issue when I restart it.
One thing it could be is that the internet is awful (has a ping of about 500 ms), could that be related?
Hope someone can help!
Edit: Tried gradle offline mode, no luck there. After a further restart it even takes a long to stop the build too, it's been trying to stop the gradle build for about 10 minutes now...
Yes it can be related to your slow internet connection. Gradle re-downloads its dependencies at first run. It will be faster if you enable offline mode. To enable offline mode, Go to Preferences > Gradle and check "Offline work".
If you need to download any new dependency, disable offline work and sync project and disable it again.

What's the fastest way to run unit tests in Android

There are two options available to run unit tests in Android Studio:
Right click on the file with your unit tests (which is Gradle-Aware Make).
testAppDebugUnitTest task is from the list of available Gradle tasks.
The first one actually consists of two tasks:
- :app:assembleAppDebug and :app:assembleAppDebugUnitTest
The second one is just testAppDebugUnitTest itself.
The first one is a recommended way by Google Tools Team, but it takes as twice as much time compared to the second one.
There is no visible difference how to run unit tests except the duration. You might think your code won't be compiled without assembleAppDebug, but this is not true - changes you introduce to either class under a test or a test itself compiled and executed as expected.
So, what's the difference and why is it the recommended way if it takes as twice as much time to prepare everything before actual unit tests run?
I don't have much work experience with Gradle, But off course it is always better to use Google Team recommended tools instead of others.
it takes as twice as much time compared to the second one.
If you use the new Gradle build system with Android (or Android Studio) you might have realized, that even the simplest Gradle call (e.g. gradle project or grade tasks) is pretty slow.
To avoid this time taking processes in android studio you need to Speed up Gradle build time. It will definitely makes a difference
Here is few steps to speed up the Gradle build time >>
In compiler settings (Android Studio -> Preferences -> Project
settings[your project] -> Compiler (Gradle-based Android Projects)),
type --offline in the "Command-line options" box.
from ~4 minutes to ~20 seconds.
In another way, You can decrease this startup time of Gradle (on my computer down to two seconds), if you tell Gradle to use a daemon to build. Just create a file named gradle.properties in the following directory:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)
Add this line to the file:
org.gradle.daemon=true
From now on Gradle will use a daemon to build, whether you are using Gradle from command line or building in Android Studio. You could also place the gradle.properties file to the root directory of your project and commit it to your SCM system. But you would have to do this, for every project (if you want to use the daemon in every project).
Note: If you don’t build anything with Gradle for some time (currently 3 hours), it will stop the daemon, so that you will experience a long start-up time at the next build.
For more details please refer this Building and running app via Gradle and Android Studio is slower than via Eclipse
Performance improvements are one of the great tasks in the Gradle roadmap for 2015 (and reaching into 2016). So hope, we’ll see the general performance increasing within these years.
n g+ there is a discussion with the developers about it.
Hope this information will helps you.
The reason your option 1 takes twice as time is because most of the time is wasted on spinning up Gradle and checking if tasks are up to date. So running an no-op assemble will make it twice as long. You can verify this by running it yourself and check the time wasted by doing an no-op.
With gradle, running with "--offline --daemon --parallel" can get you somewhere (offline is helpful when you have slow internet access to Maven/JCenter repos, daemon is helpful to keep files cached, and parallel is particularly helpful if you have MultiDex on and have split your app into small modules)
If you are just asking about "What's the fastest way to run unit tests in Android?" without considering the change you would need, I would recommend switching to Buck (https://buckbuild.com/), which is 10x faster than Gradle on dex-xing and spinning up tests but has limited AS support.

Extremely slow gradle build

The following measures were taken to speed up gradle build from this (SO post)
org.gradle.parallel=true, org.gradle.daemon=true
build from terminal and not from android Studio
building with ./gradlew assembleDebug --offline
Problem:
Even after these measures build times can take upto 6-8 minutes for a single line of code change! and during this time computer mostly freezes and nothing else can be done except to stare at the terminal. I've noticed that "app:dexDebug" takes a lot of time in this. What are the possible causes?
Relevant files:
Terminal build dump
build.gradle(app)
build.gradle(project)
System details:
Android Studio 1.2.2
Description: Ubuntu 14.04.2 LTS
You can try disabling your lint checks by adding this code to your build.gradle
tasks.whenTaskAdded { task ->
if (task.name.toLowerCase().contains("lint")) {
task.enabled = false
}
}
Also you can use the --profile parameter to export a html report of what's taking so long in your build time
EDIT: removed the debug check
We had a similar problem: our build took about 4 minutes. After some research, we get next steps:
Try turning on offline work in your Gradle settings (Ctrl + Alt + S for the Settings, then Build Tools -> Gradle settings). Usually, we do not update libraries so often and Gradle will not check that they are up to date.
Multidex increases the time for build: Android documentation. To mitigate this, you can add additional flavors for debug and production builds, setting for debug builds minSdk = 21 (suggestion from official site).
If you have several modules in project, try to flatten them into one or at least extract "Library project" and connect it as .aar
As a result, our build time decreased to about minute - minute and a half. Hope these suggestions will help you too.
And if we will talk about small changes, starting from Android Studio 2.0, it is possible to use Instant run and Incremental build, which makes small changes in method or layout even faster.
Understand that Gradle Build has nothing to do with your CPU Speed or RAM. The reason why the Build gets slower in your PC is due to your Internet Connection. The build can take nearly ~100 MB approx. of data to complete it's process. Even if your Internet Connection is fast and your connection's PING speed is slow, it takes more time.

Why does building app in Android Studio take so long?

I've been using Android Studio for the past few months, and I've noticed that it's a lot slower to build an app than Eclipse. For instance, in Eclipse, if I were to modify a line of code and then re-build, it would take less than 10 seconds. The same act in the same project in Android Studio causes the build process to last more than a minute (sometimes 2). Is there any setting or configuration I can change to help with this? Is it gradle related?

Categories

Resources