Tamim Attafi
Tamim Attafi

Reputation: 2521

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.

EDIT 13/04/2021

I have managed to see the tasks that are added by my plugin in my app tasks list by including this module as a composite build.

My settings.gradle now looks like this:

pluginManagement {
    includeBuild("generator")
}

include(":SampleProject")

build.gradle of my plugin looks like this:

apply plugin: 'java-gradle-plugin' // Allows us to create and configure custom plugins
apply plugin: 'kotlin' //Needed as we'll write our plugin in Kotlin

buildscript {
    ext {
        kotlin_version = '1.4.31'
        gradle_version = '4.1.2'
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.android.tools.build:gradle:$gradle_version"
    }
}

repositories {
    google()
    jcenter()
}

dependencies {
    // Android gradle plugin will allow us to access Android specific features
}

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

And my generator class now looks like this:

class Generator : Plugin<Project> {

    override fun apply(project: Project) {

        project.tasks.register("generationTask") { task ->
            task.apply {
                group = "generation"
                actions.add(Action {
                    print("Hello from generation task!")
                })
            }
        }
    }

}

I can see generationTask in my tasks list and I can execute it normally. It prints the text without any problems.

The problem now is to include com.android.tools.build:gradle:4.1.2 in my dependecies to use it to access build types and flavors and their paths to save my generated code there. When I add it to my dependencies block, gradle fails with this error: Could not find com.android.tools.build:gradle:4.1.2.

How can I solve this problem?

Upvotes: 1

Views: 1941

Answers (1)

esentsov
esentsov

Reputation: 6522

Gradle build goes through specific set of phases, and the Configuration phase comes before the Execution phase. So you cannot use a plugin, which is built in the same build process, because by the time gradle tries to use it on Configuration phase, the plugin has not been built yet.

buildSrc directory is a special one, it's built not as part of the same build process, but in a separate build, before the main build process starts. This feature is called included or composite build. buildSrc is just a pre-defined way to set up a composite build and you can define your own included builds. So to make your plugin visible to the main build, you need to put it into a separate build and include this build into a composite build as described in the doc above. Here is an article describing how to transform a plugin defined in buildSrc into a composite build.

Upvotes: 1

Related Questions