我有一个 Java 项目,它使用注释处理器生成源代码,随后必须将源代码添加到编译类路径并编译/打包。
这个项目是由 Gradle 构建的,所以我想知道如何在后台调用这些注释处理器,以便:
- 首先处理注解,在
src/main/java
下生成所需的源代码;然后 - 当 Gradle 进入编译阶段时,该源代码已经存在并且可以像所有其他源代码一样进行编译
当我运行 gradle clean build
时,代码没有生成,其他来源(取决于生成的类)编译失败。
我试图了解需要做什么,以便 Gradle 运行必要的注释处理器并生成源代码。手头的特定处理器是 Immutables 所需的处理器。项目(也就是说,我的应用程序使用 Immutables 来生成不可变对象(immutable对象)),但是我认为这个问题的答案是通用的并且与项目无关。
更新
像这样(?):
dependencies {
// ...
apt 'com.squareup.dagger:dagger-compiler:1.1.0'
apt "org.immutables:value:2.0.21"
compile 'com.squareup.dagger:dagger:1.1.0'
provided "org.immutables:value:2.0.21:annotations"
provided "org.immutables:builder:2.0.21"
provided "org.immutables:gson:2.0.21:annotations"
}
最佳答案
这个应该可以帮到你:https://bitbucket.org/hvisser/android-apt . 来自以下链接的示例:
dependencies { apt 'com.squareup.dagger:dagger-compiler:1.1.0' compile 'com.squareup.dagger:dagger:1.1.0' }
编辑
您要使用的库似乎已经有手册http://immutables.github.io/getstarted.html
EDIT2
compile 指令使您的项目可以访问注释,但 apt 指令将启动 apt(注释处理工具)。因此完整的 build.gradle 将是:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
}
}
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.yourpackage"//FIXME
minSdkVersion 14
targetSdkVersion 23
versionCode 1//FIXME
versionName "test"//FIXME
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
apt "org.immutables:value:2.0.21" // for annotation processor
provided "org.immutables:value:2.0.21:annotations" // annotation-only artifact
provided "org.immutables:builder:2.0.21" // there are only annotations anyway
provided "org.immutables:gson:2.0.21:annotations" // annotation-only artifact
}
apt {
arguments {
resourcePackageName android.defaultConfig.applicationId
androidManifestFile variant.outputs[0].processResources.manifestFile
}
}
关于java - 用于处理注释的 Gradle 命令/配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32614784/