Android Studio: Use AndroidAnnotations

2019-02-02 07:10发布

So I wanted to try the new Android Studio and imported my eclipse projects (I generated a gradle build file). Worked pretty good.

The only library which does not seem to work is AndroidAnnotations. I selected the androidannotations-2.7.jar file under File > Settings > Compiler > Annotation Processing.

As production source directory i selected "gen". But the generated file like MainActivity_ are not generated. What did I wrong?

6条回答
劳资没心,怎么记你
2楼-- · 2019-02-02 07:42

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

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-02 07:48

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-02-02 07:50

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

查看更多
迷人小祖宗
5楼-- · 2019-02-02 07:54

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

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-02-02 08:03

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.

查看更多
forever°为你锁心
7楼-- · 2019-02-02 08:03

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

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

查看更多
登录 后发表回答