Search code examples
androidkotlingradle-kotlin-dsl

Subprojects var type in dsl


I am transitioning my code to Kotlin DSL and as i have multimodule app i have some hack to transition gralde tasks like clean, build etc to other modules. So i have following method in gradle:

private static def getSubProjectTasks(subprojects, taskName) {
    def tasks = []
    subprojects.each { Project project ->
        if (project.subprojects.size() == 0) {
            def relativeModulePath = project.path.replace(":feature:", "")
            tasks.add("$relativeModulePath:$taskName")
        }
    }
    return tasks
}

Now trying to transition into kotlin dsl i started with this way

fun getSubProjectTasks(subproject: Unit, taskName: String) {
    for(project in subproject) {
        if(project.subprojects.size() == 0) {

        }
    }
}

I am not really sure in Kotlin DSL what type subproject is? Is this just a collection or Unit or how to transition this method.


Solution

  • Have you tried Iterable<Project>, List<Project> or Array<Project>?

    I am judging based on your groovy lambda each.

    You can also change your for for a map and have a single line doing everything:

    fun getSubprojectTasks(subprojects: List<Subproject>, taskName: String) = subproject.mapNotNull { project ->
      if(project.subprojects.size == 0) {
        null
      } else {
        "${project.path.replace(":feature:", "")}$taskName"
      }
    
    }