Table of contents
Android configuration files have used Gradle Groovy since the beginning, but in recent years with the appearance of Kotlin, these files can be written taking advantage of the full power of this language.
Therefore, Gradle has decided to support Kotlin for these configuration files using the Kotlin DSL variant.
Here are the changes you need to migrate from Groovy to Kotlin easily:
Build Gradle file (:app)
Adapt dependencies in build.gradle
Replace all single quotation marks (' ') by double quotation marks (" ")
You must indicate parentheses where there are function calls.
Adapt plugins inside the files
- The plugins will now go inside a
plugins
function together with theid
function
Build types configuration
Now the buildTypes must be defined with the function
getByName
Verify that the property values are now assigned with an
=
Defaults Configs
- Package configurations, SDK and version names have minor changes
Build Gradle file (MyApp)
Adapt classpath dependencies
- Again note double quotation marks and the use of parentheses.
Changes in Task Clean function
Adapt Settings.gradle
The settings.gradle file should be written in the following way
Finally, change the extension in all files
You must add to each file the extension .kts
, the files would look like this:
app/build.gradle.kts
build.gradle.kts
settings.gradle.kts
It's as simple as that!
BONUS ZONE
Load information from keystore
To generate the release
signature using the Keystore file you can load the variables in the following way
Load variables from local.properties
and exclude library within a dependency
If you have any properties that you need to load from the local file or you need to exclude some library inside of a dependency, you can do it in the following way
That's all for today, I hope your migration to Kotlin will be made easier with this article ❤.
If you liked the article, feel free to give a thumbs up. You will motivate me to write more articles like this one. Thanks!