android - 自定义 android gradle 插件无法加载类

标签 android android-studio gradle gradle-plugin

我想构建一个自定义 gradle 插件,但是当我让根项目应用插件时它总是失败。

ERROR: Unable to load class 'com.ucloud.android.plugin.UpluginImpl'.
Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)

The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)

Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.


我的插件类是:
package com.ucloud.android.uplugin

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

class UpluginImpl implements Plugin<Project> {

    @Override
    void apply(Project project) {
        project.task("Test Ucloud Task") << {
            println("********************* Task *********************")
        }
    }
}

我的插件模块的 build.gradle 文件是:
apply plugin: 'groovy'
apply plugin: 'maven'

repositories {
    google()
    jcenter()
    mavenLocal()
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation gradleApi()
    implementation localGroovy()
    implementation 'com.android.tools.build:gradle:3.5.0'
}

sourceCompatibility = "8"
targetCompatibility = "8"

group = 'com.ucloud.android.plugin'
version = '1.0.2'
archivesBaseName='ucloud-plugin'

uploadArchives { //当前项目可以发布到本地文件夹中
    repositories {
        mavenDeployer {
            repository(url: uri('/Users/joshua/.m2/repository/')) //定义本地maven仓库的地址
        }
    }
}


我的模块/资源/META-INF/gradle-plugins/***.properties
implementation-class = com.ucloud.android.plugin.UpluginImpl

我什至尝试完全卸载 Android Studio,删除 Studio 的所有缓存目录。并删除我的 .gradle 目录。错误仍然在这里...

最佳答案

您缺少应用插件.. 所以 gradle 会知道在哪里可以找到插件:

apply plugin: UpluginImpl

关于android - 自定义 android gradle 插件无法加载类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58538998/

相关文章:

java - Android - MultiDex 安装

android - 旋转后找不到 fragment 的 id 0x View ?

android - 无法将Android Studio项目推送到Github

android - 无法在 Windows 10 上运行 Android Studio

java - 如何在toast消息中隐藏包名称?

java - Gradle war 不包含 bpmn 图文件夹

android - FFmpegFrameGrabber.start() 在加载 mp4 文件时崩溃

android - Textview 不会随着 android 3d 旋转木马中选择的改变而改变

android - Gradle Android库无法找到主项目的资源

java - 内置 gradle 插件的版本号是多少?