如何在KotlinDSL中为多模块项目创建“基本”gradle文件?

yh2wf1be  于 4个月前  发布在  Kotlin
关注(0)|答案(3)|浏览(67)

为了重用gradle文件中的代码,我通常会为某些模块创建一个“基本”gradle文件,然后应用它们并添加它可能需要的任何新依赖项。我正在将所有gradle文件转换为新的KotlinDSL,但我在使用以下“基本”文件时遇到关键字“未解析引用”错误。

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时完全中断了,关于我做错了什么或者如何在KotlinDSL中正确地拥有一个“基本”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

ttisahbt

ttisahbt1#

在一个大型构建中,我们在多个项目之间共享大量构建逻辑。共享的构建逻辑包含Java项目的配置、测试设置(例如“ci tests”的扩展)和存储库配置。
我们从应用一个基本配置文件开始,就像你的例子一样,但是转移到了预编译的脚本插件。使用预编译的脚本插件,你可以将你的共享构建逻辑移动到buildScr文件夹中的小gradle插件。在每个项目中,你可以只应用你的插件。在我们的项目中,这避免了在build.gradle.kts文件中应用其他文件时的问题。
文档中提供了一些例子,还有更多的gradle init为共享的构建逻辑生成“约定插件”。

wgeznvg7

wgeznvg72#

问题可能来自尝试应用plugins

plugins {
    id("com.android.library")
    kotlin("kotlin.android")
    kotlin("kapt")
}

apply(rootProject.file("base-android.gradle.kts"))

...

字符串

disbfnqx

disbfnqx3#

我是这么做的:
1x个月

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
    }
}

相关问题