当前位置: 首页 > 知识库问答 >
问题:

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

柴彬
2023-03-14

我正在尝试使用静态编程语言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
    

依赖关系的内容:

/**
 * 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
    }
}
...

我还想指出,android{…}块在模块gradle文件中无法识别,但我认为这可能是因为编译失败。

共有1个答案

海嘉赐
2023-03-14

您在src/main/java下有Kotlin文件。它们应该位于src/main/kotlin中,您需要在buildSrcGradle文件中包含kotlin构建支持(可能您有这个,您没有显示 中的内容)

plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
}
 类似资料: