我正在尝试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
依赖项的内容.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
}
}
...
我还想指出的是,android{…}块在模块gradle文件中无法识别,但我认为这可能是因为编译失败。
src/main/java
下有Kotlin文件。它们应该在src/main/kotlin
中,您需要在buildSrc
Gradle文件中包含Kotlin构建支持(也许您有这个,但您没有显示buildSrc/build.gradle.kts
中的内容(
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}