Better dependency management in Android Studio 3.5 with Gradle buildSrcVersions
Tags: android, java, kotlin, showdev
Published: 2019-08-21 14:59:46

Android Studio 3.5 is released! Have you updated yet? Go to [https://developer.android.com/studio](https://developer.android.com/studio), I will wait.

Personally, I'm most excited not by any new feature but by [a bugfix](https://issuetracker.google.com/issues/123032843) which makes relevant again for android developers my open-source project for managing dependencies.

{% github jmfayard/buildSrcVersions %}
 

This project was born out of a growing disgust for this dark corner of Android Studio:

> 🙀 Gradle project sync failed. [**Try again**] 🙀

<img src="https://github.com/jmfayard/buildSrcVersions/raw/master/doc/try-again.png" >

**"Try Again"** is kind of misnomer here.

## Android Studio support for editing the Gradle files was bad 😿

Android Studio is in general a magnificent IDE, and integration with [Gradle](https://gradle.org/) is an essential component of it. So you would expect to have great tooling support for editing your build files.

Instead, this error message is the exact moment where it all breaks down. Instead of _Try Again_, the wording should be more something like: "_You have some error, somewhere, and you are on your own to fix it. Good luck, and tell me when you **think** you are done._". 

The developer experience becomes more like something you would expect from the JavaScript world. You copy/paste some stuff from some website, hopefully at the right place (which is non-trivial!). Hoping for the best, you run it and a cryptic error message is thrown. Rince and repeat.

Why is the tooling so bad? Pretty much the same reasons that in the javascript world: we are doing lots of meta-programming stuff (with Gradle plugins) using a dynamic language (Groovy).

Gradle has the right long-term plan to overcome this problem: [write less Groovy and more Kotlin](https://dev.to/jmfayard/how-kotlin-makes-editing-your-gradle-build-less-frustrating-232l).

But while Kotlin is probably the future, Groovy is the present. And we need some incremental progress to help us here and now with our existing builds.

## The Gradle "buildSrc" module 😼 


With Android Studio 3.5, the Gradle "buildSrc" module comes to the rescue.

> The "buildSrc" is a Gradle module where you can write Kotlin code like usual (with full tooling support). That code is then be available to all your build files - not your final application. Crucially IntelliJ IDEA and Android Studio have good support for calling it from `build.gradle`

You can put all the things there that are a bit complicated or should be reused, and keep your `build.gradle` files nice and tidy.

The main use-case I had in mind was to replace those Groovy [libraries.gradle files](https://github.com/abbas-oveissi/SearchMovies/blob/607ce1c6f9aa48669ab1b91f8824e9251f2a1fa5/libraries.gradle) we used to write:

![](https://miro.medium.com/max/1278/1*1Neqbi_bfoUscWrxhu2NbA.png)

That's quite straightforward but... do I have to write all this code manually?

Laziness is one of the three great virtues of a programmer, so I wrote a plugin which asks Gradle which dependencies my project is using and let the [KotlinPoet](https://github.com/square/kotlinpoet) generate those files.


## Gradle buildSrcVersions 😸 

Install the plugin by editing your root `build.gradle(.kts)` file like this:

{% gist https://gist.github.com/jmfayard/4db385d27b41cf2e14b57067cc825df2 file=2.gradle %}

The plugin adds a task to your build, also called `:buildSrcVersions`. 

Run it like this:

{% gist https://gist.github.com/jmfayard/4db385d27b41cf2e14b57067cc825df2 file=3.sh %}

Sync your Gradle build.

You can now start to replace your magic strings with the properties available in `Libs.kt`

![](https://github.com/jmfayard/buildSrcVersions/raw/master/doc/Libs.gif)

Finally the IDE tooling we deserve:

- auto-completion
- jumping to definition
- ...

But the more important half of what the plugin do is looking automatically for you what are the newer versions available for all your dependencies!

## 😻 Search for dependencies updates 

Upgrade dependencies tend to be tedious. But it is really important to manage them properly. Here are some reasons:

- Few projects have versioned documentation: `master/README.md` is their documentation. This means you can waste time trying out snippets that do not work in the version you are using.
- When you open an issue, you will often be asked: Can you reproduce this problem in the latest version?
- On the other hand, you don't want to break your app because there was a breaking change and you didn't bother to read the CHANGELOG documenting it.

This plugin inherits from [ben-manes/gradle-versions-plugin](https://github.com/ben-manes/gradle-versions-plugin) the feature to automatically determine which dependencies have updates.

When you decide you want to update some dependencies, run the plugin again:

{% gist https://gist.github.com/jmfayard/4db385d27b41cf2e14b57067cc825df2 file=4.sh %}

The file `Versions.kt` is regenerated with a **comment** indicating which new version is available.

{% gist https://gist.github.com/jmfayard/4db385d27b41cf2e14b57067cc825df2 file=5.kt %}

At that point, your build is exactly the same. Commit.

Don’t waste your time finding out manually what is the exact latest stable version available. Instead, spend it deciding whether you want or not to update the version. The format is optimized to update with one DELETE key:

![Press DELETE to update](https://miro.medium.com/max/578/1*wUivl-vqs-btUhJjmrjuAA.png)

This screencast shows this in action:

![](https://github.com/jmfayard/buildSrcVersions/raw/master/doc/Versions.gif)

Once you are done:

- Sync your Gradle build
- Run your unit tests and otherwise verify that it works.
- Commit.


## Show me an example! 😽 

I submitted a pull-request to `dev.to`'s Android app

{% github https://github.com/thepracticaldev/DEV-Android/pull/44 %}

## New in Android Studio 3.5 🆕 ?

Let me backtrack a bit: the `buildSrc` is technically not a new thing. Gradle made it available a number of years ago and is widely used in JVM/IntelliJ projects. But it may well be new _to you_: the IDE integration that makes it a game changer launched in Android Studio 3.2. Without enough publicity. And then it got broken immediately after in Android Studio 3.3 and 3.4 with [bug #123032843](https://issuetracker.google.com/issues/123032843). 

So you are excused if you missed the news, and now is a good time to give it a try. 

As a conclusion, I will share my favorite reaction after first posting this story:

![](https://miro.medium.com/max/349/1*5yFYpHQW9t_6KR8Q4v1CIQ.png)


## References 📚

Github

{% github jmfayard/buildSrcVersions %}