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

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](https://docs.gradle.org/5.0/release-notes.html) 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 :tada::tada::tada:

## Gradle Kotlin DSL ([docs](https://docs.gradle.org/current/userguide/kotlin_dsl.html))

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 :sunny: and :rainbow:, but it just started with version `1.0` and will receive updates in future Gradle versions.

## Build caching ([docs](https://docs.gradle.org/current/userguide/build_cache.html))
Build Cache feature has been introduced already in [Gradle 4.0](https://docs.gradle.org/4.0/release-notes.html), 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](https://docs.gradle.org/current/userguide/java_plugin.html#sec:incremental_compile))
Stable enough and it Just Works<sup>TM</sup>!

## Incremental annotation processing ([docs](https://docs.gradle.org/current/userguide/java_plugin.html#sec:incremental_annotation_processing))
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](https://kotlinlang.org/docs/reference/kapt.html#incremental-annotation-processing-since-1330). But don't forget that this feature must be first supported by [annotation processor plugin itself](https://docs.gradle.org/5.0/userguide/java_plugin.html#making_an_annotation_processor_incremental).
Please read [What's new in Android Gradle Plugin 3.5](https://dev.to/xsveda/what-s-new-in-android-gradle-plugin-3-5-157k) post for more information about the history and proper configuration of incremental annotation processing with Java and Kotlin.

## BOM support ([docs](https://docs.gradle.org/5.0/userguide/managing_transitive_dependencies.html#sec:bom_import))
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")
}
```
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")
}
```

## Periodic Gradle cache cleanup ([docs](https://docs.gradle.org/5.0/userguide/directory_layout.html#dir:gradle_user_home:cache_cleanup))
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.