OiO.lk Blog Android Gradle plugin Kotlin DSL get android plugin variants
Android

Gradle plugin Kotlin DSL get android plugin variants


I’m trying to write a Gradle plugin and register tasks for release and debug variants. I’m following the official instructions here.

I don’t quite understand how to get the android config from project. This one doesn’t compile:

     val variants = if (project.extensions.findByName("android")?.hasProperty("applicationVariants") == true) {
        project.extensions.findByName("android")?.applicationVariants
     } else {
        project.extensions.findByName("android")?.libraryVariants
     }

    // Ensure variants is non-null before proceeding
    (variants as? Iterable<BaseVariant>)?.all { variant ->
        val variantUpper = variant.name.capitalize()

        project.tasks.register("buildCargoNdk$variantUpper", CargoNdkBuildTask::class.java) {
            group = "Build"
            description = "Build rust library for variant ${variant.name}"
            setVariant(variant.name)
            extension = ext
        }
    }

    project.tasks.whenTaskAdded { task ->
        (variants as? Iterable<BaseVariant>)?.forEach { variant ->
            val variantName = variant.name
            val variantUpper = variantName.capitalize()
            val preTasks = listOf("compile${variantUpper}Sources", "merge${variantUpper}JniLibFolders")

            if (task.name in preTasks) {
                task.dependsOn("buildCargoNdk$variantUpper")
            }
        }
    }

The idea is to rewrite this Groovy version to Kotlin DSL. Any pointer?



You need to sign in to view this answers

Exit mobile version