将现有 groovy build.gradle 文件转换为基于 kotlin 的 build.gradle.kts
Convert an existing groovy build.gradle file into a kotlin based build.gradle.kts
我的项目有两个不同的 build.gradle 文件,使用 groovy 语法编写。
我想将这个 groovy 编写的 gradle 文件更改为使用 Kotlin 语法 (build.gradle.kts).
编写的 gradle 文件
我将向您展示根项目 build.gradle 文件。
// 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
}
我尝试了几个在互联网上找到的"ways",但没有任何效果。重命名文件,这显然不是解决方案,没有帮助。我在我的根项目中创建了一个新的 build.gradle.kts 文件,但该文件没有显示在我的项目中。
另外 gradle 无法识别新文件。
所以我的问题是:如何将我的 groovy build.gradle 文件转换为 kotlin build.gradle.kts 并将这个新文件添加到我现有的项目中?
感谢您的帮助。
当然重命名也无济于事。您需要使用 Kotlin DSL 重写它。它类似于 Groovy,但有一些差异。 Read their docs, look at the examples.
对于您的情况,问题是:
ext.kotlin_version
是无效的 Kotlin 语法,请使用 square brackets
- 全部Kotlin strings使用双引号
- most function calls (there are exceptions, like infix functions)
的参数需要大括号
- 任务管理略有不同API。有不同的款式可供选择。您可以声明 all the tasks in
tasks
block as strings,或使用单一类型的函数,如下例所示。
看看转换后的顶层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)
}
我的项目有两个不同的 build.gradle 文件,使用 groovy 语法编写。 我想将这个 groovy 编写的 gradle 文件更改为使用 Kotlin 语法 (build.gradle.kts).
编写的 gradle 文件我将向您展示根项目 build.gradle 文件。
// 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
}
我尝试了几个在互联网上找到的"ways",但没有任何效果。重命名文件,这显然不是解决方案,没有帮助。我在我的根项目中创建了一个新的 build.gradle.kts 文件,但该文件没有显示在我的项目中。 另外 gradle 无法识别新文件。
所以我的问题是:如何将我的 groovy build.gradle 文件转换为 kotlin build.gradle.kts 并将这个新文件添加到我现有的项目中?
感谢您的帮助。
当然重命名也无济于事。您需要使用 Kotlin DSL 重写它。它类似于 Groovy,但有一些差异。 Read their docs, look at the examples.
对于您的情况,问题是:
ext.kotlin_version
是无效的 Kotlin 语法,请使用 square brackets- 全部Kotlin strings使用双引号
- most function calls (there are exceptions, like infix functions) 的参数需要大括号
- 任务管理略有不同API。有不同的款式可供选择。您可以声明 all the tasks in
tasks
block as strings,或使用单一类型的函数,如下例所示。
看看转换后的顶层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)
}