我正在尝试使用 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{...}块没有被识别,但我认为这可能是由于编译失败导致的。