Thorny84
Thorny84

Reputation: 335

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:

Upvotes: 6

Views: 4851

Answers (3)

Juan Sandoval
Juan Sandoval

Reputation: 1

I hope is not too late. It took make 1 whole day to figure out this.

This was my AndroidApplicationConventionPlugin.kt

package com.jsandoval

import com.android.build.api.dsl.ApplicationExtension
import com.jsandoval.convention.configureKotlinAndroid
import com.jsandoval.convention.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

class AndroidApplicationConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        target.run {
            pluginManager.run {
                apply("com.android.application")
                apply("org.jetbrains.kotlin.android")
            }
            extensions.configure<ApplicationExtension> {
                defaultConfig {
                    applicationId = libs.findVersion("projectApplicationId").get().toString()
                    targetSdk = libs.findVersion("projectTargetSdkVersion").get().toString().toInt()

                    versionCode = libs.findVersion("projectVersionCode").get().toString().toInt()
                    versionName = libs.findVersion("projectVersionName").get().toString()
                }
                configureKotlinAndroid(this)
            }
        }
    }
}

I was struggling and the error point out line : 1 on that class. What did i do? remove the package com.jsandoval.

now the class is like

import com.android.build.api.dsl.ApplicationExtension
import com.jsandoval.convention.configureKotlinAndroid
import com.jsandoval.convention.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

class AndroidApplicationConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        target.run {
            pluginManager.run {
                apply("com.android.application")
                apply("org.jetbrains.kotlin.android")
            }
            extensions.configure<ApplicationExtension> {
                defaultConfig {
                    applicationId = libs.findVersion("projectApplicationId").get().toString()
                    targetSdk = libs.findVersion("projectTargetSdkVersion").get().toString().toInt()

                    versionCode = libs.findVersion("projectVersionCode").get().toString().toInt()
                    versionName = libs.findVersion("projectVersionName").get().toString()
                }
                configureKotlinAndroid(this)
            }
        }
    }
}

And the error dissappear. Omg XD

Upvotes: 0

Abdelrahman Esam
Abdelrahman Esam

Reputation: 91

Maybe it's late

I have faced the same issue and I solved it by moving my plugins folders to the kotlin directory itself and delete the package references from all the plugin classes.

See the kotlin directory in the screenshot

Upvotes: 8

Chriki
Chriki

Reputation: 16338

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'

Upvotes: 4

Related Questions