与使用赋值“=”混淆和括号{} [英] Confused with the use of assignment "=" and brackets { }

查看:108
本文介绍了与使用赋值“=”混淆和括号{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对gradle很陌生(也对groovy很陌生),我几乎没有理解这个概念,也不知道它是否与Groovy或Gradle相关,以及谷歌有什么关键字,以获得一些帮助。



我经常碰到类似的东西:

  android {
[一些配置]
}

那么android是什么?一类?一个命名空间?一组属性?



但是更令人困惑的是:有时候我看到的配置看起来像:

/ p>

  minSdkVersion 19 

这似乎是一项任务,但没有等号。
有时还有一些带有相同符号的任务,比如

  source =folder / file.java

所以这很混乱。等号与不等号。所有这些支架的东西。



我看到的所有常规介绍都没有涵盖这些主题。
所以它是一些gradle约定还是真正的groovy语法?

解决方案

在Groovy中,括号有时是可选的。

第一个 android 传递一个闭包给Method。即:
$ b $ pre $ void android(Closure config){
println在Android中:$ {config()}


$ {b $ b'tim'
}

打印:在Android中:tim 与调用相同:

  android({
'tim'
})



minSdkVersion 的第二个例子是相同的,但它传递的是整数 $ b

  void minSdkVersion(Integer version){
println在MinSdkVersion中:$ version
}

minSdkVersion 19
//与minSdkVersion相同(19)

因此,打印在MinSdkVersion中:19



最后一个例子是将一个属性设置为一个String你会期望)


I am new to gradle (and so also to groovy) and I am hardly getting behind some concept and don't really know if it is groovy or gradle related and what keywords to google, to get some help.

I very often stumble over stuff like:

android {
    [some configuration]
}

So what is android? A class? A namespace? A collection of properties?

But what is more confusing:

Sometimes I see configurations that look like:

minSdkVersion 19

This seems to be an assignment, but without an equal sign. And sometimes there are assignments with equal signs like

source = "folder/file.java"

So this is very confusing. Equal sign vs. no equal sign. All these bracket stuff.

All the groovy introductions I saw don't cover exactly these topics. So is it some gradle convention or is it real groovy syntax?

解决方案

In Groovy, parentheses are sometimes optional.

The first android is passing a closure to a Method. ie:

void android( Closure config ) {
    println "In Android : ${config()}"
}

android {
    'tim'
}

prints : In Android : tim it is the same as calling:

android( {
    'tim'
} )

If you put the parens back.

The second example with minSdkVersion is the same, but it is passing an Integer to a method.

void minSdkVersion( Integer version ) {
    println "In MinSdkVersion : $version"
}

minSdkVersion 19
// same as minSdkVersion( 19 )

So that prints In MinSdkVersion : 19

The last example is setting a property to a String (as you'd expect)

这篇关于与使用赋值“=”混淆和括号{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆