【问题标题】:Gradle Kotlin DSL unresolved references defined in buildSrc在 buildSrc 中定义的 Gradle Kotlin DSL 未解析的引用
【发布时间】:2022-01-06 08:22:36
【问题描述】:

我正在尝试使用 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 { ... } 块,但我认为这可能是因为编译失败。

【问题讨论】:

    标签: android kotlin gradle gradle-kotlin-dsl


    【解决方案1】:

    src/main/java 下有 Kotlin 文件。它们应该在 src/main/kotlin 中,并且您需要在 buildSrc Gradle 文件中包含 Kotlin 构建支持(也许您有这个,您没有显示 buildSrc/build.gradle.kts 中的内容)

    plugins {
        `kotlin-dsl`
    }
    
    repositories {
        mavenCentral()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-16
      • 2022-12-09
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      相关资源
      最近更新 更多