在Gradle中,在Android上,Storm注释处理器可以像AndroidAnnotations处理器一样运行吗?

时间:2013-10-30 02:39:11

标签: java android apt android-gradle build.gradle

我已经能够使用gradle(以及Dagger)成功运行AndroidAnnotations,但是当我尝试将storm-apt添加到生成器时,gradle会无声地失败,跳过所有注释处理。

这是一个示例build.gradle,它可以工作(即运行aa apt处理器,但不是暴风雨处理器)而不包含storm-apt jar,但是会失败(即无法同时运行aa apt处理器,以及风暴处理器):

import groovy.io.FileType

apply plugin: 'android'

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 18
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

repositories {
    mavenCentral()
}

ext.androidAnnotationsVersion = '2.7.1'
configurations {
    apt
}

dependencies {
    apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
    // commenting out the following line will allow the aa apt processor to complete,
    // while leaving it uncommented will cause all annotation processing, 
    // both aa and storm to silently fail
    apt files('libs/storm-apt-0.97.jar')

    compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
    compile files('libs/storm-api-0.97.jar')
}

afterEvaluate { project ->
    android.applicationVariants.each { variant ->

        variant.javaCompile.doFirst {
            println "*** compile doFirst ${variant.name}"
            ext.aptOutput = file("${project.buildDir}/source/r/${variant.dirName}")
            aptOutput.mkdirs()

            aptOutput.eachFileRecurse FileType.FILES, {
                if (it.name.equals('R.java')) {
                    return
                }
                it.delete()
            }

            variant.javaCompile.options.compilerArgs += [
                    '-processorpath', configurations.apt.getAsPath(),
                    '-s', aptOutput
            ]

            println "*** ${variant.javaCompile.options.compilerArgs}"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定该代码中的失败,但现在有一个更好的解决方案叫做android-apt,这是一个gradle插件。