Android 在build.gradle文件中添加buildFeatures时出错
我正在试用compose,这是Andorid jetpack的一项新功能。下面是我的代码。我正在应用程序的build.gradle文件中添加buildfeatures,而不是在根文件夹中Android 在build.gradle文件中添加buildFeatures时出错,android,user-interface,androidx,android-jetpack-compose,Android,User Interface,Androidx,Android Jetpack Compose,我正在试用compose,这是Andorid jetpack的一项新功能。下面是我的代码。我正在应用程序的build.gradle文件中添加buildfeatures,而不是在根文件夹中 android { compileSdkVersion compileSDKVer buildToolsVersion buildToolsVer defaultConfig { applicationId "com.sample.slothyhacker.jetpack
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"
}
}
但是我的项目给了我一个编译错误。如果有人能告诉我我做错了什么,我将不胜感激
Could not find method buildFeatures() for arguments [build_7yf57wk394cperk1t82v120yf$_run_closure1$_closure5@78c292be] on object of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
您只能在Android Studio 4.0+中添加此功能,Android Studio 4.0+仅提供Canary build要将jetpack compose添加到您的项目中,您需要遵循以下步骤: 注意:您应该使用安卓Studio的4.1金丝雀版本 步骤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"
}
}
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'
...
}
注: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' }
}
}
步骤2:在build.gradle
文件中添加Jetpack Compose toolkit依赖项
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"
}
}
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'
...
}
我在尝试将Jetpack添加到现有应用程序时发现了此错误。我遵循了答案,甚至使用了最新的Kotlin gradle插件,也无法准确地找出问题所在。我也跟着那个官员,但没用。一切似乎都很好,但毫无帮助 安装安卓Studio 4.0金丝雀也无济于事 事实证明,仅仅包含某些依赖项是不够的——您需要特定版本或更高版本。我使用的是一个较旧的AndroidGradle插件:
3.5.3
。升级到4.0.0-alpha07
修复了错误:
classpath 'com.android.tools.build:gradle:4.0.0-alpha07'
如果要将Jetpack添加到现有应用程序中,请确保检查您的依赖项,因为
build.gradle.kts
I无法添加它
android {
buildFeatures {
dataBinding = true
viewBinding = true
}
}
起作用的是:
android {
buildFeatures.dataBinding = true
buildFeatures.viewBinding = true
}
看来这个
buildFeatures {
viewBinding true
}
被替换为
viewBinding {
enabled true
}
尝试增加依赖项
yourproject/build.gradle
buildscript {
ext.kotlin_version = '1.4.10'
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
}
}
及
yourproject/gradle/wrapper/gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
添加此项的build.gradle文件一定是错误的。如果您看到,有两个文件,我们必须选择build.gradle(模块:),当您打开它时,它被命名为build.gradle(:app) 这取决于您使用的Android Studio(和gradle)的版本。以上语法用于较新版本的gradle(Android Studio 4.0 canary及以上版本) 如果您使用的是3.5.3,请使用以下内容:
dataBinding {
enabled = true
}
来源:我通过升级到Android Studio 4.1.2和Gradle到4.1.2解决了这个问题。这将使用Android 4.0及以上版本解决 如果仍然无法工作,请尝试设置数据绑定 替换
android {
buildFeatures {
dataBinding = true
}
}
由此
android {
buildFeatures.dataBinding = true
}
我的Android Studio是3.2.1版本,下面的代码对我有用。Gradle脚本->build.Gradle文件,在android部分,添加以下行:
dataBinding {
enabled = true
}
升级至最新版本,网址为您的Gradle版本是什么?最低api级别是什么?它应该是21或更高,你只能在Android Studio 4.0+中添加它,这是唯一可用的金丝雀buildYeah!这很有效。谢谢你,我想知道你在哪里找到的密码?我没有发现,我不认为这是一个确切的答案。当我有4.0.1版时,这个错误会重复。你知道吗?4.0.1版的金丝雀版本@Mr.LemonI正在使用4.1 RC3,但我仍然使用4.1.1版,但它仍然给我错误这个答案不正确。你是对的,不知道你为什么被否决。谷歌的文档也是错误的。谢谢你为我工作。甚至来自Marvin Effing的链接也说:“buildFeatures{viewBinding=true}”,这也适用于Android Studio 3.6.3。谢谢,我被告知永远不要升级Gradle,每次升级我都要重新安装所有东西!