buildFeatures {
viewBinding true
}
I try to add *View binding in build.gradle.kts.*I can't add buildFeatures {viewBinding; true} to build.gradle.kts shows an error"Unexpected tokens (use ';' to separate expressions on the same line)"
我尝试在Build.gradle.kts中添加*View Binding。*我无法将BuildFeature{viewBinding;true}添加到Build.gradle.kts中。kts显示一条错误消息“意外的令牌(使用‘;’分隔同一行上的表达式)”
更多回答
You can use buildFeatures { viewBinding = true }
in build.gradle.kts
.
您可以在Build.gradle.kts中使用BuildFeature{viewBinding=true}。
thanks. but why is it shown differently in the android documentation?
谢谢。但为什么它在Android文档中的显示有所不同呢?
Gradle build scripts may be written in groovy
or kotlin
language. Your example was written in groovy
but placed into a kotlin script file. In kts
files, you should use kotlin. docs.gradle.org/current/userguide/…
Gradle构建脚本可以用Groovy或Kotlin语言编写。您的示例是用groovy编写的,但放置在一个Kotlin脚本文件中。在kts文件中,您应该使用kotlin。.org/CURRENT/USERGUIDE/…
优秀答案推荐
The buildFeatures
block in the build.gradle.ktx
file is used to enable or disable certain build features. In the code snippet you provided, viewBinding true
is used to enable view binding for your project but in build.gradle
aka Groovy
Build.gradle.ktx文件中的BuildFeature块用于启用或禁用某些构建功能。在您提供的代码片段中,viewBinding True用于为您的项目启用视图绑定,但在build.gradle中也称为Groovy
So you must add =
sign when you are using build.gradle.ktx
like this:
因此,在使用Build.gradle.ktx时必须添加=sign,如下所示:
buildFeatures {
viewBinding = true
}
Regarding the difference between the syntax of build.gradle and build.gradle.ktx, the latter is a Kotlin DSL (Domain Specific Language) file that provides a more concise and type-safe way of writing Gradle build scripts. The syntax of build.gradle.ktx is different from that of build.gradle because it is written in Kotlin, whereas build.gradle is written in Groovy.
考虑到build.gradle和Build.gradle.ktx之间的语法差异,后者是一种Kotlin DSL(域特定语言)文件,它提供了一种更简洁、类型安全的编写Gradle构建脚本的方法。Ktx的语法与build.gradle的不同,因为它是用科特林编写的,而build.gradle是用Groovy编写的。
Documentation : https://developer.android.com/topic/libraries/view-binding#kts
文档:https://developer.android.com/topic/libraries/view-binding#kts
更多回答
我是一名优秀的程序员,十分优秀!