我们开发过程中都需要写些findViewByid、serOnclickListener等类似的代码,虽然不费事,但是一个项目下来,工作量还是很大的。为了节省工作量,运生了很多对应的注解框架。网上的博客、身边的同事大多使用的是 xUtils、ButterKnife实现注解,我初次使用的也是ButterKnife。然而,今天小试了下Android Annotations注解框架,用起来确实比ButterKnife爽。其他的介绍和废话就不多说了,下面就介绍如何在

Android Studio中配置Android Annotations框架,以及我所发现的坑。 AA官网:http://androidannotations.org/ AA Github:https://github.com/excilys/androidannotations AA Github wiki页:https://github.com/excilys/androidannotations/wiki AA Github Gradle配置页:https://github.com/excilys/androidannotations/wiki/Building-Project-Gradle 以上网址是很有用的,尤其是Gradle配置页。AA框架配置比较蛋痛,看似简单,但是有很多小细节。我今天在网上搜了很多博客,都没弄成功,总是报错。英文不好而官网的配置有一直没找到。其实不用再去看其他人写的博客文档了,或过时或错误很误导人。接下来我带领大家根据官方Gradle配置搭建和使用AA框架。
一、新建一个项目。 二、在project根目录下的build.gradle文件中,添加:

  1. classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

这个地方的版本号是1.8,就用最新的吧,怎么知道这个最新的版本号呢?去这儿查:右键整个项目-》Open Module Settings-》点击左侧的app-》点击右边顶部的Dependencies-》右上角的+号-》选择1.Dependency-》输入com.neenbedankt.gradle.plugins:android-apt 回车查询版本号。 这个文件的配置就完成了。官网文档和许多网上的博客都说还要修改依赖库,即mavenCentral()发现也可以不改,无所谓。

  1. apply plugin: 'com.android.application'
  2. apply plugin: 'android-apt'  //1处添加
  3. def AAVersion = '4.0.0'
  4. android {
  5. compileSdkVersion 23
  6. buildToolsVersion "24.0.0"
  7. defaultConfig {
  8. applicationId "com.znke.androidannotations_test"
  9. minSdkVersion 15
  10. targetSdkVersion 23
  11. versionCode 1
  12. versionName "1.0"
  13. }
  14. buildTypes {
  15. release {
  16. minifyEnabled false
  17. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  18. }
  19. }
  20. }
  21. dependencies {
  22. compile fileTree(include: ['*.jar'], dir: 'libs')
  23. testCompile 'junit:junit:4.12'
  24. compile 'com.android.support:appcompat-v7:23.4.0'
  25. apt "org.androidannotations:androidannotations:$AAVersion"  //2处添加
  26. compile "org.androidannotations:androidannotations-api:$AAVersion"
  27. }
  28. apt {  //3处添加
  29. arguments {
  30. androidManifestFile variant.outputs[0]?.processResources?.manifestFile//new style
  31. //androidManifestFile variant.processResources.manifestFile           //old style
  32. resourcePackageName "com.znke.androidannotations_test"                //your pakcage name
  33. }
  34. }
apply plugin: 'com.android.application'
apply plugin: 'android-apt' //1处添加
def AAVersion = '4.0.0' android {
compileSdkVersion 23
buildToolsVersion "24.0.0" defaultConfig {
applicationId "com.znke.androidannotations_test"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0' apt "org.androidannotations:androidannotations:$AAVersion" //2处添加
compile "org.androidannotations:androidannotations-api:$AAVersion"
} apt { //3处添加
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile//new style
//androidManifestFile variant.processResources.manifestFile //old style
resourcePackageName "com.znke.androidannotations_test" //your pakcage name
}
}

说明:apply plugin: 'android-apt'  是引用下面的这部分: 三、接下来在app module下的build.gradle文件中添加配置:

  1. apt {
  2. arguments {
  3. androidManifestFile variant.outputs[0]?.processResources?.manifestFile//new style
  4. //androidManifestFile variant.processResources.manifestFile           //old style
  5. resourcePackageName "com.znke.androidannotations_test"                //your pakcage name
  6. }
  7. }
apt {
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile//new style
//androidManifestFile variant.processResources.manifestFile //old style
resourcePackageName "com.znke.androidannotations_test" //your pakcage name
}
}

apt这部分是需要查找和解析androidManifestFile文件吧!切记,需要注意的地方

  1. androidManifestFile variant.outputs[0]?.processResources?.manifestFile
androidManifestFile variant.outputs[0]?.processResources?.manifestFile

这句话,中间有问号,根据官网的来。有很多博客上都不是这么写的,坑死我了,总是报属性为空错误。里面的resourcePackageName可不写,无大碍。def AAVersion = '4.0.0'部分,就是定义了一个变量名AAVersion的值是'4.0.0',此处版本号用最新的吧。在dependencies 节点中添加

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

两句,引用上面的版本号,貌似也可直接把$AAVersion替换成4.0.0版本号,去掉def AAVersion = '4.0.0',试过可行。算了还是根据上面官网的配置来吧。 def AAVersion = '4.0.0'里面的版本号根据org.androidannotations:androidannotations去搜索查询(依照最开始说的方式查询)。

配置完毕。接下来做个小例子试试手。第一个界面,一个输入框和一个按钮。点击按钮获取值,跳转到第二个页面并显示值。就这么简单,看看AA注解怎么用。上代码:

  1. AndroidManifest.xml
  2. <application
  3. android:allowBackup="true"
  4. android:icon="@mipmap/ic_launcher"
  5. android:label="@string/app_name"
  6. android:supportsRtl="true"
  7. android:theme="@style/AppTheme">
  8. <activity android:name=".MainActivity_">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <activity android:name=".Main2Activity_"/>//对,没错,最后面加下划线
  15. </application>
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity_">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity_"/>//对,没错,最后面加下划线
</application>
  1. MainActivity
  2. @EActivity(R.layout.activity_main)  //注入页面
  3. public class MainActivity extends AppCompatActivity {
  4. @ViewById
  5. public EditText username;//如果页面的id与此处对象名一样,@ViewById后面可以省略
  6. @Override
  7. protected void onCreate(@Nullable Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. //setContentView(R.layout.activity_main);//这儿不要了
  10. //跟UI无关的数据初始化可以写在这里
  11. }
  12. @Click
  13. void submit(){//如果页面button的id和方法名一样,@Click后面可以省略
  14. //跳转到Main2Activity界面,看看这个跳转牛逼不,包含了intent,extra带参数,start启动。
  15. Main2Activity_.intent(this).extra("msg",username.getText().toString()).start();
  16. }
  17. }
MainActivity
@EActivity(R.layout.activity_main) //注入页面
public class MainActivity extends AppCompatActivity { @ViewById
public EditText username;//如果页面的id与此处对象名一样,@ViewById后面可以省略 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);//这儿不要了 //跟UI无关的数据初始化可以写在这里 } @Click
void submit(){//如果页面button的id和方法名一样,@Click后面可以省略
//跳转到Main2Activity界面,看看这个跳转牛逼不,包含了intent,extra带参数,start启动。
Main2Activity_.intent(this).extra("msg",username.getText().toString()).start();
}
}
  1. R.layout.activity_main
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <EditText
  7. android:id="@+id/username"
  8. android:layout_width="match_parent"
  9. android:layout_height="200dp"
  10. android:gravity="left" />
  11. <Button
  12. android:id="@+id/submit"
  13. android:layout_width="match_parent"
  14. android:layout_height="50dp"
  15. android:layout_alignParentBottom="true"
  16. android:text="提交" />
  17. </RelativeLayout>
R.layout.activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="left" /> <Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:text="提交" /> </RelativeLayout>
  1. Main2Activity
  2. @EActivity(R.layout.activity_main2)
  3. public class Main2Activity extends AppCompatActivity {
  4. @ViewById
  5. public TextView text;
  6. //获取intent里面的扩展值
  7. @Extra
  8. public String msg;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. //@EActivity(R.layout.activity_main2)处已经setContentView
  13. //setContentView(R.layout.activity_main2);
  14. /**
  15. * 请关注Main2Activity_源码分析
  16. * 懂我的意思吗?
  17. */
  18. /*
  19. Main2Activity_类的定义
  20. public final class Main2Activity_ extends Main2Activity
  21. implements HasViews, OnViewChangedListener {*/
  22. /*
  23. Main2Activity_类的onCreate方法
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. ......
  27. super.onCreate(savedInstanceState);
  28. ......
  29. setContentView(R.layout.activity_main2);
  30. }*/
  31. }
  32. @Override
  33. protected void onStart() {
  34. super.onStart();
  35. text.setText(msg);
  36. }
  37. }
Main2Activity
@EActivity(R.layout.activity_main2)
public class Main2Activity extends AppCompatActivity { @ViewById
public TextView text; //获取intent里面的扩展值
@Extra
public String msg; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//@EActivity(R.layout.activity_main2)处已经setContentView
//setContentView(R.layout.activity_main2); /**
* 请关注Main2Activity_源码分析
* 懂我的意思吗?
*/
/*
Main2Activity_类的定义
public final class Main2Activity_ extends Main2Activity
implements HasViews, OnViewChangedListener {*/ /*
Main2Activity_类的onCreate方法
@Override
public void onCreate(Bundle savedInstanceState) {
......
super.onCreate(savedInstanceState);
......
setContentView(R.layout.activity_main2);
}*/
} @Override
protected void onStart() {
super.onStart(); text.setText(msg); }
}

此文件中,我为什么没有吧text.setText(msg);放在onCreate方法中,而放在了onStart中?你试试,根据错误排查。

  1. R.layout.activity_main2
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:id="@+id/text"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:gravity="center"
  11. android:textSize="30sp" />
  12. </RelativeLayout>
R.layout.activity_main2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp" />
</RelativeLayout>

代码使用部分,初略的贴出来了,有注释。 本文重点说配置,正确引导初学者。

demo下载

http://blog.csdn.net/fesdgasdgasdg/article/details/51835461

Android Studio配置Android Annotations框架详解--说说那些坑的更多相关文章

  1. android studio配置android开发环境

    1.下载安装android-studio-bundle 地址:https://developer.android.com/sdk/index.html 注意:指定android sdk和android ...

  2. android studio下的NDK开发详解(一)

    源地址:http://www.voidcn.com/blog/chengkaizone/article/p-5761016.html 好记性不如烂笔头,开始坚持写博客,学一点记一点,只为了生活更好. ...

  3. Android Studio 2.2 新功能详解

    Tamic /文 -译 http://blog.csdn.net/sk719887916/article/details/52672688 Android的Studio 2.2 已经可以在官网下载了. ...

  4. android studio配置AndroidAnnotations

    现在很多人都使用Android studio开发工具代替eclipse了,当然的 在eclipse使用的好的一些开发框架也会对应的在android studio上面使用. 参考文档:http://bl ...

  5. Android开发:文本控件详解——TextView(一)基本属性

    一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...

  6. Android开发工具Android Studio、Android SDK和Genymotion完全配置

    所谓“工欲善其事,必先利其器”.Android Studio 是谷歌推出一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提 ...

  7. Android 广播大全 Intent Action 事件详解

    Android 广播大全 Intent Action 事件详解 投稿:mrr 字体:[增加 减小] 类型:转载 时间:2015-10-20我要评论 这篇文章主要给大家介绍Android 广播大全 In ...

  8. Android零基础入门第13节:Android Studio配置优化,打造开发利器

    原文:Android零基础入门第13节:Android Studio配置优化,打造开发利器 是不是很多同学已经有烦恼出现了?电脑配置已经很高了,但是每次运行Android程序的时候就很卡,而且每次安装 ...

  9. Android安卓书籍推荐《Android驱动开发与移植实战详解》下载

    百度云下载地址:点我 Android凭借其开源性.优异的用户体验和极为方便的开发方式,赢得了广大用户和开发者的青睐,目前已经发展成为市场占有率很高的智能手机操作系统. <Android驱动开发与 ...

随机推荐

  1. 练习题|网络编程-socket开发

    1.什么是C/S架构? C指的是client(客户端软件),S指的是Server(服务端软件),C/S架构的软件,实现服务端软件与客户端软件基于网络通信. 2.互联网协议是什么?分别介绍五层协议中每一 ...

  2. ubuntu16.04通过apt-get方式安装MongoDB

    虽然Ubuntu本身也提供MongoDB安装包,但往往官网的安装包版本更新. hupeng@hupeng-vm:~$ apt-cache show mongodb-clients Package: m ...

  3. POJ 1655 Balancing Act (求树的重心)【树形DP】(经典)

    <题目链接> 题目大意:给你一棵树,任意去除某一个点后,树被分成了几个联通块,则该点的平衡值为所有分成的连通块中,点数最大的那个,问你:该树所有点中,平衡值最小的那个点是什么? 解题分析: ...

  4. HDU 1159 Common Subsequence 【最长公共子序列】模板题

    题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...

  5. python的pickle和shelve模块

    python中用于序列化的模块总结 目录 pickle模块 shelve模块 xml模块 pickle模块 介绍 Pickle的问题和所有其他编程语言特有的序列化问题一样,就是它只能用于Python, ...

  6. ogg - 从oracle到mysql的同步

    说明:这篇文章将介绍如何配置oracle到mysql的ogg同步 源端:ip-192.168.56.11 数据库类型-oracle 11.2.0.4目标端:ip-192.168.56.71 数据库类型 ...

  7. memcached协议解析

    本文转载自:http://www.ccvita.com/306.html 协议memcached 的客户端使用TCP链接与服务器通讯.(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的 ...

  8. 集合(4)—Collection之Set的使用方法

    定义 set接口及其实现类–HashSet Set是元素无序且不可重复的集合,被称为集. HashSet是哈希集,是Set的一个重要实现类 set中循环只能使用foreach和iterator这两个, ...

  9. How to modify analog output range of Arduino Due

    Voltage Translation for Analog to Digital Interface ADC How to modify analog output range of Arduino ...

  10. Scala:Method 小技巧,忽略result type之后的等号

    var x = 0 def IncreaseOne(): Int = { x += 1 x } def IncreaseOne() = { x += 1 x } def IncreaseOne = { ...