4

Android Annotations浅析

 3 years ago
source link: https://blog.csdn.net/ljphhj/article/details/37601173
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Android Annotations浅析

这阵子遇到了好多事情,挺久没来更新博文了,这两天在学这个开源框架Android Annotations,用起来感觉挺方便的,

相信用过Spring注解的孩子理解起来应该比较容易!就是配置起来比较吃力。

关于AndroidAnnotaions的介绍,网上已经很多了,我这里不再累赘。

  1、AndroidAnnotations官网:http://androidannotations.org/(也许你需要翻墙)

  2、eclipse中使用androidannotations的配置方法说明:https://github.com/excilys/androidannotations/wiki/Eclipse-Project-Configuration

  3、Android Studio中配置AndroidAnnotations:(这个是我这篇博文中要涉及到的!)

一、Android Studio配置androidannotations环境。

1、首先你建立一个module之后,在对应的app中会有一个名为build.gradle的文件(该module有效),而在整个项目外面也会有一个名为build.gradle的文件(全局有效)【这个工具中的application的目录下(相当于Eclipse下的workspace)是可以有多个module的(相当于Eclipse下的project)】

2、我们配置的时候大概要分为下面两步

在局部build.gradle中(加入红色字体部分):

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.0.1'



android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"


    defaultConfig {
        applicationId "com.tongbu.mytest"
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'

apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"

}


apt {
    arguments {
        androidManifestFile variant.processResources.manifestFile
        resourcePackageName 'com.tongbu.mytest'
    }
}

在全局build.gradle中(加入红色字体部分):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
    }
}


allprojects {
    repositories {
        jcenter()
    }
}

如果以上的都顺利完成了,那么恭喜你已经快配置完了,还需要把AndroidManifest.xml中的Activity的名字后面加上 _(下划线),因为这个框架解析编译的时候,比如类MainActivity会被解析成MainActivity_.class,所以在清单文件中我们要在Activity的名字后面加一个下划线,或者androidannotation会报错!

但并不会这么顺利,在你补充完下划线之后,你会发现会提示你找不到MainActivity_这个东东

那么怎么办呢??我们说了它是在编译的时候整出来的,那我们只要按一下编译的按钮即可生成了!!

这样子androidannotation在android studio上的环境就配置好了,Eclipse的话资料比较多,再这里就不介绍了

二、一个Demo来了解androidannotations的部分注解

几个方法的先后调用顺序:

资源涉及的注解(不一一列举):


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK