DEV Community

Pavel Sveda
Pavel Sveda

Posted on • Updated on

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

In this series I'll go through the list of recent Gradle versions and pick the most useful features for the users of Gradle build system (a.k.a. developers), but ignoring updates that target plugin creators and large system admins. The features will be picked in context of a mobile (Android) app project, but most of it is applicable also to any JVM related or Kotlin Multiplatform projects.

For the first post in this series we will go back in time to the end of 2018 when Gradle 5.0 has been released. The reason is that Gradle 5.x versions are still very recent, but more important, for the first time in history, Gradle 5.0 comes with an alternative to Groovy for writing configuration scripts -> Kotlin 🎉🎉🎉

Gradle Kotlin DSL (docs)

This is big! Actually, Gradle Kotlin DSL is not just about adding another programming language for writing configuration scripts. Introducing of this feature is actually a game changer for many teams I know to give Gradle a try, and these are the main reasons why:

IDE & tooling support

Yes, with Gradle Kotlin DSL the IDE can actually assist you with writing the script, you can use refactoring or read the Gradle API code documentation!!!
Well, to be fair, some of those features were introduced to the top-used IDEs also for Groovy DSL, but those were just few chosen features that were backed directly to the IDEs that not always work well.
With Kotlin it's a different story. Gradle Kotlin scripts (*.gradle.kts files) are processed on the fly when you write the code so the IDE works in very similar way as when you write your app's code and all the nice stuff we will love just works here.

Type safety

No more blindly trying to write some Groovy code and running the Gradle over and over again still failing due to wrong type/method call error. Now you know what plugin class you are dealing with, you can overview its methods, and in case of an error a syntax highlighting will guide immediately.

Gradle API polishing

Gradle was built on top of Groovy "magic" features that allows you to use the same syntax for calling properties and functions, shortcuts for working with collections or methodMissing concept.
But when a thoughts about introducing new language arise it was obvious that it won't be possible without a solid and kind of language "independent" API. Don't take me wrong, Gradle is a JVM tool and always will be (at least in near future), but the new Gradle Java API standardization is kind of "neutral" for these purposes, and helps the Gradle eco-system a lot.

Gradle Kotlin DSL ain't all ☀️ and 🌈, but it just started with version 1.0 and will receive updates in future Gradle versions.

Build caching (docs)

Build Cache feature has been introduced already in Gradle 4.0, but Gradle 5.0 enables it in many more scenarios so it can rapidly speed up your build without not much effort (at least for your local builds).

Incremental Java compilation (docs)

Stable enough and it Just WorksTM!

Incremental annotation processing (docs)

Introduces new annotationProcessor configuration so Gradle can handle Java annotation processors correctly to avoid needless re-compilations. To make it work with Kotlin kapt annotation processors you need to use Kotlin 1.3.30 or newer. But don't forget that this feature must be first supported by annotation processor plugin itself.
Please read What's new in Android Gradle Plugin 3.5 post for more information about the history and proper configuration of incremental annotation processing with Java and Kotlin.

BOM support (docs)

Gradle 5.0 introduces a notion of a platform that is used for consuming Maven BOM files. This helps with introducing libraries like Firebase Android SDK where one need to be careful about selecting right components with compatible versions. With BOM support you can replace

dependencies {
  implementation("com.google.firebase:firebase-analytics:17.2.2")
  implementation("com.google.firebase:firebase-auth:19.2.0")
  implementation("com.google.firebase:firebase-firestore:21.3.1")
}
Enter fullscreen mode Exit fullscreen mode

with

dependencies {
  implementation(platform("com.google.firebase:firebase-bom:24.5.0"))
  implementation("com.google.firebase:firebase-analytics")
  implementation("com.google.firebase:firebase-auth")
  implementation("com.google.firebase:firebase-firestore")
}
Enter fullscreen mode Exit fullscreen mode

Periodic Gradle cache cleanup (docs)

Gradle binaries and project dependency artifacts in Gradle cache are checked every day and removed if not used for last 30 days. Neat!

And that's it! Stay tuned for the next posts por this series.

Top comments (0)