首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Kotlin DSL中为多模块项目创建一个“基础”gradle文件?

如何在Kotlin DSL中为多模块项目创建一个“基础”gradle文件?
EN

Stack Overflow用户
提问于 2018-12-04 10:16:34
回答 2查看 1.9K关注 0票数 10

为了重用gradle文件中的代码,我通常对某些模块有一个“基础”gradle文件,只需应用它们并添加它可能需要的任何新依赖项。我正在将我所有的gradle文件转换成新的Kotlin DSL,但我使用下面的"base“文件得到了关键字的"Unresolved reference”错误。

代码语言:javascript
运行
复制
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)
}

上面的文件在我的根项目中,我只是在功能模块中使用了以下内容

代码语言:javascript
运行
复制
apply(rootProject.file("base-android.gradle.kts"))

这在Groovy中工作得很好,但在迁移到Kotlin时完全崩溃了,有什么想法是我做错了什么,或者如何在Kotlin DSL中正确地拥有一个“基础”gradle文件?

编辑:添加完整的错误消息

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

发布于 2020-07-07 13:45:20

我是这样做的:

build.gradle.kts

代码语言:javascript
运行
复制
apply(from = "${rootProject.projectDir}/common-setup.gradle.kts")

common-setup.gradle.kts

代码语言:javascript
运行
复制
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
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-12-04 10:32:47

问题可能来自于尝试应用plugins

代码语言:javascript
运行
复制
plugins {
    id("com.android.library")
    kotlin("kotlin.android")
    kotlin("kapt")
}

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

...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53604782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档