一、报错信息
参考 【Android Gradle 插件】自定义 Gradle 插件模块 ④ ( META-INF 中声明自定义插件的核心类 | 在应用中依赖本地 Maven 仓库中的自定义 Gradle 插件 ) 系列博客 , 自定义 Gradle 插件 ;
import org.gradle.api.Plugin
import org.gradle.api.Project
class Plugin4 implements Plugin<Project> {
@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
二、解决方案
查看 " C:\Users\octop.m2\repository\kim\hsl\plugin\plugin " 目录下的插件信息 ;
打开 jar 包 , 发现 Plugin4 插件没有包名 , 直接在根目录存放 , 如果有包名的话 , 会存放在 " kim/hsl/plugin " 目录中 ;
上述原因是在 groovy 代码中没有写包名 , 在 groovy 代码文件中不会自动添加包名 , 需要手动添加 ;
修改完成后的代码 :
package kim.hsl.plugin
import org.gradle.api.Plugin
import org.gradle.api.Project
class Plugin4 implements Plugin<Project> {
@Override
void apply(Project project) {
println 'Plugin4'
}
}
再次执行 publishPluginPublicationToMavenLocal 任务 , 将插件上传到本地 Maven 仓库 ;
此时 , 发现自定义插件 Plugin4 被定义在了 kim\hsl\plugin\ 目录下 ;
再次编译应用 , 编译成功 ;