Friday 5 March 2021

Android Gradle: Custom Plugin with id 'XXXX' not found - Kotlin DSL

I'm trying to develop a gradle plugin to use it for generating some objects and methods for our api using some scheme. I have followed some tutorials but they all seem not to work, atleast for me. Some of these tutorials were:

  1. https://musings.animus.design/kotlin-poet-building-a-gradle-plugin/
  2. https://medium.com/@magicbluepenguin/how-to-create-your-first-custom-gradle-plugin-efc1333d4419

I have not used the buildSrc module because I'm already using it for Kotlin DSL, so I decided to create a new module and create my plugin there.

My plugin's module build.gradle.kts looks like this:

plugins {
    id("java-gradle-plugin")
    id("kotlin")
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath(config.ClassPaths.androidBuildTools)
        classpath(config.ClassPaths.kotlinGradlePlugin)
    }
}

repositories {
    google()
    jcenter()
}

dependencies {
    implementation(config.ClassPaths.androidBuildTools)
    implementation(config.ClassPaths.kotlinGradlePlugin)
}

gradlePlugin {
    plugins {
        create("Generator") {
            id = "Generator"
            implementationClass = "Generator"
        }
    }
}

My projects settings.gradle.kts looks like this:

include(":SampleProject", ":scheme-generator")

And in my application module's build.gradle.kts I'm applying this plugin like this:

apply(plugin = "Generator")

The build script stops here with an error: plugin 'Generator' not found

My Generator class looks like this:

class Generator : Plugin<Project> {
    override fun apply(target: Project) {
        target.android().variants().all { variant ->

            // Make a task for each combination of build type and product flavor
            val myTask = "myFirstTask${variant.name.capitalize()}"

            // Register a simple task as a lambda. We can later move this to its own
            // class to make our code cleaner and also add some niceties.
            target.tasks.create(myTask){task ->

                // Group all our plugin's tasks together
                task.group = "MyPluginTasks"
                task.doLast {
                    File("${target.projectDir.path}/myFirstGeneratedFile.txt").apply {
                        writeText("Hello Gradle!\nPrinted at: ${SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())}")
                    }
                }
            }
        }
    }
}

The android and variants methods are declared in a utils file, they look like this:

object GeneratorUtils {

    fun Project.android(): BaseExtension {
        val android = project.extensions.findByType(BaseExtension::class.java)
        if (android != null) {
            return android
        } else {
            throw GradleException("Project $name is not an Android project")
        }
    }

    fun BaseExtension.variants(): DomainObjectSet<out BaseVariant> {
        return when (this) {
            is AppExtension -> {
                applicationVariants
            }

            is LibraryExtension -> {
                libraryVariants
            }

            else -> throw GradleException("Unsupported BaseExtension type!")
        }
    }

}

I have tried many things, but I seem not to get this right.

EDIT: Using the buildSrc module for my plugin works totally fine, the plugin is applied and the gradle tasks are visible. However, buildSrc is reserved for other purposes, and we would like our plugin to be in a separate module, so we will be able to use it in other projects.



from Android Gradle: Custom Plugin with id 'XXXX' not found - Kotlin DSL

No comments:

Post a Comment