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).

我将向您展示根项目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
}

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.

所以我的问题是:如何转换groovy构建.将文件升级到kotlin版本中.格雷德尔.kts并将这个新文件添加到我现有的项目中?

谢谢你的帮助.

推荐答案

当然,重命名也无济于事.您需要使用Kotlin DSL重写它.它类似于Groovy,但有一些不同之处.Read their docs,看看the examples.

In your case, the issues are:

  1. ext.kotlin_version is not valid Kotlin syntax, use square brackets
  2. 所有Kotlin strings个都使用双引号
  3. Braces are required around parameters for most function calls (there are exceptions, like infix functions)
  4. Slighlty不同的任务管理API.有不同的款式可供 Select .您可以声明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)
}

Kotlin相关问答推荐

将基于注册的服务转换为流

在KMM合成多平台中创建特定于平台的视图

Kotlin编译器如何决定是否可以在任何给定点调用Suspend方法?

T和T有什么区别:任何>

Spring Boot 3:为Kotlin中的TestRestTemplate配置读取超时

禁用 Gradle执行消息

为什么 Kotlin 在将协变类型参数转换为不变类型参数时会产生 Unchecked Cast 警告?

Kotlin:调用 CoroutineScope.launch 与在协程内启动之间的区别

如何从 kotlin 函数中 Select 正确的枚举值

Kotlin 插件错误:无法为类 org.jetbrains.kotlin.gradle.tasks.KotlinCompile 生成代理类

Anko 中的水平线性布局

Map.mapTo 到另一个map

如何使用 Kotlin Coroutines 使 setOnClickListener debounce 1 秒?

主机名不能为空

Kotlin - 如果不为空,则使用修改后的 Obj props 覆盖 Obj props

从 java 活动 *.java 启动 kotlin 活动 *.kt?

如何在 IntelliJ IDEA 中禁用粘贴时将 Java 转换为 Kotlin?

如何将 Kotlin 的 `with` 表达式用于可空类型

为什么在 Kotlin 中return可以返回一个return?

Kotlin for assertThat(foo, instanceOf(Bar.class))