【问题标题】:What's the difference between '' vs "" in build.gradle of Gradle? [duplicate]Gradle 的 build.gradle 中的 '' 与 "" 有什么区别? [复制]
【发布时间】:2017-09-15 10:28:54
【问题描述】:

比较:

compile 'org.apache.commons:commons-lang3:3.6'
compile "org.apache.commons:commons-lang3:3.6"

我曾经在我的 build.gradle 中使用'',因为它比""("") 更简单。

但是我发现额外的变量在使用'' 时不起作用,例如,

Gradle 无法解决:

compile 'org.apache.commons:commons-lang3:$versions.commonsLang',

compile "org.apache.commons:commons-lang3:$versions.commonsLang" 是 工作。

【问题讨论】:

    标签: gradle groovy build.gradle


    【解决方案1】:

    '(单引号)用于标准java String,而"(双引号)将被评估为GString,如果它有$表达式或普通String它没有' t.

    由于 gradle 脚本(在这种特殊情况下)是用 groovy 编写的,请查看 docs

    【讨论】:

      【解决方案2】:

      如您所见," 允许variable interpolation / expansion inside the string,而' 不允许。如果不需要变量插值,则使用 ' 被认为是一种很好的做法,例如出于性能原因,编译器不需要解析字符串中的变量名。

      【讨论】:

      • 编译器性能?!
      • 好吧,解析器,如果你愿意的话。但是,是的,我喜欢微优化:-D
      • 我认为即使是 "micro" 的尺寸也太大了......“纳米优化”下面是什么? ;-P
      猜你喜欢
      • 1970-01-01
      • 2015-07-21
      • 2018-04-29
      • 2015-03-29
      • 2014-06-09
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多