Android Studio Pro-Tips for working with Gradle
Disclaimer: this is not yet-another-article about build performance. If that’s what you were looking for, go to https://guides.gradle.org/performance/ and https://developer.android.com/studio/build/optimize-your-build
Here are some tips I have found useful when working with Gradle in Android Studio.


Run unit tests before launching the app
So you made the effort to write unit tests? Good.
How useful are they?
Well, a test does not directly benefit the user.
Delivering bug fixes earlier benefits directly the user.
And that in turns depends on how often you run the unit tests.
I hope that you have a CI that runs your unit tests on each commit. But I wanted to go one step further, and run the unit tests on each installation.
As it turns out, it’s not hard to do in Android Studio


That makes the compile-install-run loop slower, but since it allows to catch errors earlier, you should hopefully have fewer iterations, and it will give you more motivation to have faster and better unit tests.
Gradle project sync failed: don’t try again!
When the Gradle sync fails, the experience of working with Android Studio is not super nice. Try again means something like Sync the world, then download all the jars, then also download all the javadocs, then re-index the project, then fail again, often without a clear error message.
So before I sync again, I prefer to have a much quicker sanity check by running a simple Gradle task like :tasks


- Menu
Run>Edit Configurations>+>Gradle - Name:
$ gradle tasks - Gradle project: root project
- Tasks:
tasks
Define a “Gradle” scope
Speaking of Gradle sync failures: as long as that happens, Android Studio is basically useless. So I’ve found very useful to be able to focus on only the Gradle files. IntelliJ IDEA and Android Studio have a powerful but hidden feature that allows doing that: Scopes


To set this up:
Preferences>Appearance & Behavior>Scopes>+- Name:
Gradle - Check
Share scope Pattern: copy-paste this
file:*.gradle||file:*.kts||file:*.properties||file[buildSrc]:src//*
A very useful feature, alas not discoverable at all. I have opened tickets to have them included by default in the IDE.
Gradle from the command-line: beware of the “incompatible daemons” problem!
I wish I didn’t have to write about this bug, but if you are using Gradle from the command-line, you really should check that you are not affected by issue #68374709
To check that, create a blank new project in Android Studio and run the Gradle :properties task from both Android Studio and the command-line.
Expect a bad user experience as long as you see that message.
Starting a Gradle Daemon, 1 incompatible could not be reused
Hopefully, the Android team will fix this dumb configuration problem that causes a lot of suffering.
Kotlin makes editing your Gradle build less frustrating
I used to have a love-hate relationship with Gradle because of the poor tooling support that makes it painful to maintain those build.gradle files.
While the build.gradle files are easy to read, they are quite hard to write. When you need to maintain them, you are mostly on your own. Usually, it doesn’t work for a while, and you don’t know why. Then at some point, you copy-paste the right solution, and it works, but you don’t really know why either.
Since then, Gradle and JetBrains have worked on allowing to configure Gradle with Kotlin instead of Groovy. This a larger subject so I dedicated a complete article on it
I present there a modest and practical strategy of adding just a bit of Kotlin to your existing build instead of going for a full rewrite.
Read it here: