I am trying to create a project using AndroidAnnotations in Android Studio. When I build and run the project, everything seems to compile fine, yet I get nothing but a blank
adding dependencies doesn't do anything. JavaCompile task has to be notified about annotation processor. It was possible to access JavaCompile task in version 0.3:
configurations {
compile
androidannotations.extendsFrom(compile)
}
dependencies {
compile 'org.androidannotations:androidannotations-api:3.0-SNAPSHOT'
androidannotations 'org.androidannotations:androidannotations:3.0-SNAPSHOT'
}
android.buildVariants.each {
variant ->
variant.javaCompile.options.compilerArgs += [
'-classpath', configurations.compile.asPath,
'-processorpath', configurations.androidannotations.asPath,
'-processor', 'org.androidannotations.AndroidAnnotationProcessor',
'-AandroidManifestFile=' + variant.processResources.manifestFile
]
}
For gradle plugin 0.4 onwards compile tasks have to be appended in different way. Thanks to Artiom @ Android Project here is how entire build.gradle should look like
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
configurations {
compile
androidannotations.extendsFrom(compile)
}
dependencies {
compile files('libs/android-support-v4.jar')
compile 'org.androidannotations:androidannotations-api:3.0-SNAPSHOT'
androidannotations 'org.androidannotations:androidannotations:3.0-SNAPSHOT'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
packageName "org.labaa.aa"
testPackageName "org.labaa.aa.test"
testInstrumentationRunner "org.labaa.aa.test.Runner"
}
}
afterEvaluate { project ->
android.applicationVariants.each { variant ->
variant.javaCompile.options.compilerArgs += [
'-classpath', configurations.compile.asPath,
'-processorpath', configurations.androidannotations.asPath,
'-processor', 'org.androidannotations.AndroidAnnotationProcessor',
'-AandroidManifestFile=' + variant.processResources.manifestFile
]
}
}
This won't update Android Studio dependencies. Add androidannotations-api manually. Run build from commandline gradle installDebug
When compiling from Andoid Studio disable app launch otherwise launcher will complain about missing annotated activity.
One possible problem is the R class is not found. Android Studio doesn't place the R.java into the gen directory by default like eclipse. The solution is to go into Project Settings -> Facets -> Select the Android facet for your project -> Compiler tab, and change the "R.java and Manifest.java files" from "Run process-resources Maven task before Make" to "Generated by IDE".
with the answers here and the help of +Hugo Visser who answered me on my Google+ Question i got this build.grade configuration, which allows gradew builds AND also adds the apt output path in Android Studio as source directorys.
buildscript {
repositories {
mavenCentral()
}
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 'com.android.support:support-v4:13.0.+'
apt "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
compile "org.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 17
}
}
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()
}
}
UPDATE: updated for gradle android plugin 0.5.5; changed android.applicationVariants.each
to android.applicationVariants.all
The project now has up to date documentation for this case. A full example project also exists.
buildscript {
repositories {
mavenCentral()
}
dependencies {
// replace with the current version of the Android plugin
classpath 'com.android.tools.build:gradle:0.7.3+'
// the latest version of the android-apt plugin
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
}
}
apply plugin: 'android'
apply plugin: 'android-apt'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
apt {
arguments {
androidManifestFile variant.processResources.manifestFile
// If you're using Android NBS flavors you should use the following line instead of hard-coded packageName
//resourcePackageName android.defaultConfig.packageName
resourcePackageName "com.example.your.package.name"
// You can set optional annotation processing options here, like these commented options:
// logLevel 'INFO'
// logFile '/var/log/aa.log'
}
}
dependencies {
apt "org.androidannotations:androidannotations:3.0+"
compile "org.androidannotations:androidannotations-api:3.0+"
}
This is similar to robotoaster's response, but it works in 0.4.1 and it places the generated java source files in a new directory (consistent with the other generated source folders), which allows Android Studio to see the source and stop complaining. It also works with more annotation processors. Just add your annotation processors to the "apt" configuration.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.1'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
ext.daggerVersion = '1.0.0';
ext.androidAnnotationsVersion = '2.7.1';
configurations {
apt
}
dependencies {
apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"
compile "com.squareup.dagger:dagger:${daggerVersion}"
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 17
}
}
android.applicationVariants.all { variant ->
aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
println "****************************"
println "variant: ${variant.name}"
println "manifest: ${variant.processResources.manifestFile}"
println "aptOutput: ${aptOutput}"
println "****************************"
variant.javaCompile.doFirst {
println "*** compile doFirst ${variant.name}"
aptOutput.mkdirs()
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-s', aptOutput
]
}
}
UPDATE: This still works to compile, but with Android Studio 0.1.1 you can no longer edit your project structure with the UI, so you can't tell AS to look at the new source folder. I'd like to add the folder to a sourceSet, but variants don't seem to actually have their own sourceSets so I'm not sure yet where to put it.
UPDATE 2: You can get Android Studio 0.1.1 to recognize the apt-generated source files by right-clicking on build/source/apt_generated/debug in the project browser and selecting Mark Directory As->Source Root
UPDATE 3: Since gradle plugin 0.5.5 the android.applicationVariants.each
does not work anymore. Use android.applicationVariants.all
instead. See the changelog at android.com:
access to the variants container don't force creating the task.
This means android.[application|Library|Test]Variants will be empty during the evaluation phase. To use it, use .all instead of .each
this is an update to the answer that does the following things:
In order to achieve this I copy the output to build/source/r witch will remain marked as source during rebuilds.
import groovy.io.FileType
buildscript {
}
apply plugin: 'android'
repositories {
mavenCentral()
}
ext.androidAnnotationsVersion = '2.7.1';
configurations {
apt
}
dependencies {
apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 16
}
}
android.applicationVariants.each { variant ->
aptOutput = file("${project.buildDir}/source/r/${variant.dirName}")
println "****************************"
println "variant: ${variant.name}"
println "manifest: ${variant.processResources.manifestFile}"
println "aptOutput: ${aptOutput}"
println "****************************"
variant.javaCompile.doFirst {
println "*** compile doFirst ${variant.name}"
aptOutput.mkdirs()
aptOutput.eachFileRecurse FileType.FILES, {
if (it.name.equals('R.java')) {
return
}
it.delete()
}
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-s', aptOutput
]
}
}
Still pretty hacky improvement suggestions are welcomed
EDIT the android-apt way
Recently a package called android-apt https://bitbucket.org/hvisser/android-apt/overview was release that makes things a lot easier (version 1.1 has the support android annotations needs)
Simply update your build.gradle like so:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "com.neenbedankt.gradle.plugins:android-apt:1.1"
}
}
apply plugin: 'android-apt'
apt {
arguments {
androidManifestFile variant.processResources.manifestFile
}
}
dependencies {
apt 'com.googlecode.androidannotations:androidannotations:2.7.+'
}
A big thank you goes to hvisser the creator of android-apt