Convert an existing groovy build.gradle file into a kotlin based build.gradle.kts

14,409

Of course renaming won't help. You'll need to re-write it using Kotlin DSL. It is similar to Groovy, but with some differences. Read their docs, look at the examples.

In your case, the issues are:

  1. ext.kotlin_version is not valid Kotlin syntax, use square brackets
  2. All Kotlin strings use double quotes
  3. Braces are required around parameters for most function calls (there are exceptions, like infix functions)
  4. Slighlty different task management API. There are different styles available. You can declare all the tasks in tasks block as strings, or use a single typed function, as in the example below.

Take a look at the converted top-level build.gradle.kts:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext["kotlin_version"] = "1.1.51"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath ("com.android.tools.build:gradle:3.1.0-alpha01")
        classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:${ext["kotlin_version"]}")
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

task<Delete>("clean") {
    delete(rootProject.buildDir)
}
Share:
14,409

Related videos on Youtube

Exitare
Author by

Exitare

Updated on April 16, 2020

Comments

  • Exitare
    Exitare about 4 years

    my project has two different build.gradle files written with groovy Syntax. I´d like to change this groovy written gradle file into a gradle file written with Kotlin Syntax (build.gradle.kts).

    I´ll show you the root project build.gradle file.

        // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
        //ext.kotlin_version = '1.2-M2'
        ext.kotlin_version = '1.1.51'
        repositories {
            google()
            jcenter()
    
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.0-alpha01'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
            mavenCentral()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    I tried several "ways" i found in the internet, but nothing worked. Renaming the file, which is obviously not the Solution, didn´t help. I´ve created a new build.gradle.kts file in my root project but the file isn´t shown in my project. Also gradle didn´t recognize the new file.

    So my question is: How can i transform my groovy build.gradle file into a kotlin build.gradle.kts and add this new file into my existing project?

    Thanks for your help.

  • Exitare
    Exitare over 6 years
    Thanks for the fast and clear answer. I ´ve implemented the top gradle file and it works well. Thx for that. I´ve read the Docu and had a look at the Samples.
  • Exitare
    Exitare over 6 years
    In fact, i don´t get it all. If i try to convert my 2nd file the same way as you "told" to do. there are several errors like "unresolved References" etc. For example: This is my code: android { compileSdkVersion ("28") buildToolsVersion ("27.0.0") } but android and the following is an unknow expression. What is my failure? Is there a docu as well. I´m so sorry for being so dumb. I´m new to this whole area.
  • madhead
    madhead over 6 years
    We're here to help. Please, create another question with the issues you have with other file. I guess, most probably you'll need to use configure<Android...Extension> {} (I do not know the class), instead of android, but let us see it all.
  • Josh C.
    Josh C. over 4 years
    Is there any advantage of switching to .kts? I'm trying to convert my build.gradle now, and I keep stubbing my toe.
  • madhead
    madhead over 4 years
    Probably if you're fine with Groovy then there will be no advantage. Kotlin scripts will be even a little bit slower. However, Kotlin scripts provide better IDE support due to static typing. Moreover (that's totally my IMHO): Groovy is loosing its positions and we'll see more Kotlin support and Kotlin-centric features in Gradle in future.
  • aaddesso
    aaddesso over 4 years
    I want to point out that there's a [GradleKotlinConverter][1] script to automate some of the more common chores when converting groovy gradle files to kotlin. [1]: github.com/bernaferrari/GradleKotlinConverter