androidgradleandroid-gradle-plugin

Unable to load class Plugin


I'm trying to generate first plugin but have this error:
Could not find implementation class 'CommonPluginClass' for plugin 'common-plugin' specified in jar:file
org.gradle.api.plugins.InvalidPluginException: An exception occurred applying plugin request [id: 'common-plugin']

enter image description here
this is my CommonPluginClass:

class CommonPluginClass: Plugin<Project> {

    override fun apply(project: Project) {
        project.task("hello") {
            doLast {
                println ("Hello from the CommonPluginClass")
            }
        }
    }
}

its very simple. My build.gradle (convention)

plugins {
    id 'groovy-gradle-plugin'
}

gradlePlugin {
    plugins {
        commonPlugin {
            id = "common-plugin"
            implementationClass = "CommonPluginClass"
        }
    }
}

and into settings.gradle (build-logic)

dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "build-logic"
include(":convention")

Into build.gradle (app) i call plugin in this mode:
plugins { id 'common-plugin' }
my exeption:


Solution

  • In short

    You should basically get this to work by changing the plugins block of your build-logic/convention/build.gradle file to something like the following:

    plugins {
        id 'java-gradle-plugin'
        id 'org.jetbrains.kotlin.jvm' version '1.6.21'
    }
    

    In more detail

    I have replicated the relevant bits of your setup as follows (not showing Gradle Wrapper files):

    ├── app
    │   └── build.gradle
    ├── build-logic
    │   ├── convention
    │   │   ├── build.gradle
    │   │   └── src
    │   │       └── main
    │   │           └── kotlin
    │   │               └── CommonPluginClass.kt
    │   └── settings.gradle
    └── settings.gradle
    

    The files have the same content as given in your question, except that I’ve changed the files below.

    With that, I could successfully run: ./gradlew :app:hello

    build-logic/convention/build.gradle

    plugins {
        id 'java-gradle-plugin'
        id 'org.jetbrains.kotlin.jvm' version '1.6.21'
    }
    
    gradlePlugin {
        plugins {
            commonPlugin {
                id = "common-plugin"
                implementationClass = "CommonPluginClass"
            }
        }
    }
    

    build-logic/convention/src/main/kotlin/CommonPluginClass.kt

    import org.gradle.api.Project
    import org.gradle.api.Plugin
    
    class CommonPluginClass: Plugin<Project> {
    
        override fun apply(project: Project) {
            project.task("hello") { t ->
                t.doLast {
                    println ("Hello from the CommonPluginClass")
                }
            }
        }
    }
    

    settings.gradle

    pluginManagement {
        includeBuild('build-logic')
    }
    
    include ':app'