为EventBus添加注解处理器
来源:互联网 发布:微商城数据统计 编辑:程序博客网 时间:2024/06/10 01:07
EventBus同时支持编译时注解和运行时注解, 当没配置编译时注解处理器时, 会自动通过反射查找运行时的注解, 这将有轻微的性能损失. 可以添加注解处理器, 让其支持编译时注解.
build.gradle(Project)中添加 classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' }}
app/build.gradle(Module)的配置
添加 apply plugin: 'com.neenbedankt.android-apt'......android { compileSdkVersion 23 buildToolsVersion "23.0.3"...... apt { arguments { //生成java文件路径, 比如 eventBusIndex "com.maimiho.config.EventBusIndex" } }......}dependencies { compile 'org.greenrobot:eventbus:3.0.0' apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'}
然后正常的使用EventBus, 最终编译出的Apk中就能找到生成的文件EventBusIndex.class
如下:
package com.maimiho.config;import com.maimiho.EventBusActivity;import com.maimiho.entity.Entry;import java.util.HashMap;import java.util.Map;import org.greenrobot.eventbus.ThreadMode;import org.greenrobot.eventbus.meta.SimpleSubscriberInfo;import org.greenrobot.eventbus.meta.SubscriberInfo;import org.greenrobot.eventbus.meta.SubscriberInfoIndex;import org.greenrobot.eventbus.meta.SubscriberMethodInfo;public class EventBusIndex implements SubscriberInfoIndex{ private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX = new HashMap(); static { putIndex(new SimpleSubscriberInfo(EventBusActivity.class, true, new SubscriberMethodInfo[] { new SubscriberMethodInfo("onEvent", Entry.class, ThreadMode.MAIN) })); } private static void putIndex(SubscriberInfo paramSubscriberInfo) { SUBSCRIBER_INDEX.put(paramSubscriberInfo.getSubscriberClass(), paramSubscriberInfo); } public SubscriberInfo getSubscriberInfo(Class<?> paramClass) { paramClass = (SubscriberInfo)SUBSCRIBER_INDEX.get(paramClass); if (paramClass != null) { return paramClass; } return null; }}
0 0
- 为EventBus添加注解处理器
- 注解处理器
- 注解和注解处理器
- 注解(Annotation)注解处理器
- 如何为Delphi程序添加事件和事件处理器
- 如何为Delphi程序添加事件和事件处理器
- 英特尔AMD竞相为笔记本处理器添加图形功能
- 如何为Delphi程序添加事件和事件处理器
- [delphi]如何为Delphi程序添加事件和事件处理器
- 为Annotation注解添加各种属性
- iOS MKAnnotation协议为地图添加注解
- 注解(Annotation)--注解处理器
- 注解三之注解处理器
- 注解(Annotation)--注解处理器
- 注解(Annotation)--注解处理器
- Java注解(二):注解处理器
- Annotation注解处理器
- 插入式注解处理器
- 我读FaceNet
- sql2008和sql2012混合安装后打开 SQL SERVER 2008 R2配置管理器出现“远程过程调用失败”【0x800706be】的解决办法
- RAID认识(二)RAID关键技术的应用
- sbt初次使用配置
- eclipse中导入项目一直loading descriptor for问题
- 为EventBus添加注解处理器
- Node.js中同步函数异步编程
- unity发布APP到手机的屏幕显示方式
- HTTP协议经典详解
- 大数据需要学什么
- 网站502与504错误分析
- 44 leetcode - Rotate Array
- checkbox利用JQuer中 attr('checked', true)设置状态只有第一次有用
- oracle监听常见错误和解决思路