美文网首页
模块化解耦框架RxFluxArchitecture5-Appli

模块化解耦框架RxFluxArchitecture5-Appli

作者: coolfireApy | 来源:发表于2019-05-12 10:29 被阅读0次

    相关系列文章

    模块化解耦框架RxFluxArchitecture1-框架简介

    模块化解耦框架RxFluxArchitecture2-基本功能实现

    模块化解耦框架RxFluxArchitecture3-订阅管理绑定生命周期

    模块化解耦框架RxFluxArchitecture4-依赖库与依赖注入

    模块化解耦框架RxFluxArchitecture5-Application多模块共存

    引言

    组件化的一个目的是为了业务解耦,业务模块有些特殊功能的初始化需要在Application中完成,而壳模块不需要知道具体如何完成这些初始化操作。这就需要解决多个业务模块中Application共存的问题。网上很多方案是使用反射或者在AndroidManifest中配置meta-data来实现。这里通过注解编译时生成代码和反射实现,简单方便,易于理解。

    框架图

    框架图.jpg

    方案实现

    1、初始化

    壳模块和业务模块中都需要如下配置:

    dependencies {
        //框架内核包括注解和接口,可以单独提取出来使用
        implementation 'com.github.coolfire2015.RxFluxArchitecture:core-arch:+'
        //注解编译器
        annotationProcessor 'com.github.coolfire2015.RxFluxArchitecture:core-arch-processor:+'
    }
    

    2、业务模块

    创建 Application 代理类,实现RxAppLifecycle接口,并使用@RxAppDelegate标注。

    @RxAppDelegate
    public class GanAppLifecyle implements RxAppLifecycle {
        @Override
        public void onCreate(Application application) {
            EventBus.builder()
                    .addIndex(new GanEventBusIndex())
                    .eventInheritance(false);
        }
    }
    

    3、壳模块

    SimpleApplication继承RxApp类,并使用@RxAppBody标注。项目中唯一存在。

    @RxAppBody
    public class SimpleApplication extends RxApp {
        
    }
    

    方案原理

    该方案采用GlideLibraryGlideModule的实现方式。

    1、业务模块中生成索引类

    业务模块中,@RxAppDelegate标注的RxAppLifecycle接口实现类,编译后会在业务模块路径/build/generated/source/apt/debug/com/huyingbao/core/processor/下会生成一个索引类RxIndexer_

    @RxIndex(
        modules = "com.huyingbao.module.wan.action.WanAppLifecyle"
    )
    public class RxIndexer_com_huyingbao_module_wan_action_WanAppLifecyle {
    }
    

    2、壳模块生成全局代理类

    在编译过程中,编译处理器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/附加的类中获取到所有业务模块中编译生成的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 (annotation != null) {
                Collections.addAll(rxAppLifecycles, annotation.modules());
            }
        }
        mProcessorUtil.debugLog("Found RxAppLifecycle: " + rxAppLifecycles);
        return rxAppLifecycles;
    }
    

    然后,在壳模块路径/build/generated/source/apt/debug/com/huyingbao/core/arch/下会生成一个RxAppLifecycle实现类RxAppLifecycleImpl(全局 Application 生命周期周期代理类)。

    final class RxAppLifecycleImpl implements RxAppLifecycle {
      @Override
      public void onCreate(Application application) {
        new GanAppLifecycle().onCreate(application);
        new WanAppLifecyle().onCreate(application);
      }
    }
    

    3、壳模块使用反射并调用方法

    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");
            }
        } catch (InstantiationException e) {
            throwIncorrectRxAppLifecycle(e);
        } catch (IllegalAccessException e) {
            throwIncorrectRxAppLifecycle(e);
        } catch (NoSuchMethodException e) {
            throwIncorrectRxAppLifecycle(e);
        } catch (InvocationTargetException e) {
            throwIncorrectRxAppLifecycle(e);
        }
        return result;
    }
    

    RxApp生命周期方法中调用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);
        }
    }
    

    源码

    开源模块化解耦框架RxFluxArchitecture,欢迎大家点赞Fork,更欢迎点评指导。

    相关文章

      网友评论

          本文标题:模块化解耦框架RxFluxArchitecture5-Appli

          本文链接:https://www.haomeiwen.com/subject/qdiqaqtx.html