【问题标题】:Unresolved reference: buildFeatures in Android multi module project未解决的参考:Android 多模块项目中的 buildFeatures
【发布时间】:2022-01-20 17:22:59
【问题描述】:

在一个多模块项目上工作,我有以下设置:-

  • android 工作室 = 4.1.3
  • AGP = "4.1.3"
  • kotlin gradle 插件 = "1.4.32"
  • 对我的所有 gradle 构建文件使用 gradle.kts

在我的(应用程序)模块 build.gradle.kts 中,我启用了 DataBinding 和 ViewBinding,一切都很好:-

android{

    /* compileSdkVersion, buildToolsVersion etc...*/

     buildFeatures {
        dataBinding = true
        viewBinding = true
    }

}

我在 buildSrc 目录中使用了一个常见的 Dependencies.kt 类,将依赖项注入到模块中,同时在我的 buildSrc 目录中使用 Plugins.kt 类来提供诸如“com.android.application”和“kapt”等插件。 .. 到所有模块。

在我的:modules:rxandroidbuild.gradle.kts 如下:

plugins {
    /* defined in Plugins.kt*/
    androidLibrary()
    kotlinAndroid()
    kotlinKapt()

}
android {

    compileSdkVersion(AndroidSDK.compileSdk)
    buildToolsVersion(AndroidSDK.buildTools)

    defaultConfig {
        minSdkVersion(DefaultConfig.minSdk)
        targetSdkVersion(DefaultConfig.targetSdk)

    }

    /* 
        compilation error kicks in here, if the below block is removed eveyrthing works fine 
        but of ocurs i can't used either ViewBinding or DataBinding

    */
     buildFeatures {
        dataBinding = true
        viewBinding = true
    }


    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    // For Kotlin projects
    kotlinOptions {
        jvmTarget = "1.8"
    }

}

dependencies {

    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    testImplementation(TestLibs.junit)

    /* defined in Dependencies.kt*/
    androidTestImplementation(TestLibs.runner)
    androidTestImplementation(TestLibs.espresso)

    implementation (KotlinLibs.kotlin_lib)
    implementation (KotlinLibs.kotlin_coroutines_core)
    implementation (KotlinLibs.kotlin_coroutine_android)
    implementation (KotlinLibs.kotlin_viewmodel_ktx)

    implementation (AndroidX.android_app_compat)
    implementation (AndroidX.android_constrain_layout)
    implementation (AndroidX.android_recyclerview)
    implementation (AndroidX.android_lifecycle_extensions)
    implementation (AndroidX.android_core_ktx)
    implementation (AndroidX.lifecycle_runtime_ktx)

    ...

}

在构建项目时,控制台日志中会出现以下错误消息:

Unresolved reference: buildFeatures

以及详细截图:-

【问题讨论】:

  • 我在使用 mlModelBinding 构建功能时遇到了同样的问题

标签: android kotlin gradle gradle-kts


【解决方案1】:

我不确定这是否与我之前遇到的相同问题,但我犯的一个错误是没有像下面这样添加它(没有等号)

android {


  ...

   buildFeatures {
        dataBinding true
    }

}

【讨论】:

  • 我在 Gradle 构建中使用 kotlin .kts(Kotlin 脚本),如果您的文件是用 Groovy 编写的 build.gradle,我的文件是用 Kotlin 构建的 build.gradle.kts,那么您提到的内容有效。这是迁移步骤的链接:developer.android.com/studio/build/migrate-to-kts
  • 啊哈,我的错,没有注意到语法的其余部分有所不同,抱歉,我还没有使用过 ktx,所以不确定它的好处,认为它可能会有所帮助,祝你好运那么
猜你喜欢
  • 1970-01-01
  • 2019-03-19
  • 2019-05-28
  • 2021-12-16
  • 2022-10-20
  • 2020-01-27
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
相关资源
最近更新 更多