如何使用 Java 开发自定义 Gradle 插件

How to develop Custom Gradle Plugin with Java

我正在尝试使用 Java(而非 Groovy)开发自定义 Gradle 插件。

我正在使用这个 Groovy 插件作为指南

    @Override
    void apply(Project project) {
        def hasApp = project.plugins.withType(AppPlugin)
        def hasLib = project.plugins.withType(LibraryPlugin)
        if (!hasApp && !hasLib) {
            throw new IllegalStateException("'android' or 'android-library' plugin required.")
        }

        final def log = project.logger
        final def variants
        if (hasApp) {
            variants = project.android.applicationVariants
        } else {
            variants = project.android.libraryVariants
        }

        project.dependencies {
            debugCompile 'com.jakewharton.hugo:hugo-runtime:1.2.2-SNAPSHOT'
            // TODO this should come transitively
            debugCompile 'org.aspectj:aspectjrt:1.8.6'
            compile 'com.jakewharton.hugo:hugo-annotations:1.2.2-SNAPSHOT'
        }

        project.extensions.create('hugo', HugoExtension)

        variants.all { variant ->
            if (!variant.buildType.isDebuggable()) {
                log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
                return;
            } else if (!project.hugo.enabled) {
                log.debug("Hugo is not disabled.")
                return;
            }

            JavaCompile javaCompile = variant.javaCompile
            javaCompile.doLast {
                String[] args = [
                        "-showWeaveInfo",
                        "-1.5",
                        "-inpath", javaCompile.destinationDir.toString(),
                        "-aspectpath", javaCompile.classpath.asPath,
                        "-d", javaCompile.destinationDir.toString(),
                        "-classpath", javaCompile.classpath.asPath,
                        "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
                ]
                log.debug "ajc args: " + Arrays.toString(args)

                MessageHandler handler = new MessageHandler(true);
                new Main().run(args, handler);
                for (IMessage message : handler.getMessages(null, true)) {
                    switch (message.getKind()) {
                        case IMessage.ABORT:
                        case IMessage.ERROR:
                        case IMessage.FAIL:
                            log.error message.message, message.thrown
                            break;
                        case IMessage.WARNING:
                            log.warn message.message, message.thrown
                            break;
                        case IMessage.INFO:
                            log.info message.message, message.thrown
                            break;
                        case IMessage.DEBUG:
                            log.debug message.message, message.thrown
                            break;
                    }
                }
            }
        }
    }
}

我开发这个是为了检查 Android App / Android Library

 final PluginCollection hasAndroidAppPlugin = project.getPlugins().withType(AppPlugin.class);
        final PluginCollection hasAndroidLibraryPlugin = project.getPlugins().withType(LibraryPlugin.class);
        if (hasAndroidAppPlugin.isEmpty() & hasAndroidLibraryPlugin.isEmpty()) {
            throw new IllegalStateException("'android' or 'android-library' plugin required.");
        }

这用于设置我的依赖关系

final DependencyHandler dependencyHandler = project.getDependencies();
        dependencyHandler.add("debugCompile", "com.example.perspective:perspective-runtime:0.0.1");
        dependencyHandler.add("debugCompile", "org.aspectj:aspectjrt:1.8.10.3");

如何使用 Java 实现以下目标?

我无法识别 "Variants" 例如applicationVariants/libraryVariants.

if (hasApp) {
            variants = project.android.applicationVariants
        } else {
            variants = project.android.libraryVariants
        }

免责声明:我熟悉 Gradle,而不是 Android

我猜 android 属性 是一个扩展对象。参见 plugin docs and ExtensionContainer

如果您将此 println 放入 android build

println "${project.android.class.name}" 

您将看到扩展对象的类型。然后你可以做

AndroidModel aModel = getProject().getExtensions().getByType(AndroidModel.class)

我在这里使用 AndroidModel,您可以将其替换为上面 println 中打印的任何内容

你也可以

Object aModel = getProject().getExtensions().getByName("android");
System.out.println(aModel.getClass().getName());