DEV Community

Pavel Sveda
Pavel Sveda

Posted on • Updated on

What's new in Gradle 5.1 (for mobile developers)

Gradle 5.1 came in January 2019 as a first minor update of Gradle 5.0 with few feature updates.

Repository to dependency matching (docs)

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.*""")
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

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" 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
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Gradle Kotlin DSL 1.1 (docs)

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).
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)

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) {...}
Enter fullscreen mode Exit fullscreen mode

by

   tasks.register("myTask", MyTask) {...}
Enter fullscreen mode Exit fullscreen mode

And that's it! 🎉🎉🎉
If you are interested in more details, I recommend you to read this blog post.

Top comments (0)