简体   繁体   English

循环所有 SourceSets - Groovy -> Kotlin DSL

[英]Loop all SourceSets - Groovy -> Kotlin DSL

How to loop all source-sets in the kotlin DSL?如何循环 kotlin DSL 中的所有源集? The below groovy code loops all sourceSets including 'multiplicative' like androidTestFoobarProdDebug以下 groovy 代码循环所有 sourceSets,包括像 androidTestFoobarProdDebug 这样的“乘法”

flavorDimensions("brand", "releaseType")
productFlavors {
    create("prod") {
        dimension = "releaseType"
    }
    create("foobar") {
        dimension = "brand"
    }
}
sourceSets.all { com.android.build.api.dsl.AndroidSourceSet sourceSet ->
     // Also prints foorbarProd
     println("0 "+sourceSet.name)
}
sourceSets.all {
    println("1 "+it.name)
}

but this kotlin-code does not loop the multiplicative concatenated flavor-dimensions like foobarProdDebug但是这个 kotlin 代码不会像 foobarProdDebug 这样循环乘法连接的风味维度

sourceSets.all { sourceSet ->
    println(sourceSet.name) // does not print foobarProd
    true // seems its a predicate in kotlin
}

People at Google were so kind to help me out here:谷歌的人非常友好地帮助我:

Sadly this seems like the mixing of kotlin's all extension method on collection (which is not lazy) and gradle's DomainObjectContainer.all (which is lazy).遗憾的是,这似乎是 kotlin 的 all 集合扩展方法(不是惰性的)和 gradle 的 DomainObjectContainer.all(是惰性的)的混合。

If you change如果你改变

sourceSets.all { sourceSet -> //: com.android.build.api.dsl.AndroidSourceSet ->
    println("sourceSet.name = \"src/${name}/svg\"")
    true // seems its a predicate in kotlin
}

to

sourceSets.all {// this: com.android.build.api.dsl.AndroidSourceSet ->
    println("sourceSet.name = \"src/${name}/svg\"")
    // Not a kotlin.sequences.all call anymore so nothing to return
}

it works.有用。

Here's the full kotlin solution:这是完整的 kotlin 解决方案:

flavorDimensions("brand", "releaseType")
productFlavors {
    create("prod") {
        dimension = "releaseType"
    }
    create("foobar") {
        dimension = "brand"
    }
}
sourceSets.all {
    println(name)
}

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

相关问题 SourceSets-Groovy-> Kotlin DSL - SourceSets - Groovy -> Kotlin DSL Kotlin DSL 添加 Kotlin SourceSets 不会影响 - Kotlin DSL adding Kotlin SourceSets doesn't affect 将 kotlin DSL 转换回 groovy? - Converting kotlin DSL back to groovy? 如何从 kotlin dsl 正确调用 groovy gradle 方法 - How to properly call groovy gradle method from kotlin dsl 如何将保护级别 build.gradle 的文件从 Gradle Groovy 迁移到 Gradle Kotlin DSL - How to migrate protect level build.gradle file from Gradle Groovy to Gradle Kotlin DSL 如何在 Android Studio Bumblebee 中将 build.gradle 从 Groovy 迁移到 Kotlin DSL? - How to Migrate build.gradle from Groovy to Kotlin DSL in Android Studio Bumblebee? 如何创建Kotlin DSL - DSL语法Kotlin - How to create Kotlin DSL - DSL syntax Kotlin 'sourceSets'无法应用于Android Studio中的'groovy.lang.closure'警告 - 'sourceSets' cannot be applied to 'groovy.lang.closure' warning in Android Studio groovy.lang.MissingPropertyException: Could not set unknown property 'useIR' for object of type org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl - groovy.lang.MissingPropertyException: Could not set unknown property 'useIR' for object of type org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl Kotlin DSL 添加新的 sourceSet - Kotlin DSL add new sourceSet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM