I have been using this tutorial to educate myself on how to build APK outside Eclipse by just using command line (and Ant) - http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html
Now that build system will be shifting toward Gradle I would like to have similar advanced tutorial for reference. Most of the tutorials out there (like this one)deal just with basic stuff but I would like to know how to perform some "advanced" things like automatically replacing values in code during build (so that I can have multiple variants of APK).
Standard examples provided by Google are here
http://tools.android.com/tech-docs/new-build-system/gradle-samples-0.4.2.zip?attredirects=0&d=1
For automatically changing values in code use BuildConfig class. Examples are in the link above.
Variants are explained here http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
UPDATE
as this example gets bit stale here is pasetbin to newer version http://pastebin.com/FmcCZwA5
main difference is Robolectric support provided by plugin, and support library fetched from SDK internal repo
Older version
Less basic example with Robolectric and AndroidAnnotations
Use nexus
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
use AndroidAnnotation processor, Robolectric local tests and Jackson
configurations {
compile
testLocalCompile.extendsFrom(compile)
androidannotations.extendsFrom(compile)
}
dependencies {
compile files('libs/android-support-v4.jar')
compile 'org.androidannotations:androidannotations-api:3.0-SNAPSHOT'
compile 'com.github.japgolly.android:svg-android:2.0.3'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.12'
testLocalCompile 'junit:junit:4.8.2'
testLocalCompile 'org.robolectric:robolectric:2.2-SNAPSHOT'
testLocalCompile 'com.google.android:android:4.0.1.2'
testLocalCompile 'com.google.android:support-v4:r6'
testLocalCompile 'org.roboguice:roboguice:2.0'
androidannotations 'org.androidannotations:androidannotations:3.0-SNAPSHOT'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
Configure standard instrumentation tests
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
testPackageName "com.mypackage.myapp.test"
testInstrumentationRunner "com.maypackage.myapp.test.Runner"
}
}
Invoke AndroidAnnotations processor on all variants
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
]
}
}
Define sourcesets for Robolectric local tests
sourceSets {
testLocal {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
Local Robolectric tests task
task localTest(type: Test, dependsOn: assemble) {
testClassesDir = sourceSets.testLocal.output.classesDir
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
Run Robolectric in debug mode
localTest.doFirst {
jvmArgs '-Xdebug',
'-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
}
Related
I am using gradle build system to run Roboletric tests however I've encountered the problem that was described here Gradle Android unit tests that depend on an 'aar' but the solution only works for build tool version 0.9.+ and not 0.11.+
as I cannot find the exploded-aar directory. Any ideas?
Here's the partial build file
configurations {
testLocalCompile {
extendsFrom compile
}
}
sourceSets {
testLocal {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/res')
compileClasspath += configurations.testLocalCompile
runtimeClasspath += compileClasspath
}
}
dependencies {
testLocalCompile fileTree(dir: "$project.buildDir/intermediates/exploded-aar", include: "**/classes.jar")
}
task localTest(type: Test, dependsOn: assemble) {
testClassesDir = sourceSets.testLocal.output.classesDir
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'intermediates', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
check.dependsOn localTest
That change again in 0.12.2
http://tools.android.com/tech-docs/new-build-system
Move unzipped aar back in each project as a temporary fix for a possible race condition.
So you have to change the dependency back:
unitTestCompile fileTree(dir: "$project.buildDir/intermediates/exploded-aar/", include:"**/classes.jar")
(PS: do not know if is it the right way to post this in stackoverflow)
Fixed it: it seems like in the 0.11.+ build tool the exploded-aar folder is moved to root. I had to change the dependency:
testLocalCompile fileTree(dir: "${rootDir}/build/exploded-aar", include: "**/classes.jar")
Just when I'd achieved an effective development and build environment with the android-maven-plugin, the new kid on the block, Gradle, starts making inroads into the Android circles. Not being hot on Groovy and with the android-gradle plugin almost as fragmented as the OS itself I've hit some issues. Specifically around building library projects, with flavours and our buddy Robolectric.
Short version
I am at a loss as to what my next move should be upon encountering the gradle error;
Cannot add a SourceSet with name 'testDebug' as a SourceSet with that name already exists.
The error emanates from having productFlavours on a library (i.e. moving to the 0.9.2 android build system) and the gradle-android-test-plugin recently forked by the team over at Robolectric from Jake's creation (see here). I have followed all lines of investigation to near exhaustion and can report that the meer existence of the 'android-test' plugin within my library gradle file sends things awry.
Longer version
Here is the abridged application build.gradle file with pertinent information retained;
apply plugin: 'android-library'
apply plugin: 'android-test'
...
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 18
targetSdkVersion 19
versionCode buildNumber().toInteger()
versionName "1.0.0"
}
productFlavors {
estimote {
dependencies {
compile '<flavour specific dependency>'
}
}
radius {
dependencies {
compile '<flavour specific dependency>'
}
}
}
}
...
dependencies {
compile 'com.squareup.dagger:dagger:1.2.1'
compile 'com.squareup.dagger:dagger-compiler:1.2.1'
compile 'com.google.code.gson:gson:2.2.+'
// Testing dependencies
androidTestCompile 'org.mockito:mockito-core:1.9.5'
androidTestCompile 'com.squareup:fest-android:1.0.7'
androidTestCompile 'org.hamcrest:hamcrest-all:1.3'
androidTestCompile 'org.robolectric:robolectric:2.2'
androidTestCompile 'junit:junit:4.11'
}
And here is the abridged root build.gradle file.
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'
}
}
allprojects {
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
}
If you've got this far down the page, give yourself a pat on the back. Now, the eagle eyed amongst you have probably noticed the omission on the sourceSets redirection with commands akin to;
sourceSets {
androidTest {
setRoot('src/test')
}
}
After the initial error is corrected these lines will need to be reinstated to inform gradle of the project's structure. The project's structure is standard and looks like;
- project_name
+ gradle
- lib
+ flavour1
+ flavour2
- main
+ java
- test
+ java
build.gradle
build.gradle
gradle.properties
settings.gradle
What is being used
The app is using gradle-1.10-all, 0.9.2 android-gradle plugin and 0.9.+ gradle-android-test-plugin.
The question
How should the project be set-up/changed to facilitate Robolectric testing on a library with flavours? Is this even possible yet?
I ran into the same issue, dug into the code, fixed it, and submitted a pull request which has just now been merged. See my explanation on the PR for details, but it boils down to a bad optimization in the plugin code:
https://github.com/robolectric/robolectric-gradle-plugin/pull/70
As of today you need to clone the repo and build and install the plugin to your local maven repo. The next time they do a release to maven central (perhaps release 0.13.1?), you'll be able to use the plugin directly from there.
I have a project and migrating to gradle dependency, but I find myself with an issue trying to setup dagger with gradle, the first time I compile it work perfectly (or if I clean) but if I try it twice then it gives me error like:
Error:(13, 14) error: duplicate class: com.myapp.android.application.InjectingApplication$InjectingApplicationModule$$ModuleAdapter
I try using android-apt plugin and configured as in the documentation but I still get the same error (https://bitbucket.org/hvisser/android-apt/overview)
I also try using provided dependency instead like in this tutorial (https://github.com/frankdu/android-gradle-dagger-tutorial) of compile but no luck so far.
Do you have any ideas how to configure dagger and gradle?
EDIT
My build.gradle looks like this
apply plugin: 'android'
apply plugin: 'android-apt'
android {
compileSdkVersion 19
buildToolsVersion "19.0.2"
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
packageName "com.myapp.android"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':volley')
apt 'com.squareup.dagger:dagger-compiler:1.2.0'
compile 'com.squareup.dagger:dagger:1.2.0'
}
And my top level build.gradle look like this
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2'
}
}
allprojects {
repositories {
mavenCentral()
}
}
EDIT#2:
I tried with provided again as #Marco suggested no luck, I don't know if there is a library or a version of gradle that could be causing this problem, I'm currently using 1.10. On the bright side I did find a way to make it work, but I would love to do it by just adding the provided statement. The way I did it is the old way:
Define apt configuration
configurations {
apt
}
add Dagger compiler lib
apt 'com.squareup.dagger:dagger-compiler:1.2.0'
And implement the this hook to applicationVariant which as far as I know android-apt does something similar. Does this make sense? why?
def getSourceSetName(variant) {
return new File(variant.dirName).getName();
}
android.applicationVariants.each { variant ->
def aptOutputDir = project.file("build/source/apt")
def aptOutput = new File(aptOutputDir, variant.dirName)
android.sourceSets[getSourceSetName(variant)].java.srcDirs+= aptOutput.getPath()
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-s', aptOutput
]
variant.javaCompile.source = variant.javaCompile.source.filter { p ->
return !p.getPath().startsWith(aptOutputDir.getPath())
}
variant.javaCompile.doFirst {
aptOutput.mkdirs()
}
}
I am using dagger in this sample Volley Examples. I'm not experiencing any problems with dagger and I'm including the compiler using:
provided 'com.squareup.dagger:dagger-compiler:1.2.1'
It is work for me.
Step 1:
Add this code to you build.gradle
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
Step 2:
Add source code folder app/gen to you project. So you can add this code to you app/build.gradle (src/main/java is you project core code folder)
sourceSets.main {
java.srcDirs = ['src/main/java', 'gen']
}
Update Gradle plugin to (root/gradle)
classpath 'com.android.tools.build:gradle:2.2.0'
app/gradle
compile 'com.google.dagger:dagger:2.4'
annotationProcessor 'com.google.dagger:dagger-compiler:2.4'
So I have created an Android library and successfully compiled it into a .aar file. I called this aar file: "projectx-sdk-1.0.0.aar". Now I want my new project to depend on this aar so what I have done is follow this post.
But the post confuses me since I do not get the desired result:
The package name of the aar is : com.projectx.photosdk and the module inside is called sdk
Here is my current project structure:
|-SuperAwesomeApp
|--.idea
|--gradle
|--App
|---aars
|----projectx-sdk-1.0.0.aar
|---build
|---jars
|---src
|---build.gradle
And here is my Gradle build file:
apply plugin: 'android'
buildscript {
repositories {
mavenCentral()
flatDir {
dirs 'aars'
}
}
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:gridlayout-v7:19.0.1'
compile 'com.android.support:support-v4:19.0.1'
compile 'com.android.support:appcompat-v7:19.0.1'
compile 'com.projectx.photosdk:sdk:1.0.0#aar'
// compile files( 'aars/sdk-1.0.0.aar' ) // Does not work either
}
EDIT
The errors I am getting:
Failed to refresh Gradle project 'SuperAwesomeApp'
Could not find com.projectx.photosdk:sdk:1.0.0.
Required by:
SuperAwesomeApp:App:unspecified
You put your flatDir block in the wrong repostories block. The repositories block inside buildscript tells Gradle where to find the Android-Gradle plugin, but not the rest of the dependencies. You need to have another top-level repositories block like this:
repositories {
mavenCentral()
flatDir {
dirs 'aars'
}
}
I tested this and it works okay on my setup.
With recent versions of Android Studio, tested with 1.3, to use local .AAR file and not one fetched from maven/jcenter repository, just go to File > New > New module and choose Import .JAR/.AAR Package.
What you will end up with is a new module in your project that contains very simple build.gradle file that looks more or less like this:
configurations.create("default")
artifacts.add("default", file('this-is-yours-package-in-aar-format.aar'))
Of course, other projects have to reference this new module with regular compile project directive. So in a project that uses this new module which is simple a local .aar file has this in it's build.gradle
[...]
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
[...]
compile project(':name-of-module-created-via-new-module-option-described-above')
}
[...]
In Android Studio 3.1.3 with gradle 3.0.1.
Simply adding implementation fileTree(dir: 'libs', include: ['*.aar']) or implementation files('libs/app-release.aar') without any other flatdir works.
These days (over 1 year after this question) with Android Studio >1.0, local dependency does work properly:
The android sdk looks for dependencies in a default local repo of: $ANDROID_HOME/extras/android/m2repository/
In a local library project you can publish the aar to this directory. Here's a snippet that can be added to your module's build.gradle file (ex: sdk/build.gradle)
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
+ "/extras/android/m2repository/")
pom.version = '1.0-SNAPSHOT'
pom.groupId = 'your.package'
pom.artifactId = 'sdk-name'
}
}
}
some reference gradle docs http://gradle.org/docs/current/userguide/artifact_management.html
In your library project, run ./gradlew uploadArchives to publish the aar to that directory
In the application project you want to use the library in, add the dependency to your project/app/build.gradle. compile 'your.package:sdk-name:1.0-SNAPSHOT'
For local dependency, the next gradle build should find the previously deployed archive and that's it!
In my case, I use the above for local dev, but also have a Bamboo continuous integration server for the Library that publishes each build to a shared Nexus artifact repository. The full library code to deploy the artifact then becomes:
uploadArchives {
repositories {
mavenDeployer {
if (System.getenv("BAMBOO_BUILDNUMBER") != null) {
// Deploy to shared repository
repository(url: "http://internal-nexus.url/path/") {
authentication(userName: "user", password: "****")
}
pom.version = System.getenv("BAMBOO_BUILDNUMBER")
} else {
// Deploy to local Android sdk m2repository
repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
+ "/extras/android/m2repository/")
pom.version = '1.0-SNAPSHOT'
}
pom.groupId = 'your.package'
pom.artifactId = 'sdk-name'
}
}
}
In order to tell applications to download from my internal Nexus repository, I added the internal Nexus maven repository just above jcenter() in both "repositories" blocks in the project/build.gradle
repositories {
maven {
url "http://internal-nexus.url/path/"
}
jcenter()
}
And application dependency then looks like compile 'your.package:sdk-name:45' When I update the 45 version to 46 is when my project will grab the new artifact from the Nexus server.
With the newest Gradle version there is now a slightly updated way of doing what Stan suggested (see maving publishing)
apply plugin: 'maven-publish'
publishing {
publications {
aar(MavenPublication) {
groupId 'org.your-group-id'
artifactId 'your-artifact-id'
version 'x.x.x'
// Tell maven to prepare the generated "*.aar" file for publishing
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
}
}
repositories {
maven {
url("file:" + System.getenv("HOME") + "/.m2/repository")
}
}
}
It seems adding .aar files as local dependency is not yet supported(Planned to be supported in 0.5.0 Beta)
https://code.google.com/p/android/issues/detail?id=55863
But the way you are using your library in dependency will only work if your library is on central maven repository or in the local maven repository.
Refer this for How to use local maven repository to use .aar in module dependencies.
http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds
This is for Kotlin DSL (build.gradle.kts) assuming you put the files in my-libs subdirectory relative to where the build file is located:
dependencies {
implementation(
fileTree("my-libs/") {
// You can add as many include or exclude calls as you want
include("my-first-library.aar")
include("another-library.aar")
// You can also include all files by using a pattern wildcard
include("*.jar")
exclude("the-bad-library.jar")
}
)
// Other dependencies...
}
For more ways to do this, see Gradle documentations and this post and this post.
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 activity for the app. In addition, it does not appear that anything is generated by AndroidAnnotations.
I have added androidannotations-api-2.7.1.jar as a dependency for my project, and enabled annotation processing with the processor path the path to androidannotations-2.7.1.jar, which is in a separate folder from androidannotations-api-2.7.1.jar. I have checked store generated sources relative to module content root, and tried many different directories for the sources -- from generated, to gen/aa, to (currently) build/source/aa to match where it seems the generated files are created in Android Studio. Nothing has worked. I have changed the activity name in the manifest to Activity_, and set the configuration to launch this when the project is run.
The only other dependencies I have are android-support-v4 and ActionBarSherlock. I have tried with both of these disabled, to no result. I initially planned to use Roboguice in conjunction with AndroidAnnotations, but have disabled it for the time being to try to focus on this issue.
I am also using, or trying to use, Gradle. This is currently my build.gradle:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/actionbarsherlock-4.3.1.jar')
compile files('libs/androidannotations-api-2.7.1.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
}
}
However, I haven't really figured out how Gradle works, so I mostly just manually added the dependencies as you would a normal project, then put the compile lines in Gradle so the project would compile properly. I know this is probably not the correct way to use it.
My activity and its layout are fairly standard, I just copied them from the official guide to get started with AndroidAnnotations.
UPDATE: So I just went back to Maven to test the build with that, and I noticed something strange. It seems that even with how I have it set up in Maven, nothing is generated. However, with the Maven build I can run the project without changing the activity name in the manifest to Activity_ and the project will compile and run correctly. This is very odd and seems like it could either further confuse the problem, or simplify it if it is indicative of something with Gradle as well.
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
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
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.
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 an update to the answer that does the following things:
works with Android Studio 0.1.9
removes syntax errors from IDE (even on rebuilds)
uses androidannotations without dagger
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
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".