5.16.2014

According to androidannotations.org in order to add support of android annotations to your Android Studio project using Gradle you need to perform a few simple steps. These steps are described on this page - https://github.com/excilys/androidannotations/wiki/Building-Project-Gradle

But as I discovered this is not so simple as it is described. Once you perform all the steps from the above instruction, you will be available to use annotations in the source code. Moreover, gradle sync process will be successful. But as soon as you decide to run your project on some device or on emulator you will be surprised. That configuration of the gradle won't be enough. 

Probably you will face the error message like: "Could not find the AndroidManifest.xml file, going up from path ..."

In order to fix this error you need to apply the following 'dirty hack' in your build.gradle file:

applicationVariants.all { variant ->
        //  println "create folder for androidannotations:  ${aptOutput}"
        aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
        android.sourceSets[getSourceSetName(variant)].java.srcDirs += aptOutput.getPath()
        variant.javaCompile.doFirst {
            aptOutput.mkdirs()
            variant.javaCompile.classpath += configurations.androidannotations
            variant.javaCompile.options.compilerArgs += [
                    '-processor', 'org.androidannotations.AndroidAnnotationProcessor',
                    '-AandroidManifestFile=' + variant.processResources.manifestFile,
                    '-s', aptOutput
            ]
        }
        variant.javaCompile.source = variant.javaCompile.source.filter { p ->
            return !p.getPath().startsWith(aptOutput.getPath())
        }
    }

The complete build.gradle file looks like the following for me:

buildscript{
    repositories{
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.1'
    }
}

apply plugin: 'android'
apply plugin: 'android-apt'
ext.AAVersion = '3.0.1'
ext.daggerVersion = '1.0.0';

configurations {
    apt
    androidannotations
    androidannotations.extendsFrom(compile)
}

repositories{
    //There is the issue with local maven repositories
    //https://code.google.com/p/android/issues/detail?id=63908
    //bug in Gradle with local maven repo
    //as workaround, instead of the mavenLocal() the below line can be used
    maven {   url "/home/deezzel/.m2/repository" }
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

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

    applicationVariants.all { variant ->
        //  println "create folder for androidannotations:  ${aptOutput}"
        aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
        android.sourceSets[getSourceSetName(variant)].java.srcDirs += aptOutput.getPath()
        variant.javaCompile.doFirst {
            aptOutput.mkdirs()
            variant.javaCompile.classpath += configurations.androidannotations
            variant.javaCompile.options.compilerArgs += [
                    '-processor', 'org.androidannotations.AndroidAnnotationProcessor',
                    '-AandroidManifestFile=' + variant.processResources.manifestFile,
                    '-s', aptOutput
            ]
        }
        variant.javaCompile.source = variant.javaCompile.source.filter { p ->
            return !p.getPath().startsWith(aptOutput.getPath())
        }
    }
}

def getSourceSetName(variant) {
    return new File(variant.dirName).getName();
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
//    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.t4soft:ExternalWidget:0.1'

    apt "org.androidannotations:androidannotations:${AAVersion}"
    compile "org.androidannotations:androidannotations-api:${AAVersion}"
    apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"
    compile "com.squareup.dagger:dagger:${daggerVersion}"

    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:19.0.+'
}

The changes to add support of android annotations are in bold.

Hope this will help people who faced the same issue.

Subscribe to RSS Feed Follow me on Twitter!