【问题标题】:Gradle commands/configs to process annotations用于处理注释的 Gradle 命令/配置
【发布时间】:2015-12-13 09:43:26
【问题描述】:

我有一个 Java 项目,它使用注释处理器生成源代码,随后必须将其添加到编译类路径并编译/打包。

这个项目是由 Gradle 构建的,所以我想知道如何在后台调用这些注释处理器,以便:

  1. 首先处理注解,在src/main/java下生成需要的源码;然后
  2. 当 Gradle 进入编译阶段时,该源代码已经存在并且可以像所有其他源一样进行编译

当我运行gradle clean build 时,不会生成代码,并且其他源(取决于生成的类)无法编译。

我正在尝试了解需要做什么,以便 Gradle 运行必要的注释处理器并生成源代码。手头的特定处理器是 Immutables 项目所需的那些(也就是说,我的应用程序使用 Immutables 来生成不可变对象),但是我认为这个问题的答案是通用的并且与项目无关。


更新

类似这样的东西(?):

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"
}

【问题讨论】:

    标签: java gradle code-generation annotation-processing


    【解决方案1】:

    这个应该可以帮助你: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
        }
    }
    

    【讨论】:

    • 感谢@Vikto Yakunin (+1) 但是compile 'org.immutables:value:2.0.16' 已经是我列出的编译依赖项之一,但这仍然不起作用。想法?
    • @smeeb 查看第二次编辑,也不要忘记添加 android-apt 插件的所有代码
    • 再次感谢@Viktor Yakunin (+1) - 您能否查看我的更新并确认我的dependencies 关闭看起来应该如此?另外,您能否简要解释一下为什么会这样?再次感谢!
    • @smeeb 添加了完整的 build.gradle 列表
    猜你喜欢
    • 2012-09-26
    • 2018-04-27
    • 1970-01-01
    • 2021-12-21
    • 2017-10-26
    • 2011-04-08
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多