only id(String), alias(Provider), or alias( Provider Convertible) method calls allowed in plugins {} script block

huangapple go评论75阅读模式
英文:

only id(String), alias(Provider), or alias( Provider Convertible) method calls allowed in plugins {} script block

问题

我在复制/粘贴插件 {} 块中的一些代码行之后遇到了这个错误,这些代码行添加了一些 Kotlin 特性,就像这样

    id: 'kotlin-android'

然后我遇到了标题错误

我检查了代码,但它看起来没问题。

英文:

I got this error after copy/paste some lines in the plugins {} block, those lines add some kotlin features, like this

    id: 'kotlin-android'

then I got the title error

I checked the code but it seems ok

答案1

得分: 1

作为入门,Gradle 构建脚本通常使用 Groovy(作为 build.gradle)或 Kotlin(作为 build.gradle.kts)来定义。由于您正在使用单引号(Kotlin 不允许非字符单引号),我将假设您正在使用 Groovy。


您遇到的问题的原因是因为在 Groovy 中,x: y 语法 通常会转换为在 Java 中创建一个 Map

abc: "def"

(应该与以下 Kotlin 代码相同)

mapOf("abc" to "def")

plugins 块通常期望插件被定义为对 idalias 方法 的调用:

plugins {
  id("java") // 包括内置的 Java 插件
  alias(libs.plugins.example) // 包括从版本目录中定义的插件
  id("com.example.plugin") version "1.2.3" // 包括第三方 "com.example.plugin" 插件,版本为 1.2.3
}

请注意,对于方法调用,括号在 Groovy 中被视为可选的,因此您可能会在文档中看到它们被省略,或者正如您在您的回答中引用的那样:

plugins {
  id 'kotlin-android' // 注意括号的缺失,而且 Groovy 也允许使用单引号
}

有关更多信息,请参阅 plugins DSL 文档

英文:

As a primer, Gradle build scripts are generally either defined in Groovy (as build.gradle), or Kotlin (as build.gradle.kts). As you're using single-quotes (which Kotlin does not allow for non-characters), I'll presume that you're using Groovy.


The reason why you're encountering the issue you're facing is because x: y syntax in Groovy generally translates to creating a Map in Java:

abc: "def"

(should be the same as)

mapOf("abc" to "def")

While the plugins block generally expects plugins to be defined as calls to the id or alias methods:

plugins {
  id("java") // Include the built-in Java plugin
  alias(libs.plugins.example) // Include a plugin defined from a version catalog
  id("com.example.plugin") version "1.2.3" // Include a 3rd-party "com.example.plugin" plugin at version 1.2.3
}

Note that parentheses are considered as optional in Groovy for method calls, hence why you may see them omitted in documentation, or as you referenced in your answer:

plugins {
  id 'kotlin-android' // Note the missing parentheses, and Groovy allows for single-quotes to be used too
}

For more info, see the plugins DSL documentation.

答案2

得分: 0

我解决了从这些行中去掉冒号,就像这样:

id 'kotlin-android'
英文:

I solved removing the colon from the lines, like this:

    id 'kotlin-android'

huangapple
  • 本文由 发表于 2023年2月19日 15:31:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定