我有一个使用注解处理器(Android Annotations)生成代码的Gradle构建脚本。在添加了一个新的Pro Flavor之后,构建过程出现问题。我可以成功构建Free flavor,但当我构建Pro flavor时,注解处理器不会运行。这导致缺失代码和构建失败。
以下是我的脚本:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
ext.androidAnnotationsVersion = '3.0-SNAPSHOT';
configurations {
apt
}
dependencies {
compile files('libs/android-support-v13.jar')
compile fileTree(dir: 'libs', include: '*.jar')
apt "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
compile "org.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
versionCode 29
versionName "2.0.3"
packageName "com.MyCompany.MyApp"
}
productFlavors {
free {
buildConfig "final public static boolean PRO_VERSION = false;"
}
pro {
packageName "com.MyCompany.MyApp.Pro"
versionName (versionName + ".Pro")
buildConfig "final public static boolean PRO_VERSION = true;"
}
}
buildTypes {
release {
buildConfig "final public static String BASE_URL = \"http://data.MyCompany.com/\";", \
"final public static String APP_NAME = \"com.MyCompany.MyApp\";"
}
debug {
buildConfig "final public static String BASE_URL = \"http://192.168.1.15/GDM/\";", \
"final public static String APP_NAME = \"com.MyCompany.MyApp\";"
}
}
}
def getSourceSetName(variant) {
return new File(variant.dirName).getName();
}
android.applicationVariants.all { variant ->
def aptOutputDir = project.file("build/source/apt")
def aptOutput = new File(aptOutputDir, variant.dirName)
println "****************************"
println "variant: ${variant.name}"
println "manifest: ${variant.processResources.manifestFile}"
println "aptOutput: ${aptOutput}"
println "****************************"
android.sourceSets[getSourceSetName(variant)].java.srcDirs+= aptOutput.getPath()
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-s', aptOutput
]
variant.javaCompile.source = variant.javaCompile.source.filter { p ->
return !p.getPath().startsWith(aptOutputDir.getPath())
}
variant.javaCompile.doFirst {
aptOutput.mkdirs()
}
}
构建免费版本时,如下所示在gradle输出中运行注解处理器:
Note: Starting AndroidAnnotations annotation processing
构建Pro版本时,注解处理器不会运行,因此对生成代码的引用将失败。
有趣的是,我(真正偶然地)发现,如果我从脚本中删除packageName "com.MyCompany.MyApp.Pro"
,则注释处理器将运行并且可以正确构建。但我需要更新软件包名称以适应Google Play。
在Android Studio中查看时,可以看到apt(注释处理工具)显示Pro版本正在运行,即使我选择了FreeDebug构建变体。我不确定这是否表示问题或者这只是测试版Android Studio(Android Studio版本:0.2.13)的问题。所以这要带着一定程度的怀疑看待。
我对Gradle构建系统还很陌生,但我认为我已经开始理解了。我一遍又一遍地查看了脚本,但我不知道为什么注解处理器不能运行pro变体。除了使用--info和--debug参数运行wrapper之外,我还不知道如何调试这些问题。
我运行了带-info和-debug标志的gradle wrapper,以获取扩展输出,但直到它达到由于缺少生成的代码而引起的错误之前,没有任何指示其他错误(或缺失项)。因此,这使我相信根本问题是该变体未运行androidannotations。(即,我认为这不是由上游错误引起并在以后被错误报告的错误。当然,我也可能是错的)
我真的很困惑,已经卡在这里两天了。