gradle - 通过使用kotlin-gradle中的子项目"Unresolved reference: implementation"

标签 gradle kotlin

我想将我的项目拆分为多个子项目。 IntelliJ IDE的默认Gradle设置为:

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"
    }
}
但我得到一个异常(exception):

e: C:[...]\build.gradle.kts:1:12: Unresolved reference: jetbrains e: C:[...]\build.gradle.kts:16:9: Unresolved reference: implementation e: C:[...]\build.gradle.kts:19:20: Unresolved reference: KotlinCompile e: C:[...]\build.gradle.kts:19:35: Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected e: C:[...]\build.gradle.kts:20:9: Unresolved reference: kotlinOptions

FAILURE: Build failed with an exception.

  • Where: Build file 'C:[...]\build.gradle.kts' line: 1

  • What went wrong: Script compilation errors:

    Line 01: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile ^ Unresolved reference: jetbrains

    Line 16: implementation(kotlin("stdlib-jdk8")) ^ Unresolved reference: implementation

    Line 19: tasks.withType { ^ Unresolved reference: KotlinCompile

    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 errors

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • 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"
    }
}

您可以在apply false块的类型/范围PluginDependenciesSpecMethod details中阅读有关plugins {}部分的更多信息。

您可以在Understanding what to do when type-safe model accessors are not available中阅读有关val implementation by configurations部分的更多信息

关于gradle - 通过使用kotlin-gradle中的子项目"Unresolved reference: implementation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58669082/

相关文章:

android - 使用Google NearBy Messages API时“<App> is having trouble with Google Play Services. Please try again”

android - 膨胀类 android.webkit.WebView 时出错

java - 使用 Spock 框架运行 Android 单元测试

android - 如何在独立的Android SDK中使用Gradle?

android - Gradle位置不正确如何解决Android Gradle错误

android - 当按下 RecyclerView 内的按钮时如何下载文件?

java - 阻止 Android 中某些选定的菜单点以进行家长控制

xml - Kotlin:格式化字符串

android - 从 JCenter 迁移到 MavenCentral 后无法使用 Android 库。错误 : Could not find :unspecified:

Android Espresso 测试(用 Kotlin 编写)看不到主要类和包