Gradle Kotlin DSL中buildSrc中定义的未解决引用。

4

我正在尝试使用 Kotlin DSL,但无法让它识别我在 buildSrc 中定义的对象。虽然 IDE 能够解析它们,但当我编译代码时它却不起作用。

这是我的项目结构:

build.gradle.kts
settings.gradle.kts
+buildSrc
    build.gradle.kts
    +src
        +main
            +java
                Dependencies.kt
                Versions.kt
+module1
    build.gradle.kts
+module2
    build.gradle.kts
    

Dependencies.kt的内容:

/**
 * To define plugins
 */
object BuildPlugins {
    val gradle by lazy { "com.android.tools.build:gradle:${Versions.gradlePlugin}" }
    val kotlinGradle by lazy { "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}" }
    val safeArgs by lazy { "androidx.navigation:navigation-safe-args-gradle-plugin:${Versions.safeArgs}" }
}

/**
 * To define dependencies
 */
object Deps {
    val appCompat by lazy { "androidx.appcompat:appcompat:${Versions.appCompat}" }
    val core by lazy { "androidx.core:core-ktx:${Versions.core}" }
    val timber by lazy { "com.jakewharton.timber:timber:${Versions.timber}" }
    val kotlin by lazy { "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlin}" }
    val material by lazy { "com.google.android.material:material:${Versions.material}" }
    val constraintLayout by lazy { "androidx.constraintlayout:constraintlayout:${Versions.constraintLayout}" }
}

项目级别的 build.gradle.kts 文件(首先出现失败的文件):

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        BuildPlugins.gradle
        BuildPlugins.kotlinGradle
        BuildPlugins.safeArgs

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
...

我还想指出的是,在模块的Gradle文件中,android{...}块没有被识别,但我认为这可能是由于编译失败导致的。


嘿,你解决问题了吗? - Arst
1个回答

0

你在 src/main/java 目录下有 Kotlin 文件。它们应该在 src/main/kotlin 目录下,并且你需要在 buildSrc Gradle 文件中包含 Kotlin 构建支持(也许你已经添加了,但是你没有展示 buildSrc/build.gradle.kts 中的内容)。

plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接