【错误记录】自定义 Gradle 插件报错 ( Could not find implementation class x for plugin x specified in jar:file )

一、报错信息

import org.gradle.api.Plugin
import org.gradle.api.Project

class Plugin4 implements Plugin {

    @Override
    void apply(Project project) {
        println 'Plugin4'
    }
}

定义完成后 , 将插件上传到本地 Maven 仓库中 , 发布配置如下 :

// 指定自定义 Gradle 插件的分组
group 'kim.hsl.plugin'

// 指定自定义 Gradle 插件的版本号
version '0.1'

// 自定义 Gradle 插件的名称 , 默认为工程名
// 也可以在 publishing / publications 脚本块中 自己指定


// 用于将 插件上传到 远程仓库 或者 本地仓库 中
apply plugin: 'maven-publish'

// 发布到 远程/本地仓库 相关配置
publishing {
    publications {
        // plugin 函数是随意命名的函数
        plugin(MavenPublication) {
            // 配置上传内容
            // components.java 是打包的 jar 包
            from components.java

            // 指定自定义 Gradle 插件名称
            artifactId 'plugin'
        }
    }
}

之后在应用中 , 引入本地 Maven 仓库 , 并添加该自定义插件的依赖 ;

buildscript {
    repositories {
        mavenLocal()    // 依赖本地 Maven 仓库
    }
    dependencies {
        classpath "kim.hsl.plugin:plugin:0.1" // 依赖本地 Maven 仓库下的插件
    }
}

最终在执行 apply plugin: 'kim.hsl.plugin' 代码时 , 报错 ;

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* Where:
Build file 'D:\002_Project\002_Android_Learn\Android_UI\app\build.gradle' line: 15

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not find implementation class 'kim.hsl.plugin.Plugin4' for plugin 'kim.hsl.plugin' specified in jar:file:/C:/Users/octop/.gradle/caches/jars-8/e398a38a1f5565d019651add920bb7ec/plugin-0.1.jar!/META-INF/gradle-plugins/kim.hsl.plugin.properties.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
A problem occurred configuring project ':app'.
> com.android.builder.errors.EvalIssueException: compileSdkVersion is not specified. Please add it to build.gradle

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 567ms

你可能感兴趣的:(安卓gradle,jar,java)