androidkotlinandroid-studiogradlekotlin-multiplatform

Wrap a gradle task in a simpler one?


I have a gradle task which generates a json file containing all libraries used in my Kotlin Multiplatform Application. The output file will be parsed, and rendered in the UI.

./gradlew features:about:exportLibraryDefinitions - PaboutLibraries.exportPath=src/commonMain/composeResources/files -PaboutLibraries.exportVariant=release

Now, this task is quite long to type, and it requires the exact exportPath to be the same as the one that my file reader expects. Therefor, I would like to wrap it in another gradle (simpler) task:

tasks updateLicenseInformation = tasks.register("updateLicenseInformation"){
   group = "Entreco"
   description = "Update all license information json file"
   
   // How to execute 
   //"gradle features:about:exportLibraryDefinitions - PaboutLibraries.exportPath=src/commonMain/composeResources/files -PaboutLibraries.exportVariant=release" 
   // here?
} 

How can I execute my gradle command here? I am using Android Studio + Gradle with Kotlin


Solution

  • I expect the below to work.

    However it is not guaranteed because there appears to be a bug setting project properties in Kotlin build scripts and accessing them in other build scripts which I encountered in testing. I have logged this as a Gradle issue with a reproducible project so we can see what the Gradle team say.

    tasks.register("updateLicenseInformation"){
       group = "Entreco"
       description = "Update all license information json file"
       extra["aboutLibraries.exportPath"] = "src/commonMain/composeResources/files"
       extra["aboutLibraries.exportVariant"] = "release"
       /**
        * The following, setting properties on the "ext" object, should not be necessary
        * per the documentation, but in testing the properties were not
        * accessible by Groovy scripts without these
        * See https://docs.gradle.org/current/userguide/migrating_from_groovy_to_kotlin_dsl.html
        */
       ext["aboutLibraries.exportPath"] = "src/commonMain/composeResources/files"
       ext["aboutLibraries.exportVariant"] = "release"
       dependsOn("features:about:exportLibraryDefinitions")
    }