在 build.gradle 文件中添加 buildFeatures 时出错

Sre*_*v R 27 user-interface android androidx android-jetpack-compose

我正在尝试 compose,这是 Andorid jetpack 中的一项新功能。下面是我的代码。我在 app 的 build.gradle 文件中添加 buildfeatures,而不是在根文件夹中。

android {
    compileSdkVersion compileSDKVer
    buildToolsVersion buildToolsVer
    defaultConfig {
        applicationId "com.sample.slothyhacker.jetpackcompose"
        minSdkVersion minSdkVer
        targetSdkVersion targetSdkVer
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        // Enables Jetpack Compose for this module
        //compose true
    }

    compileOptions {
        // Set both the Java and Kotlin compilers to target Java 8.
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我的项目给了我一个编译错误。如果有人能指出我做错了什么,我将不胜感激。

Could not find method buildFeatures() for arguments [build_7yf57wk394cperk1t82v120yf$_run_closure1$_closure5@78c292be] on object of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
Run Code Online (Sandbox Code Playgroud)

Mor*_*zko 35

当我尝试将 Jetpack 添加到我现有的应用程序时,我发现了这个错误。我遵循了Suraj 的回答,甚至使用了最新的Kotlin gradle 插件,但无法完全弄清楚出了什么问题。我也遵循了官方设置指南,但没有奏效。一切似乎都很好,但没有任何帮助。

安装 Android Studio 4.0 canary 也无济于事。

事实证明,仅仅包含某些依赖项是不够的——您需要特定版本或更高版本。我使用的是较旧的Android Gradle 插件:3.5.3. 升级以4.0.0-alpha07修复错误:

classpath 'com.android.tools.build:gradle:4.0.0-alpha07'
Run Code Online (Sandbox Code Playgroud)

如果您要将 Jetpack 添加到现有应用程序,请务必检查您的依赖项


Kre*_*ese 21

看来这

buildFeatures {
    viewBinding true
}
Run Code Online (Sandbox Code Playgroud)

被替换为

viewBinding {
    enabled true
}
Run Code Online (Sandbox Code Playgroud)

参考

  • 你是对的,不知道为什么你被否决了。谷歌文档也是错误的。https://developer.android.com/topic/libraries/view-binding (2认同)
  • 为我工作谢谢 (2认同)

Sur*_*dur 7

要将 jetpack compose 添加到您的项目中,您需要按照以下步骤操作:

注意:您应该使用 4.1 Canary 版本的 Android Studio

第 1 步:在build.gradle文件内部

android {
    defaultConfig {
        ...
        minSdkVersion 21
    }

    buildFeatures {
        // Enables Jetpack Compose for this module
        compose true
    }
    ...

    // Set both the Java and Kotlin compilers to target Java 8.

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:Jetpack Compose 目前需要 Kotlin-Gradle 插件的实验版本。要在您的应用程序中包含此插件,请在项目的 build.gradle 文件中包含以下内容

buildscript {
    repositories {
        google()
        jcenter()
        // To download the required version of the Kotlin-Gradle plugin,
        // add the following repository.
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0-alpha01'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.60-eap-25'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    }
}
Run Code Online (Sandbox Code Playgroud)

第 2 步:在您的build.gradle文件中添加 Jetpack Compose 工具包依赖项

dependencies {
    // You also need to include the following Compose toolkit dependencies.
    implementation 'androidx.ui:ui-tooling:0.1.0-dev02'
    implementation 'androidx.ui:ui-layout:0.1.0-dev02'
    implementation 'androidx.ui:ui-material:0.1.0-dev02'
    ...
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*sel 6

因为build.gradle.kts无法添加它

android {
    buildFeatures {
        dataBinding = true
        viewBinding = true
    }
}
Run Code Online (Sandbox Code Playgroud)

什么工作是这样的:

android {
    buildFeatures.dataBinding = true
    buildFeatures.viewBinding = true
}
Run Code Online (Sandbox Code Playgroud)


小智 6

这取决于您使用的 Android Studio(和 gradle)版本。上述语法用于较新版本的 gradle(Android Studio 4.0 canary 及更高版本)

如果您使用的是 3.5.3,请使用以下命令:

dataBinding {
   enabled = true
 }
Run Code Online (Sandbox Code Playgroud)

来源: https: //github.com/stripe/stripe-terminal-android/issues/90


Ran*_*han 5

您只能在 Android Studio 4.0+ 中添加此功能,该版本仅适用于 Canary 版本

  • 我认为这不是一个准确的答案。当我的版本为 4.0.1 时,会重复出现此错误 (9认同)