[英]How to create a "base" gradle file in Kotlin DSL for multi-module project?
為了重用 gradle 文件中的代碼,我通常有一個用於某些模塊的“基本”gradle 文件,只需應用它們並添加它可能需要的任何新依賴項。 我正在將所有 gradle 文件轉換為新的 Kotlin DSL,但使用以下“基本”文件時出現關鍵字的“未解析引用”錯誤。
plugins {
id("com.android.library")
kotlin("kotlin.android")
kotlin("kapt")
}
android {
compileSdkVersion(App.compileSdk)
defaultConfig {
minSdkVersion(App.minSdk)
targetSdkVersion(App.targetSdk)
versionCode = App.versionCode
versionName = App.versionName
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
val implementation by configurations
val testImplementation by configurations
val androidTestImplementation by configurations
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(Libs.kotlin_stdlib_jdk8)
implementation(Libs.appcompat_v7)
testImplementation(Libs.junit)
androidTestImplementation(Libs.com_android_support_test_runner)
androidTestImplementation(Libs.espresso_core)
}
上面的文件在我的根項目中,我只是在功能模塊中使用以下內容
apply(rootProject.file("base-android.gradle.kts"))
這在 Groovy 中運行良好,但在轉移到 Kotlin 時完全中斷,關於我做錯了什么或如何在 Kotlin DSL 中正確擁有“基本”gradle 文件的任何想法?
編輯:添加完整的錯誤消息
Script compilation errors:
Line 10: android {
^ Unresolved reference: android
Line 11: compileSdkVersion(28)
^ Unresolved reference: compileSdkVersion
Line 12: defaultConfig {
^ Unresolved reference: defaultConfig
Line 13: minSdkVersion(21)
^ Unresolved reference: minSdkVersion
Line 14: targetSdkVersion(28)
^ Unresolved reference: targetSdkVersion
Line 15: versionCode = 1
^ Unresolved reference: versionCode
Line 16: versionName = "1.0"
^ Unresolved reference: versionName
Line 17: testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
^ Unresolved reference: testInstrumentationRunner
Line 20: buildTypes {
^ Unresolved reference: buildTypes
Line 21: getByName("release") {
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline fun <reified T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String): TypeVariable(T) defined in org.gradle.kotlin.dsl
public inline fun <reified T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String, configure: TypeVariable(T).() -> Unit): TypeVariable(T) defined in org.gradle.kotlin.dsl
public fun <T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String, type: KClass<TypeVariable(T)>): TypeVariable(T) defined in org.gradle.kotlin.dsl
public fun <T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String, type: KClass<TypeVariable(T)>, configure: TypeVariable(T).() -> Unit): TypeVariable(T) defined in org.gradle.kotlin.dsl
public inline fun <reified T : Any> ExtensionContainer.getByName(name: String): TypeVariable(T) defined in org.gradle.kotlin.dsl
Line 22: isMinifyEnabled = false
^ Unresolved reference: isMinifyEnabled
Line 23: proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
^ Unresolved reference: proguardFiles
Line 23: proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
^ Unresolved reference: getDefaultProguardFile
Line 27: compileOptions {
^ Unresolved reference: compileOptions
Line 28: sourceCompatibility = JavaVersion.VERSION_1_8
^ Unresolved reference: sourceCompatibility
Line 29: targetCompatibility = JavaVersion.VERSION_1_8
^ Unresolved reference: targetCompatibility
16 errors
問題可能來自嘗試應用plugins
。
plugins {
id("com.android.library")
kotlin("kotlin.android")
kotlin("kapt")
}
apply(rootProject.file("base-android.gradle.kts"))
...
這是我的方法:
build.gradle.kts
apply(from = "${rootProject.projectDir}/common-setup.gradle.kts")
common-setup.gradle.kts
apply {
fun android(configure: com.android.build.gradle.internal.dsl.BaseAppModuleExtension.() -> Unit): Unit =
(project as org.gradle.api.plugins.ExtensionAware).extensions.configure("android", configure)
android {
//common setup here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.