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.
Because you can combine Groovy and KTS build files in a project, a simple way to start converting your project to KTS is to select a simple build file, like settings. gradle , rename it to settings. gradle. kts , and convert its contents to KTS.
Gradle is a build system that is very commonly used in the Java, Android, and other ecosystems. It is the default choice for Kotlin/Native and Multiplatform when it comes to build systems.
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:
ext.kotlin_version
is not valid Kotlin syntax, use square brackets 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) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With