Migrate Android gradle from Groovy Scripts to KTS (Kotlin script)

This section will guide your step-by-step to convert or migrate your Android project's Gradle build script from Groovy script to Kotlin DSL

 When we create a project in Android (Arctic Fox 2020.3.1), We get the default Gradle Script setup for us which has 3 gradle file written in Groovy script

These 3 Gradle files are:
    1. The settings.gradle
    2. The root project’s build.gradle
    2. The app module’s build.gradle

Convert the settings.gradle file
1. Rename settings.gradle to settings.gradle.kts

You can do so by selecting the settings.gradle file, then go to Refactor
→ Rename File.

Then, just convert it to settings.gradle.kts. 

2. Fix the file syntax for “include”

You just need to change

include ':app'
to

include("app")

settings.gradle example file in groovy script:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven {
            url 'https://jitpack.io'
        }
        maven {
            url 'https://maven.google.com'
        }
        maven {
            url 'https://maven.fpregistry.io/releases'
        }
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots/"
        }
    }
}
rootProject.name = "Project name text"
include ':app'

'include ':news'
include ':events'
include ':home'
include ':profile'
include ':common'

example file becomes settings.gradle.kts in Kotlin script:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven(url = "https://jitpack.io")
        maven(url = "https://maven.google.com")
        maven(url = "https://maven.fpregistry.io/releases")    
        maven(url = "https://oss.sonatype.org/content/repositories/snapshots/")    
    }
}
rootProject.name = "Be better"
include("app")
include("news")
include("events")
include("home")
include("profile")
include("common")

No comments:

Post a Comment

Popular Posts