Hanbit the Developer

[Android] Cleaning gradle using KTS(Kotlin DSL), Version Catalogs(toml) 본문

Mobile/Android

[Android] Cleaning gradle using KTS(Kotlin DSL), Version Catalogs(toml)

hanbikan 2023. 3. 8. 16:56

KTS and version catalog make our gradle file clear. And KTS's mainly used in modern android projects like Sunflower, nowinandroid.

I used them to clear my gradle files and to practice Kotlin DSL. Let's see below images. The left one is my old gradle file, and the right one is the new one.

This article covers how I migrated gradle files using KTS, version catalog.

 

Migrating to KTS

1. Rename gradle files with '.kts' extension.

build.gradle -> build.gradle.kts

setting.gradle -> setting.gradle.kts

 

2. Modify the kts files.

I had to make those files for 'KTS rule'. The contents of the rule is here.

 

Using toml(version catalog)

1. Setup for toml

I added the below code to use toml file in setting.gradle.kts.

versionCatalogs {
    create("libs") {
        from(files("libs.versions.toml"))
    }
}

 

2. Write libs.versions.toml

Let's assume that we have the below code in our gradle file.

implementation("androidx.activity:activity-compose:1.4.0")

 

Write some codes in libs.versions.toml like:

[versions]
activityCompose = "1.4.0"
// ...

[libraries]
"activity-compose" = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
// ...

 

Then you can finally simplify the implementation code like below.

implementation(libs.activity.compose)