简体   繁体   English

一些 build.gradle 文件的语法与其他文件不同?

[英]Some build.gradle files have different syntax's then others?

Started a new job and I can see that the build.gradle files's syntax is a bit different like the one using a = to assign values and the other just without it.开始了一项新工作,我可以看到 build.gradle 文件的语法与使用=分配值的语法有点不同,而另一个则没有它。 Fx.外汇。 the Google Sunflower demo app has it's implemetation statements without parentheses and my project uses parentheses and will not work otherwise. Google Sunflower 演示应用程序的implemetation语句不带括号,我的项目使用括号,否则将无法正常工作。

I'd like to have all add-on versions in one place as I'm used to, and as it's of-course in the Sunflower app:我希望将所有附加版本都放在一个地方,就像我习惯的那样,当然在 Sunflower 应用程序中也是如此:

ext {
    appCompatVersion = '1.1.0'
    ...
}

but that fails in my project但这在我的项目中失败了

在此处输入图像描述

What is going on?到底是怎么回事?

Gradle is a build automation tool, which can be written in several languages, more importantly when it comes to Android Studio, Gradle files tend to be either written in Gradle's own Groovy DSL language which is heavily based on Apache Groovy's language, which is usually referred to in documentations as just Groovy, or they can be written in Kotlin DSL, which is based on its parent namesake Kotlin language, Gradle's Kotlin DSL support was announced in 2016 which is why you might find legacy projects using Groovy, while others are using Kotlin DSL. Gradle is a build automation tool, which can be written in several languages, more importantly when it comes to Android Studio, Gradle files tend to be either written in Gradle's own Groovy DSL language which is heavily based on Apache Groovy's language, which is usually referred to in documentations as just Groovy, or they can be written in Kotlin DSL, which is based on its parent namesake Kotlin language, Gradle's Kotlin DSL support was announced in 2016 which is why you might find legacy projects using Groovy, while others are using Kotlin DSL。

You can tell the difference between Groovy Gradle files and Kotlin DSL Gradle files by looking at the file extension, a settings.gradle is a Groovy script and settings.gradle.kt is a Kotlin DSL script. You can tell the difference between Groovy Gradle files and Kotlin DSL Gradle files by looking at the file extension, a settings.gradle is a Groovy script and settings.gradle.kt is a Kotlin DSL script.

Note that Gradle supports interoperability between Groovy and Kotlin DSL files, meaning that, while a settings.gradle must be written in Groovy and a build.gradle.kt must be written in Kotlin DSL, it's OK to have a project that have both settings.gradle and build.gradle.kt , Gradle is smart enough to make them understand each other, although for the sake of consistency, I would recommend sticking with one DSL language for all your Gradle files. Note that Gradle supports interoperability between Groovy and Kotlin DSL files, meaning that, while a settings.gradle must be written in Groovy and a build.gradle.kt must be written in Kotlin DSL, it's OK to have a project that have both settings.gradle and build.gradle.kt , Gradle 足够聪明,可以让它们相互理解,但为了保持一致性,我建议对所有 Gradle 文件使用一种 DSL 语言。

By default when you create an Android project for the first time, the Gradle files generated for you are in Groovy.默认情况下,第一次创建Android项目时,为您生成的Gradle文件在Groovy中。

For much, much more thorough details on the differences between the two syntaxes I highly recommend reading the Gradle docs, this page in particular which goes into migrating from Groovy to Kotlin DSL , starting from the "Prepare your Groovy scripts" section. For much, much more thorough details on the differences between the two syntaxes I highly recommend reading the Gradle docs, this page in particular which goes into migrating from Groovy to Kotlin DSL , starting from the "Prepare your Groovy scripts" section. It's handy to have it on your side whenever you're copy/pasting a Gradle fix from Stackoverflow which is written in a Gradle syntax that doesn't correspond to your Gradle file extension, although eventually you'll probably end up memorizing the little differences yourself.每当您从 Stackoverflow 复制/粘贴 Gradle 修复程序时,将它放在您身边很方便,该修复程序是用与您的 Gradle 语法不对应的 Gradle 语法编写的,尽管您最终可能会记住文件扩展名的微小差异你自己。

Don't confuse build.gradle (Groovy) with build.gradle.kts (Kotlin).不要将build.gradle (Groovy) 与build.gradle.kts (Kotlin) 混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM