Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio + Gradle + Android Annotations [closed]

I am trying to add AndroidAnnotations to Android Studio project that has a gradle build system. Has anyone done this? Can anyone help me with this? I do not even know where to start. I know how to add libraries to gradle but AndroidAnnotations requires 2 jar files and I do not know what should I do.

like image 392
John Avatar asked Jul 28 '13 17:07

John


People also ask

Why does Gradle Sync Fail?

In some cases when your Gradle files are deleted or corrupted you will not be able to download new Gradle files in android studio. In this case, we have to delete the Gradle files which are present already and then again sync your project to download our Gradle files again.

How do I disable Gradle offline mode?

We can find the Gradle configuration in Eclipse by navigating to the Preferences -> Gradle section. We can see the Offline Mode config and toggle it off: As a result, we'll find that the offline mode works in Eclipse.

How do I sync Gradle with Android Studio?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.

How do I add annotations to Android support?

To enable annotations in your project, add the support-annotations dependency to your library or app. Any annotations you add then get checked when you run a code inspection or lint task.


1 Answers

After 4 months I am 4 months older and a little smarter :) If you want to use      
Annotations in Android use http://jakewharton.github.io/butterknife/. 
It is way better and it is easy to set up :)

Here is what you need to do:

  • You need to modify your build.gradle file (build file for your application module)

First add dagger and annotations version. You can also declare them in dependencies. This is just more convenientwhen you have a lot of dependencies.

ext.daggerVersion = '1.0.0';
ext.androidAnnotationsVersion = '2.7.1';

configurations {
    apt
}

Add dependencies:

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}"

}

Finnaly, add this. This adds path for compiler and creates a dir for generated files (this dir will be called apt_generated):

android.applicationVariants.each { 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
    ]
}
}

Oh, yes, and after you build your aplication, you need to go to project root/build/apt_generated, right click on folder and set "Mark as source root"

like image 81
Koc Avatar answered Sep 30 '22 13:09

Koc