Android Studio: Use AndroidAnnotations

牧云@^-^@ 提交于 2019-11-30 03:14:44

I had the same issues, followed the instructions for configuring aa with intelliJ, now it works like a charm does.

AA intelliJ config page will point you to this post...

http://www.ashokgelal.com/2012/12/setting-up-intellij-idea-12-with-maven-actionbarsherlock-roboelectric-androidannotations/

...the above post walks you through setting up various libs in intelliJ, scroll towards the bottom for AA.

The main thing I had to do that I did not have to do in eclipse was go to Preferences > Compiler > Annotation Processors and set my Processor Path to something like...

[PATH TO AA JARS]/androidannotations-2.7.jar:[PATH TO AA JARS]/androidannotations-api-2.7.jar:[PATH TO AA JARS]/codemodel-2.4.1.jar

This is what works for me:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

configurations {
    apt
}

repositories {
    mavenRepo url: 'https://oss.sonatype.org/content/repositories/snapshots/'
}

ext.androidAnnotationsVersion = '3.0-SNAPSHOT';

dependencies {
    compile 'com.android.support:support-v4:18.0.+'

    apt "org.androidannotations:androidannotations:$ext.androidAnnotationsVersion"
    compile "org.androidannotations:androidannotations-api:$ext.androidAnnotationsVersion"
}

android {
    compileSdkVersion 18
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 18
    }
}

android.applicationVariants.all { variant ->
    ext.aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
    ext.aptOutput.mkdirs()

    variant.javaCompile.options.compilerArgs += [
            '-processorpath', configurations.apt.asPath,
            '-AandroidManifestFile=' + variant.processResources.manifestFile,
            '-s', ext.aptOutput
    ]
}

After that I need to mark build/sources/apt-generated/debug as source in Android Studio by right clicking it and selecting Mark Directory as > Source Root

If you don't have problems compiling and just after seeing the generated classes in the IDE, then you need to check if target/generated-sources/annotations is checked as Source Folder.

That would be File > Project Structure > Modules > Sources Tab, then look for the folder and tag it as Sources. The folder will turn blue and will be listed on the Source Folder list.

As Android Studio is based on IntelliJ did you try to follow the configuration guideline on AndroidAnnotation's wiki ?

If you're using gradle you should check this page which explains how to configure the AndroidAnnotation's plugin :

buildscript {
    repositories {
        mavenCentral()
    }

    def gradleAndroidAnnotationsPluginVersion = '0.3.0'

    dependencies {
        classpath "net.ealden.gradle.plugins:gradle-androidannotations-plugin:$gradleAndroidAnnotationsPluginVersion"
    }
}

apply plugin: 'androidannotations'
apply plugin: 'idea'

androidAnnotationsVersion = '2.2'

I didn't try this new IDE yet. I'll check that soon.

It seems there is a way of making Android Studio work with AndroidAnnotations

http://code.google.com/p/android/issues/detail?id=55764

If you try to use Android Studio with a project running Android Annotations, you may run into a cryptic compiler issue:

incorrectly typed data found for annotation element public abstract int com.googlecode.androidannotations.annotations.EActivity.value() (Found data of type int)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!