如何在 Gradle 中使用自定义 Java Annotation Processor? [英] How do I use custom Java Annotation Processor in Gradle?

查看:56
本文介绍了如何在 Gradle 中使用自定义 Java Annotation Processor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个简单的 Java 注释处理器,它扩展了 AbstractProcessor.

I've been working on a simple java annotation processor that extends AbstractProcessor.

我已经能够使用 javac -Processor MyProcessor mySource.java

问题是使用 Android Studio 将其集成到一个简单的 Hello World android 应用程序中.

The problem is integrating this into a simple Hello World android application using Android Studio.

我首先创建了一个新的 Android 项目,然后添加了一个单独的模块,我将所有注释处理器代码(MyProcessor 类以及它使用的自定义注释)添加到其中.

I started by making a new Android Project, and then adding a separate module where I place all my annotation processor code (the MyProcessor class as well as the custom annotation it uses).

然后我将这个新模块添加为 HelloWorld android 项目的依赖项.

I then added this new module as a dependency of the HelloWorld android project.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':MyProcessorModule')
}

然后我如何使用处理器根据 HelloWorld 应用程序中的所有源文件生成代码?

How do I then use the processor to generate code based on all the source files in the HelloWorld application?

推荐答案

有一个插件可以让它工作.它与 Maven 上的模块完美配合,不确定本地 .jar 文件,但您一定要试一试:

there's a plugin to make it work. It works perfectly with modules that are on Maven, not sure about local .jar files, but you sure give it a try:

这里是插件页面:https://bitbucket.org/hvisser/android-apt

这里是我的 build.gradle:

and here my build.gradle with it:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        ... add this classpath to your buildscript dependencies
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt' << apply the plugin after the android plugin

dependencies {
    // in dependencies you call it
    apt 'com.company.myAnnotation:plugin:1.0-SNAPSHOT'
    compile 'com.company.myAnnotation:api:1.0-SNAPSHOT'

它应该可以工作.

这篇关于如何在 Gradle 中使用自定义 Java Annotation Processor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆