Migrate Gradle from Groovy to Kotlin DSL
Timeline:
From the Android Studio Giraffe, all the new project is going to use Kotlin DSL, where the extension of Gradle will change to build.Gradle.kts with syntax changes.
Q. Why Kotlin DSL?
- Integration with Kotlin Language —
Most of the apps are using Kotlin language, what if the Gradle script is changed to Kotlin DSL?
— Easy to Write, easy to understand, and our app has language consistency
2. Type Safety — Kotlin DSL provides better compile-time checks and error detection, this will reduce the chance of run time error.
3. IDE Support — Kotlin is already adopted by Android Studio and with the Kotlin DSL, it's easy to navigate and easy to refactor, and it will be well-known by developers to write the Kotlin DSL, a good and productive Dev experience.
4. Concise and Readable Syntax — Kotlin DSL have more concise and expressive syntax
5. Static Compilation — Kotlin DSL is statically compiled, which means it performs compilation checks at build time, it will enable early error detection, so we will have less chance to get the error at runtime.
6. Gradle Interoperability — if you remember Java to Kotlin migration, it's the same case here.
You can gradually migrate Groovy-based Gradle DSL to Kotlin DSL. Here is the flexibility to mix both the DSL in the same project. It will provide enough time for the developer to do the migration.
The conclusion is: Kotlin DSL offers a more robust, type-safe, and modern approach to configuring build scripts compared to the Groovy-based Gradle DSL.
It provides a better developer experience and enhances the maintainability and readability of build scripts.
OK, we understand why we need to do the migration and when we need to do it.
But now, The next question is:
Q How We should start the migration?
I am going to cover the important things which we need to take care of while doing the migration because point to point Migration doc you can get from here
- Gradle version — we need to use the latest version of Gradle
- You must run Gradle with Java 8 or higher. Java 7 is not supported.
- Create a Kotlin Build Script — Start creating a new Kotlin build script(build.gradle.kts) alongside the existing Groovy build script.
- Remember the syntax difference, to write this you can refer to the Kotlin DSL syntax documentation.
- Once you will confident enough, you can say goodbye to the Groovy Build script (remove it from the project).
Happy coding... See you in the next article (Why Gradle is important in Android Project:: link is here)