Android(安卓)Annotations 学习之路(二)解决Library工程使用AA报错的问题
最近公司项目我打算将AA框架用上,但是实际导入后发现会报错:“attribute value must be constant",后来查阅了大量资料,最终才查到原来问题出现在我现在这个工程是一个Library工程,而Library工程生成的R文件不是constant的,因此才会报错,最后我找到了解决办法,方法如下:
原来的注解方式是:
@ViewById(R.id.circlelogo) View mCircleView;
@EAvtivity等等方法相类似,都要改成如下的方法:
@ViewById(resName = "circlelogo") View mCircleView;
然后我发现光是这样还不够,原本在build过之后,对应的activity应该会生成一个activity_文件,依然没有生成,导致报activity not found的错,通过查询github上AA的官方文档,我找到了解决方法:
“The id fields of the R inner classes of an Android project, such asR.id.someId
, arestatic final
constants. This allows us to use them in annotations parameter, which only accept compile time constant values.
To solve problems related to library projects, the Android team decided that the R inner classes fields of a library project would not bestatic final
anymore. Which means we can't use them in annotations in library projects any more.
You can explicitely tell AA that the project is a library project by setting thelibrary
annotation processing optiontotrue
.
”
简单说来就是引用AA框架的时候需要告诉AA这个工程是一个Library工程,方法如下;
在工程的build.gradle中,我们加入框架的时候曾经写过一个apt方法,加入下面的代码:
apt { arguments { androidManifestFile variant.outputs[0]?.processResources?.manifestFile library 'true' // if you have multiple outputs (when using splits), you may want to have other index than 0 // you should set your package name here if you are using different application IDs // resourcePackageName "your.package.name" // You can set optional annotation processing options here, like these commented options: // logLevel 'INFO' // logFile '/var/log/aa.log' }}Library ‘true’就是告诉AA我们的工程师Library工程,在这里我们还可以对AA加上各种各样的定制,详情请到官网查找:https://github.com/excilys/androidannotations/wiki/CustomizeAnnotationProcessing#library
更多相关文章
- 从Eclipse迁移到Android(安卓)Stdio中
- Android(安卓)IPC框架分析 Binder,Service,Service manager
- Android主流框架RxJava+Retrofit+MVP
- Android(安卓)UI框架概览
- AndEngine引擎学习之环境配置以及示例修改
- Java集合框架——Android中的ArrayList源码分析
- Android(安卓)Eclipse中查看 Android框架源码
- 网络请求框架(Volley)
- Android中使用开源框架Citypickerview实现省市区三级联动选择