内容简介:组件化的一个目的是为了业务解耦,子 module 有些特殊功能的初始化需要在网上很多方案是使用反射或者在这里通过注解编译时生成代码+反射实现,简单方便,易于理解。
组件化的一个目的是为了业务解耦,子 module 有些特殊功能的初始化需要在 Application
中完成,而壳 module 不需要知道具体如何完成这些初始化操作。这就需要解决多个 module 中 Application
共存的问题。
网上很多方案是使用反射或者在 AndroidManifest
中配置 meta-data
来实现。
这里通过注解编译时生成代码+反射实现,简单方便,易于理解。
方案实现
- 初始化,子 module 和壳 module 中都需要如下配置。
dependencies {
//框架内核包括注解和接口,可以单独提取出来使用
implementation 'com.github.coolfire2015.RxFluxArchitecture:core-arch:1.0.4'
//注解编译器
annotationProcessor 'com.github.coolfire2015.RxFluxArchitecture:core-arch-processor:${RX_FLUX_ARCHITECTURE}'
}
复制代码
-
子 module 中创建
AppLifecycle类实现RxAppLifecycle接口,并使用@RxAppDelegate标注。
@RxAppDelegate
public class GanAppLifecyle implements RxAppLifecycle {
@Override
public void onCreate(Application application) {
EventBus.builder()
.addIndex(new GanEventBusIndex())
.eventInheritance(false);
}
}
复制代码
-
壳 module 中的
Application继承RxApp类,并使用@RxAppBody标注。项目中唯一存在
@RxAppBody
public class SimpleApplication extends RxApp {
}
复制代码
方案原理
该方案采用 Glide
中 LibraryGlideModule
的实现方式。
-
子 module 中,
@RxAppDelegate标注的RxAppLifecycle接口实现类,编译后会在子 module 路径/build/generated/source/apt/debug/com/huyingbao/core/processor/下会生成一个索引类RxIndexer_包名_类名.java。
@RxIndex(
modules = "com.huyingbao.module.wan.action.WanAppLifecyle"
)
public class RxIndexer_com_huyingbao_module_wan_action_WanAppLifecyle {
}
复制代码
-
壳 module 在编译过程中,编译处理器
RxArchProcessor检查代码中是否唯一有使用@RxAppBody标注并继承RxApp的类
private void processRxAppBody(RoundEnvironment env) {
for (TypeElement element : mProcessorUtil.getElementsFor(RxAppBody.class, env)) {
if (mProcessorUtil.isRxApp(element)) {
mRxAppList.add(element);
}
}
mProcessorUtil.debugLog("got app modules: " + mRxAppList);
if (mRxAppList.size() > 1) {
throw new IllegalStateException("You cannot have more than one RxApp, found: " + mRxAppList);
}
}
复制代码
-
如果有,则从当前包
/com/huyingbao/core/processor/附加的类中获取到所有子 module 中编译生成的RxIndexer_类,再从RxIndexer_类的注解@RxIndex中取出 modules 字段中存储的RxAppLifecycle接口实现类名。
private Set<String> getIndexedClassNames(PackageElement packageElement) {
Set<String> rxAppLifecycles = new HashSet<>();
//获取当前包元素附加的所有元素
List<? extends Element> rxAppLifecycleGeneratedElements = packageElement.getEnclosedElements();
for (Element indexer : rxAppLifecycleGeneratedElements) {
RxIndex annotation = indexer.getAnnotation(RxIndex.class);
// If the annotation is null, it means we've come across another class in the same package
// that we can safely ignore.
if (annotation != null) {
Collections.addAll(rxAppLifecycles, annotation.modules());
}
}
mProcessorUtil.debugLog("Found RxAppLifecycle: " + rxAppLifecycles);
return rxAppLifecycles;
}
复制代码
-
在壳 module 路径
/build/generated/source/apt/debug/com/huyingbao/core/arch/下会生成一个RxAppLifecycle实现类RxAppLifecycleImpl。
final class RxAppLifecycleImpl implements RxAppLifecycle {
@Override
public void onCreate(Application application) {
new GanAppLifecycle().onCreate(application);
new WanAppLifecyle().onCreate(application);
}
}
复制代码
-
在
RxApp类中通过反射获取RxAppLifecycleImpl实例对象。
private RxAppLifecycle getAnnotationGeneratedRxAppLifecycleImpl() {
RxAppLifecycle result = null;
try {
Class<RxAppLifecycle> clazz = (Class<RxAppLifecycle>)
Class.forName("com.huyingbao.core.arch.RxAppLifecycleImpl");
result = clazz.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException e) {
if (Log.isLoggable(TAG, Log.WARN)) {
Log.w(TAG, "Failed to find RxAppLifecycleImpl. You should include an"
+ " annotationProcessor compile dependency on com.github.coolfire2015.RxFluxArchitecture:core-arch-processor"
+ " in your application and a @RxAppDelegate annotated RxAppLifecycle implementation"
+ " and a @RxAppBody annotated RxApp implementation");
}
// These exceptions can't be squashed across all versions of Android.
} catch (InstantiationException e) {
throwIncorrectRxAppLifecycle(e);
} catch (IllegalAccessException e) {
throwIncorrectRxAppLifecycle(e);
} catch (NoSuchMethodException e) {
throwIncorrectRxAppLifecycle(e);
} catch (InvocationTargetException e) {
throwIncorrectRxAppLifecycle(e);
}
return result;
}
复制代码
在 Application
生命周期方法中调用 RxAppLifecycleImpl
实例对象中的对应方法。
private RxAppLifecycle mGlobalRxAppLifecycle;
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
mGlobalRxAppLifecycle = getAnnotationGeneratedRxAppLifecycleImpl();
}
@Override
public void onCreate() {
super.onCreate();
if (mGlobalRxAppLifecycle != null) {
mGlobalRxAppLifecycle.onCreate(this);
}
}
复制代码
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 实体继承与@Builder注解共存
- 安全与公平共存的智慧城市
- Centos多版本php共存
- Spring Cloud与Dubbo共存方案总结
- 飞特 3.1,商城与后台起飞,模板与注解共存
- 飞特 3.1,商城与后台起飞,模板与注解共存
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Ajax模式与最佳实践
Christian Gross / 李锟、张祖良、蔡毅、赵泽欣 / 电子工业出版社 / 2007-3 / 49.80元
Ajax 正在将我们带入到下一代的网络应用中。 本书深入探讨了动态的网络应用,将Ajax和REST集成在一起作为单独的解决方案。一个很大的优势是,与Ajax相似,REST可以和现今存在的技术一起使用。现在上百万的客户端计算机都是基于Ajax的,上百万的服务器是基于REST的。 无论你是否已经开发过Ajax应用程序,这都是一本理想的书。因为这本书描述了各种各样的模式和最好的实践经验。通过此......一起来看看 《Ajax模式与最佳实践》 这本书的介绍吧!