override fun apply()

in BuildSrc/customizeAgpDsl/buildSrc/src/main/kotlin/ExamplePlugin.kt [26:49]


    override fun apply(project: Project) {
        // attach the BuildTypeExtension to each elements returned by the
        // android buildTypes API.
        val android = project.extensions.getByType(ApplicationExtension::class.java)
        android.buildTypes.forEach {
            (it as ExtensionAware).extensions.add(
                "exampleDsl",
                BuildTypeExtension::class.java)
        }

        val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java)
        // hook up task configuration on the variant API.
        androidComponents.onVariants { variant ->
            // get the associated DSL BuildType element from the variant name
            val buildTypeDsl = android.buildTypes.getByName(variant.name)
            // find the extension on that DSL element.
            val buildTypeExtension = (buildTypeDsl as ExtensionAware).extensions.findByName("exampleDsl")
                as BuildTypeExtension
            // create and configure the Task using the extension DSL values.
            project.tasks.register(variant.name + "Example", ExampleTask::class.java) { task ->
                task.parameters.set(buildTypeExtension.invocationParameters ?: "")
            }
        }
    }