I have a Gradle build script that is using an annotations processor (Android Annotations) to generate code. Building was fine until I added a new Pro Flavor. I can build the Free flavor but when I build the Pro flavor the annotations processor isn't run. This causes missing code and the build fails.
Here is my script:
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()
}
}
When building the free variants, the annotations processor is run as indicated by the following in the gradle output:
Note: Starting AndroidAnnotations annotation processing
When building the Pro variants, the annotations processor doesn't run, so the references to the generated code fails.
The curious thing about this is, I found (truly by accident) that if I remove the packageName "com.MyCompany.MyApp.Pro"
from the script....the annotations processor runs and it will build correctly. I need to update the the package name for Google Play.
When looking in android studio, you can see that the apt (the Annotation Processing Tool) is showing the Pro version as being active even when I have the FreeDebug build variant selected. I am not sure this is indicative of a problem or if this is just a problem with the beta android studio (Android Studio version: 0.2.13). So take that with a grain of salt.
I'm new to the gradle build system but I thought I was getting the hang of it. I have looked at the script over and over again and I don't see why the annotations processor is not running for the pro variant. And aside from running the wrapper with the --info and --debug arguments, I don't know yet how to debug these problems.
I've run the gradle wrapper with the -info and -debug to get extended output but there is nothing there that indicates any other error (or missing item) until it reaches the error caused by the missing generated code. So this leads me to believe it's just the fact that the androidannotations are not being run with that variant that is the root problem. (i.e. I don't think this is an error caused by something upstream and being misreported later on. I could be wrong though)
I'm really at a loss and have been stuck with this for 2 days now.