Android配置AndroidAnnotations注解框架

一 前言

android 快速开发框架
项目地址:https://github.com/excilys/androidannotations
文档介绍:https://github.com/excilys/androidannotations/wiki
官网网址:http://androidannotations.org/
特点:(1) 依赖注入:包括 view,extras,系统服务,资源等等
(2) 简单的线程模型,通过 annotation 表示方法运行在 ui 线程还是后台线程
(3) 事件绑定:通过 annotation 表示 view 的响应事件,不用在写内部类
(4) REST 客户端:定义客户端接口,自动生成 REST 请求的实现
(5) 没有你想象的复杂:AndroidAnnotations 只是在在编译时生成相应子类
(6) 不影响应用性能:仅 50kb,在编译时完成,不会对运行时有性能影响。
其他:与 roboguice 的比较:roboguice 通过运行时读取 annotations 进行反射,所以可能影响应用性能,而 AndroidAnnotations 在编译时生成子类,所以对性能没有影响

二 添加依赖包

有两种方法:
1 直接添加jar包(不推荐)
     需要的包有AndroidAnnotations-3.2 AndroidAnnotations-api-3.2 点击下载(如果你不使用新功能,推荐使用3.2的jar包,因为新的jar包对gradle的版本有要求,需要 Android Plugin 2.3.0,如: classpath 'com.android.tools.build:gradle:2.3.0,这样你的AS
可能要进行升级工作)。
(1) 在新建的普通项目里面,新建一个compile-libs文件夹,将下载来的androidannotation-xx.jar添加到compile-libs文件夹下,将androidannotation-api-xx.jar添加到libs文件夹下,如图:

(2)把这两个jar包分别Add As Library..., 然后在app的build文件dependencies节点下,有如下两行:
 
 
compile files('libs/androidannotations-api-3.2.jar')
compile files('compile-libs/androidannotations-3.2.jar')
(3)添加apt插件
       如果不添加apt,可能会遇见下面的第二个异常。
首先在你工程的build文件添加apt插件路径如下:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
完整代码如下:
buildscript {
....
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
//添加apt的路径
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
再着在app的build文件的头部,添加如下:
apply plugin: 'android-apt'
def AAVersion = '3.2+'
接着配置插件:
 
 
//配置apt插件
apt {
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
// androidManifestFile variant.outputs[0].processResources.manifestFile
//必须填你自己的包名 可 参见 applicationId "com.ecric.http"
resourcePackageName 'com.ecric.http'
}
}
说明:注意com.ecric.http要换成自己的项目包,再者,添加添加如下代码(部分代码省略):
android {
....
packagingOptions {
exclude 'META-INF/maven/org.androidannotations/androidannotations-api/pom.xml'
exclude 'META-INF/maven/org.androidannotations/androidannotations-api/pom.properties'
exclude 'androidannotations-api.properties'
}

}
最后就是在 dependencies节点添加如下:
apt "org.androidannotations:androidannotations:$AAVersion"
2 使用compile 添加依赖包(推荐)
        至于这种方法相对简单灵活,省去方法1中的(1)(2)步,直接进行(3),然后 dependencies节点添加如下:
compile "org.androidannotations:androidannotations-api:$AAVersion"
如果想要使用最新的AndroidAnnotations ,查看下面的链接:
如果你的android studio 版本是3.0+,则可以如下:
buildscript {
 repositories {
 google()
 jcenter()
 }
 dependencies {
 // replace with the current version of the Android plugin
 classpath "com.android.tools.build:gradle:3.0.0"
 }
 }
  
 repositories {
 mavenLocal()
 mavenCentral()
 }
  
 apply plugin: "com.android.application"
  
 def AAVersion = "4.5.0-SNAPSHOT" // change this to your desired version, for example the latest stable: 4.4.0
 dependencies {
 annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
 implementation "org.androidannotations:androidannotations-api:$AAVersion"
 }
  
 android {
 compileSdkVersion 26
 buildToolsVersion "26.0.2"
  
 defaultConfig {
 minSdkVersion 14
 targetSdkVersion 26
  
 // If you have different applicationIds for buildTypes or productFlavors uncomment this block.
 //javaCompileOptions {
 // annotationProcessorOptions {
 // arguments = ['resourcePackageName': "org.androidannotations.sample"]
 // }
 //}
 }
 }

三 使用

1 注解Activity:
@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

}
     使用EActivity注解过的Activity,会自动生成一个 Activity_ ,例如上面会生成的MainActivity_,当然在使用的时候必须的使用 MainActivity_ 而不是 MainActivity,如在启动时,配置文件如下:
<activity android:name=".MainActivity_">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
当然这是最简单的使用,更多的就参考官网文档吧:
或者
或者

四 混淆

############====================androidannotations====================##############
-dontwarn org.androidannotations.**
-keep class org.androidannotations.** {*;}
##处理注释属性
#-keepattributes *Annotation*
#
-dontwarn org.springframework.**
#-keep class org.springframework.**{*;}

五 遇到的异常

解决方案:

2 Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > java.lang.NoClassDefFoundError: com/helger/jcodemodel/AbstractJClass
解决方案:
此处提供的网址下该版本的jar包尚未发布,所以提示的更新是存在问题的,https://jcenter.bintray.com/com/android/tools/build/gradle/目录下查看最新的gradle插件版本,然后在项目dependencies里修改classpath到目前为止最新的gradle组件版本即可,在此过程中也许还会出现,如下异常:
Error:(1, 0) Minimum supported Gradle version is 3.3. Current version is 2.10. If using the gradle wrapper, try editing the distributionUrl in /Users/luzhenbang/AndroidStudioProjects/HTTPProtocolApp/gradle/wrapper/gradle-wrapper.properties to gradle-3.3-all.zip
<a href="openFile:/Users/luzhenbang/AndroidStudioProjects/HTTPProtocolApp/app/build.gradle">Open File</a>
就是说当前的AS版本过低,不支持gradle:xxx需要的AS的版本最小为3.3,那么就只能升级AS版本了。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值