I want to split my project into subprojects. The default Gradle setting from the IntelliJ IDE is:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.3.50"
}

group = "project"
version = "0.0.1-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

这种设置可以编译.但我不想在每个子项目中重复这些代码.所以我把build.gradle.kts改成了

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

subprojects {
    plugins {
        kotlin("jvm") version "1.3.50"
    }

    group = "project"
    version = "0.0.1-SNAPSHOT"

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation(kotlin("stdlib-jdk8"))
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions.jvmTarget = "1.8"
    }
}

but I get the exception:

e:c:[.]\build.gradle.kts:1:12:未解析引用:JetBrains e: C:[.]\build.gradle.kts:16:9:未解析引用:实现 E:C:[.]\build.gradle.kts:19:20:未解析引用: KotlinCompile e:c:[.]\build.gradle.kts:19:35:类型不匹配: 推断的类型是()-&gt;单位,但类&lt;TypeVariable(S)!&gt;!是意料之中的 E:C:[.]\build.gradle.kts:20:9:未解析引用:kotlinOptions

FAILURE: Build failed with an exception.

  • 其中:生成文件'C:[…]\建造.格雷德尔.kts系列:1

  • 错误:脚本编译错误:

    第01行:导入组织.喷气式飞机.Kotlin .格雷德尔.任务.科特尔不完整文件

    第16行:实现(kotlin("stdlib-jdk8"))

    第19行:任务.withType{

    Line 19: tasks.withType { ^ Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected

    Line 20: kotlinOptions.jvmTarget = "1.8" ^ Unresolved reference: kotlinOptions

5个错误

  • try :使用--stacktrace选项运行以获取堆栈跟踪.使用--info或--debug选项运行以获得更多日志(log)输出.运行--扫描以获得完整的洞察力.

  • Get more help at https://help.gradle.org

BUILD FAILED in 1s

我想有一个简单的语法错误,但我找不到.

推荐答案

不确定将plugins { }块嵌套在subprojects { }下(如Limitations of the plugins DSL中所述)是否也会出现错误:

The plugins {} block must also be a top level statement in the buildscript. It cannot be nested inside another construct (e.g. an if-statement or for-loop).

因此,要解决问题,请将plugins {}移至顶部,并强制应用subprojects {}块中的插件:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.3.50" apply false
}

subprojects {
    apply {
        plugin("org.jetbrains.kotlin.jvm")
    }

    group = "project"
    version = "0.0.1-SNAPSHOT"

    repositories {
        mavenCentral()
    }

    val implementation by configurations

    dependencies {
        implementation(kotlin("stdlib-jdk8"))
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions.jvmTarget = "1.8"
    }
}

You can read more about the apply false part in the Method details of PluginDependenciesSpec which is the type/scope of the plugins {} block.

你可以在Understanding what to do when type-safe model accessors are not available篇文章中阅读更多关于val implementation by configurations部分的内容

Kotlin相关问答推荐

为什么onEach不是挂起函数,而Collect是?

为什么Kotlin函数参数名会 destruct 方法调用?

编译后的JavaFX应用程序立即以静默方式崩溃

可以从背景图像中点击图标吗?

Webpack 配置未应用于 kotlin 多平台react 项目

如何处理基于枚举提前返回的 forEach 循环,Kotlin 中的一条路径除外

Jetpack BottomNavigation - java.lang.IllegalStateException:Already attached to lifecycleOwner

如何使用 Kotlin KClass 属性 simpleName 生成空值

使用 Kotlin 创建新目录,Mkdir() 不起作用

Hilt Activity 必须附加到 @AndroidEntryPoint 应用程序

Kotlin 具体化的泛型不会按计划保持类型

哪里可以找到aapt2日志(log)?

如何在使用 Gradle 的 AppEngine 项目中使用 Kotlin

Kotlin解构when/if语句

kotlin 委托有什么用?

什么是 Kotlin 等价于 Class<?>

在android java类中使用Kotlin扩展

尾随 lambda 语法(Kotlin)的目的是什么?

在 Kotlin 中声明 Byte 会出现编译时错误The integer literal does not conform to the expected type Byte

Android room DAO 接口不适用于继承