通过使用kotlin-gradle中的子项目来“未解决的参考:实现”

时间:2019-11-02 07:26:00

标签: 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"
    }
}

但是我得到了例外:

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<KotlinCompile> {
                              ^ Unresolved reference: KotlinCompile

  Line 19:     tasks.withType<KotlinCompile> {
                                             ^ 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

我认为这是一个简单的语法错误,但是我找不到它...

非常感谢!

2 个答案:

答案 0 :(得分:3)

不确定如何通过将plugins { }嵌套在subprojects { }下的plugins {}块中来也不会出错:

  

plugins {}块还必须是构建脚本中的顶级语句。它不能嵌套在另一个结构(例如if语句或for循环)中。

因此,要解决您的问题,请将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

您可以阅读PluginDependenciesSpec的{​​{3}}中plugins {}部分的更多信息,val implementation by configurationspublic class DB_helper extends SQLiteAssetHelper { private static final String DATABASE_NAME = "myDb.db"; private static final int DATABASE_VERSION = 1; public DB_helper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } } 块的 type / scope。

您可以在Limitations of the plugins DSL

中详细了解 SQLiteDatabase mydb = new DB_helper(appContext).getWritableDatabase(); 部分

答案 1 :(得分:1)

子模块不必显式定义插件,但必须由谁来做是父模块,并且在settings.gradle.kts (project)中定义存储库以识别子模块的依赖关系也很重要

settings.gradle.kts(项目)

pluginManagement {
    repositories {
        maven("https://dl.bintray.com/kotlin/kotlin-eap")
        mavenCentral()
        maven("https://plugins.gradle.org/m2/")
    }
}
rootProject.name = "project"

include("app")

build.gradle.kts(项目)

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

        plugins {
            java
            kotlin("jvm") version "1.4-M3" apply false
        }

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

    repositories {
        maven("https://dl.bintray.com/kotlin/kotlin-eap")
        mavenCentral()
    }

    val implementation by configurations

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

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

build.gradle.kts(:app)

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

GL