欢迎访问我的技术博客Bigflower http://www.flowerfat.com
最近刚刚开始由eclipse转studio , 感觉很生疏啊有木有。
有的朋友可能担心使用注解的方式可能会影响性能。但Annotation不会的。别问我为什么,我是看慕课网的一个老师讲的。
一般使用反射机制的注解可能会影响性能,不过Annotation嘛,好像是另一种方式,有兴趣的朋友可以自行了解哈
本文说说我是怎么将Annotations添加到Studio的,网上找了很多,借鉴了很多 http://blog.csdn.net/ljphhj/article/details/37601173
我只说说我的步骤哈
1. 新建一个工程
2. 修改app文件夹下的build.gradle
一共要修改3个地方:
第一个是在顶部,添加
如下:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.3.2'
第二个和第三个,添加
如下(注意了!!! resourcepackageName 后面的 是你的包名哈 就是把 com.**** 换成你的包名)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'com.flowerfat.annotation'
}
}
3. 修改最外层的build.gradle
具体修改如下:
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
通过上述,就基本完成了。
然后我们简单的看下实践:
原activity:
使用Annotation后的activity
具体用法去百度搜就可以了哈。
最后一步,把Manifest里的MainActivity后面加上下划线
再点击下这个,
OK, 大功告成。
话又说回来。后来觉得这个不是很好用,不够变通。于是在自己的小项目里都用黄油刀了ButterKnife : https://github.com/JakeWharton/butterknife
这个很简单,我就不说了哈