【问题标题】:Convert groovy to kotlin dsl将 groovy 转换为 kotlin dsl
【发布时间】:2022-10-14 06:38:57
【问题描述】:

大家好,我使用 appDynamics 库,文档只解释了 groovy Gradle,我在将 groovy Gradle 脚本转换为 kotlin Gradle DSL 时遇到问题,我尝试了几种方法和几种语法,我什至还使用了从 groovy 到 Kotlin Gradle 的转换工具没有用 groovy Gradle 解决脚本后的问题

adeum {
    
    account {
        name 'xxx'
        licenseKey 'yyyy'
    }
    proguardMappingFileUpload {
        failBuildOnUploadFailure true //should build fail if upload fails? Defaults to false.
        enabled true //enables automatic uploads. Defaults to true.
    }
}

[错误][1] [1]:https://i.stack.imgur.com/tet7q.png

我还必须提到 groovy 运行良好

【问题讨论】:

  • 那是您真正的许可证密钥和帐户名吗?如果是这样,可能最好不要在互联网上发布
  • 并不是所有的 Gradle 插件都可以在 Kotlin 中工作……有些插件可能使用 Groovy 功能​​,因此几乎不可能从其他 JVM 语言中使用。除非你有复杂的 Gradle 文件,否则只要使用 Groovy DSL 就行了……花时间让 Kotlin 处理一个简单的 Gradle 文件可能不是最好的利用时间。
  • 你有一个简单的 groovy DSL 构建示例,我们可以查看它以找到 kotlin 等价物吗?
  • 不,它们只是随机键@tim_yates
  • 你是对的@Renato 我通过编写使用 Closure 接受这些插件的 Kotlin 代码来解决它

标签: android kotlin groovy gradle-kotlin-dsl appdynamics


【解决方案1】:

问题是在 kts 中的某些插件中,您必须使用闭包来确定插件包

 adeum {
           account(closureOf<com.appdynamics.android.gradle.ADPluginExtension.Account> {
            this.name ="xxx"
            this.licenseKey ="yyy"
    
        })
        proguardMappingFileUpload(closureOf<com.appdynamics.android.gradle.ADPluginExtension.ProguardConfig> {
            this.failBuildOnUploadFailure = true
            this.enabled = true
        })
    }

【讨论】:

    【解决方案2】:

    可以将adeum 更改为configure&lt;ADPluginExtension&gt;,如下所示:

    configure<ADPluginExtension> {
               account(closureOf<com.appdynamics.android.gradle.ADPluginExtension.Account> {
                this.name ="xxx"
                this.licenseKey ="yyy"
        
            })
            proguardMappingFileUpload(closureOf<com.appdynamics.android.gradle.ADPluginExtension.ProguardConfig> {
                this.failBuildOnUploadFailure = true
                this.enabled = true
            })
        }
    

    当我使用adeum 插件和kotlin-dsl 时,我遇到了一些问题,其中gradle sync 没有生成对androiddependenciesimplementation 等的扩展访问器,这些是由kotlin-dsl 插件提供的。

    【讨论】:

      猜你喜欢
      • 2018-07-08
      • 1970-01-01
      • 2019-04-06
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      相关资源
      最近更新 更多