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

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

问题描述

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

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

我去过能够使用 javac -Processor MyProcessor mySource.java

成功测试这个问题将问题集成到一个简单的Hello中使用Android Studio的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注释处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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