What's new in Gradle 5.1 (for mobile developers)
Tags: gradle, android, kotlin, build
Published: 2020-03-22 09:13:11

[Gradle 5.1](https://docs.gradle.org/5.1/release-notes.html) came in January 2019 as a first minor update of Gradle 5.0 with few feature updates.

## Repository to dependency matching ([docs](https://docs.gradle.org/5.1/release-notes.html#repository-to-dependency-matching))
Official documentation states that:
> It is now possible to match repositories to dependencies, so that Gradle doesn't search for a dependency in a repository if it's never going to be found there.
```
    repositories {
        maven(url = "https://nexus.mycompany.com") {
            content {
                includeGroupByRegex("""com.mycompany.*""")
            }
        }
    }
```
Well, that's nice. You know that your company internal dependencies can't be in any other public repository, so you tell Gradle and it saves some time when searching for them. But not every project have private dependencies in company internal repository.
However this feature has yet another use case. Maybe you read the article ["A Confusing Dependency"](https://blog.autsoft.hu/a-confusing-dependency/) by Márton Braun about how easy is to populate fake library to `jcenter()` repository. When you just do not trust some repository enough to use it in general but you still need it because the library you want is not available anywhere else, you can at least limit the use of such repository:
```
    repositories {
        jcenter {
            content {
                includeGroup("org.koin") // Koin is not available anywhere else
            }
        }
    }
```

## Gradle Kotlin DSL 1.1 ([docs](https://github.com/gradle/kotlin-dsl-samples/releases/tag/v1.1.0))
Kotlin DSL 1.1 is a first update of Kotlin DSL feature originally introduced in Gradle 5.0 (you can read more about Kotlin DSL in my [previous post](https://dev.to/xsveda/what-s-new-in-gradle-5-0-for-mobile-developers-45en)).
The update is mainly focused on polishing the DSL syntax.
We can now use lambdas in the Gradle APIs, we have type-safe accessors for `dependencies`, and `kotlin-dsl` plugin tasks are now all cacheable which positively affects build speed.

## Configuration avoidance for Tasks ([docs](https://docs.gradle.org/5.1/release-notes.html#configuration-avoidance-for-tasks))
From time to time you need to configure custom Gradle task as part of the build and unfortunately there is not a single way how to do this.
Historically when one defined a new task it has been configured eagerly (`create`) with all its dependencies in every build you run. And if you did not call such task during the Gradle run all the time spent on its configuration was wasted.
As a part of Gradle's continuous effort to speed up the build speeds a "Configuration avoidance APIs" were introduced as Preview in Gradle 4.9 and became stable in Gradle 5.1.
It allows us to configure a task in a lazy manner (`register`). In fact, you won't configure a `Task` this way but just a `TaskProvider` that creates a `Task` only once it is really needed during a Gradle run.
It might sound complicated but the only thing you need to do is to replace
```
   tasks.create("myTask", MyTask) {...}
```
by
```
   tasks.register("myTask", MyTask) {...}
```
And that's it! :tada::tada::tada:
If you are interested in more details, I recommend you to read this [blog post](https://blog.gradle.org/preview-avoiding-task-configuration-time).