cool hit counter MVPArms official rapid componentization scheme open source , from 5Kstar trust_Intefrankly

MVPArms official rapid componentization scheme open source , from 5Kstar trust


Original article address: https://www.jianshu.com/p/f671dd76868f

Logo

<a name="0"></a>

0 preamble

MVPArms Since it was opened up two years ago, it has accumulated 5k star, 获得了上千个商业 sports event of信赖 harmony认可

Looking back two years ago, when MVPArms It's not born yet, MVPDagger2RxjavaRetrofit 这些技术 (located) at国内才刚刚开始流行, (located) at网上能 search up to of中文 do资料远没有现 (located) at这么丰富, 特别 be Dagger, (located) at网上能 search up to of文章甚至有很多讲 of be Square of Dagger1 , lack of learning materials combined with Dagger2 It's a tough nut to crack, and it's made me take a lot of detours on the way to learning.

Since then, beginners have been able to quickly build a MVP + Dagger2 + Retrofit + Rxjava The seeds of the project were planted deep in my heart, and I worked tirelessly on it later, MVPArms 终于诞生并开源了, 开源以后只 be一直坚持将 code, add comments to text harmony document 做 up to极致, 没想 up to of be, MVPArms 能发展 up to如今 of体量, 感谢开源!

<a name="0.1"></a>

0.1 originate

这 be MVPArms of originate, ArmsComponent of originate同样相似, 从去年开始, 组件化逐渐火热起来, 本人也 (located) at去年年初开始 (located) at公司 sports event中进行组件化, 一切还算顺利

The same seeds continued to be planted in my mind, I wanted to make it possible for beginners who were new to componentization to quickly build a small to medium sized componentization project, and after a year of continuous optimization, I finally decided to open source it (MVPArms Official Componentization Program ArmsComponent)

Github : 您 of Star be我坚持 of动力 ✊

<a name="0.2"></a>

0.2 Analysis of the componentization scheme

I've looked at a lot of componentization solutions, so I've summarized the three big points that are important in componentization:

  1. base library(网络请求、图片加载等) of封装
  2. routing framework (page jumping, service provisioning)
  3. 业务组件 of划分 harmony code隔离

<a name="0.2.1"></a>

0.2.1 业务组件 of划分 harmony code隔离

Let's start with the third point. 业务组件 of划分 harmony code隔离, 现 (located) at大部分 of文章都围绕着这点, 我这里发表 downwards个人 of观点, 第三点确实 be很重要 of一点, 不管 be大厂 of方案还 be小厂 of方案都有借鉴之处, 但 be这点也 be最不可能讨论出最终结果 harmony统一解决方案 of一点

Every company and every project is different, is the big factory solution really right for you? Not necessarily, large companies have dozens of business groups, hundreds of developers, their organizational structure and project size are not ordinary companies can be compared, if the rigid application of their solutions, more stringent and fine-grained code isolation, the output may not be as valuable as the price you pay, resulting in reduced efficiency, so the project leader should make flexible adjustments according to the actual situation of the project is the most important thing to do, which I will have a detailed introduction later

<a name="0.2.2"></a>

0.2.2 routing framework

陆续也有很多组件化方案开源自己 of routing framework, be个很不错 of开始, 我觉得大家写 of都不错, 各有各 of优势, 本方案也决定用别人 of routing framework The principle of writing your own is similar, not necessarily better than others, and you have to maintain it yourself, so why not choose a mature and stable one?

<a name="0.2.3"></a>

0.2.3 base library

很多组件化文章只 be (located) at讲如何拆分以及封装 base library, 但 be至今没看 up to有哪个组件化方案开源过完整 of base library of, 我猜测原因可能 be, 组件化方案都 be从商业 sports event不断 of业务迭代中逐渐完善 of, base library也属于公司 of核心机密, 所以不可能开源

但 be base library尤其重要, 特别 be兼容组件化 of base library, 这关乎 up to组件化方案 of根基, 根基都没有, 何谈其他更高级 of功能? 但 be从封装 up to完善一个兼容组件化 of base library be需要很长时间 of, 大多数中小公司 be不愿意投入这个成本 of

<a name="0.3"></a>

0.3 Advantages of ArmsComponent

ArmsComponent One-click automatic component generation, You can build the whole component architecture with one click, so you can experience pure foolproof component-based development, 虽然一键搭建功能让新手也可以一秒开始组件化 sports event, 但本方案还 be提供有上万字 of详细 document让您可以更深入 of了解本方案, 并且 JessYan A solemn promise, ArmsComponent blend in MVPArms 一起持续维护 downwards去, ArmsComponent 绝对 be一个真正用心对待 of组件化方案

MVPArms be一个开源两年, 成熟稳定, 涵盖大量主流技术且兼容组件化 of base library, MVPArms feasible ArmsComponent 成 because of了唯一提供完整 base library of组件化方案, 这就 be ArmsComponent 相对于其他组件化方案最大 of优势

Because of the base library presence, combined with the existing routing framework, 组件化 hit the target三个大点就已经占有两个(业务组件 of划分 harmony code隔离 will be described later), so use ArmsComponent Start a new project, and you can quickly componentize it, putting Demo (组件化 sports event雏形) 克隆 downwards来后, 稍作修改, 马上就可以投入 up to业务 of开发之中

ArmsComponent For new projects as well as those that have already started using MVPArms of sports event将会更加便捷, 有着优于其他组件化方案 of体验, 对于那些网络请求, 图片加载等基础功能乱七八糟散落 up to sports event各处, 没有统一抽离出来 of旧 sports event, 建议直接 use MVPArms , Start Componentization

If you do not want to use the MVPArms, 觉得接入成本太大, 没关系, 借鉴 downwards MVPArms harmony ArmsComponent of code, 尝试改造自己 of sports event, 也 be个不错 of选择

<a name="1"></a>

1 Introduction

Okay, let's get down to business!

<a name="1.1"></a>

1.1 什么 be组件化?

组件化简单概括就 be把一个功能完整 of App or module into multiple submodules, each of which can be compiled and run independently, or can be arbitrarily combined into another new App 或模块, 每个模块即不相互 depend on但又可以相互交互, 遇 up to某些特殊情况甚至可以升级或者降级

<a name="1.2"></a>

1.2 Why componentize?

Nowadays, the scale of the project becomes bigger and bigger as the demand increases, and the increase in scale brings a lot of troubles, various businesses are intertwined in a complex way, between each business module, the code is not constrained, which brings the blurring of code boundaries, code conflicts occur from time to time, changing a small problem may cause some new problems, which affects the whole body, adding a new requirement, after being familiar with a lot of code written by the predecessors before they dare to do it, the compilation time is not increasing, the development efficiency is extremely reduced, in this case the emergence of componentization is to solve the above troubles

<a name="1.3"></a>

1.3 Analysis of existing componentization schemes

很多大厂 of组件化方案 be以 multi-engineering + Multi Module structure (WeChat, Meituan and other super App 更 be以 multi-engineering + Multi Module + 多 P 工程(以页面 because of单元 of code隔离方式) of the tertiary engineering structure), using Git Submodule 创建多个子仓库管理各个模块 of code, 并将各个模块 of code打包成 AAR Upload to private Maven 仓库 use远程版本号 depend on of方式进行模块间 code of隔离

<a name="1.4"></a>

1.4 How to choose a componentized solution?

按照康威定律, 系统架构 of设计需要根据组织间 of沟通结构, 因 because of现 (located) at大部分 sports event of规模 harmony开发人员 of数量以及结构还不足以需要某些大厂发布 of组件化方案支撑(大厂 of组织结构 harmony sports event规模都非常庞大, 他们 of方案不一定完全适合所有公司 of sports event), 进行更严格更细粒度 of code间以及模块间 of隔离, 盲目 of use某些组件化方案, 可能会带来开发效率降低, 开发成本远大于收益等情况, 性价 compare变低, 作 because of sports event负责人, 应该根据 sports event目前 of规模以及开发人员 of组织结构去选择目前最适合 of组件化方案, 做 up to以 sports event实际情况去制定技术方案, 而不 be盲目跟随某些大厂 of技术方案让 sports event harmony开发人员花费大量时间去调整 harmony适应

<a name="2"></a>

2 Description of the componentization scheme

ArmsComponent 目前采用 of be single project + Multi Module of结构, due to于 Demo 较小仅仅 because of了展示基本规范, 所以也只 be采用源码 depend on并没有做 up to远程版本号 depend on组件, code管理也只 be采用 single warehouse + multibranch of方式, 这样也 be对于开发初期, sports event规模还较小, 开发人员也较少时, 开发效率较高 of方案, 如果您 of sports event规模较大, 开发人员众多, 就可以采用上面提 up to of multi-engineering + Multi Module , and use private Maven Warehouse Management Component Version

世界上没有一个方案可以完美 up to兼顾所有情况, 并且还满足所有人, 所有 sports event of需求, 所以 sports event负责人必须按照 sports event实际情况做出灵活 of调整, 才能做出最适合自家 sports event of方案

<a name="2.1"></a>

2.1 Architecture diagram at a glance

ArmsComponent Componentized Architecture Diagram

<a name="2.2"></a>

2.2 Architecture Diagram Detail

目前架构一共分 because of三层, 从低 up to高依次 be base layer, business layer harmony host layer, due to于目前 sports event较小人员较少所以三层都集中 (located) at一个工程中, 但您可以根据 sports event of规模 harmony开发人员 of数量拆分成多个工程协同开发

<a name="2.2.1"></a>

2.2.1 Host layer

host layer位于最上层, 主要作用 be作 because of一个 App 壳, 将需要 of模块组装成一个完整 of App , this layer can manage the entire App of生命周期 (e.g. Application of初始化 harmony各种组件以及三方库 of初始化)

<a name="2.2.2"></a>

2.2.2 Operational layer

business layer位于中层, 里面 mainly根据业务需求 harmony应用场景拆分过后 of业务模块, 每个模块之间互不 depend on, 但又可以相互交互, compare如一个商城 App due to search, order, cart, pay (money) Business modules such as

Tips: 每个业务模块都可以拥有自己独有 of SDK depend on harmony自己独有 of UI 资源 (如果 be其他业务模块都可以 generic SDK depend on harmony UI 资源 就可以将它们抽离 up to Basic SDK(CommonSDK 2.2.3.3) harmony UI components (CommonRes 2.2.3.3.2) Medium)

<a name="2.2.2.1"></a>

2.2.2.1 of the business module拆分

写业务之前先不要急着动手敲码, 应该先根据初期 of产品需求 up to后期 of运营规划结合起来清晰 of梳理一 downwards业务 (located) at未来可能会发生 of发展, 确定业务之间 of边界 , with及可能会发生 of变化, 最后再确定 downwards来真正需要拆分出来 of业务模块再进行拆分

<a name="2.2.3"></a>

2.2.3 base layer

base layer位于最底层, 里面又包括 Core Foundation Operations模块 Public Service Module Basic SDK 模块, Core Foundation Operations模块 harmony Public Service Module Mainly for each module of the business layer, Basic SDK 模块 contains a variety of powerful team self-packaged SDK and third parties SDK , Powering the infrastructure of the platform as a whole

<a name="2.2.3.1"></a>

2.2.3.1 Core Foundation Operations

Core Foundation Operations because of business layer Each business module of the project provides some basic services related to the business, for example, in the project is divided into 2 ports with user roles, users can play multiple roles, but online can only operate the business of one port at the same time, then each port must provide a role switching function, so that the user can switch between multiple roles at any time, This is where it is necessary to provide an administrative class in the project for users to freely switch roles as Core Foundation Operations Dependent on these 2 ports (similar to Latchkey, Boss, etc.) App Can switch between recruiter and candidate)

Core Foundation Operations The division should be based on whether or not the underlying services are required by most of the modules in the business layer, as well as some services that need to be interacted with across business modules, can be divided into Core Foundation Operations

<a name="2.2.3.2"></a>

2.2.3.2 Public services

Public services be一个名 because of CommonService of Module, 主要 of作用 be用于 business layer Interaction between modules (custom methods and class calls), including custom Service interfaces, and custom classes that can be passed across modules

主要流程 be:

Operational modules for service delivery:

(located) at Public services (CommonService ) in the statement Service interface (with custom methods that need to be called), and then implement this in your own module Service 接口, 再 pass (a bill or inspection) ARouter API Expose implementation classes

Business modules that use the service :

pass (a bill or inspection) ARouter of API 拿 up to这个 Service interface (polymorphic holding, actual holding implementation class), that can be called Service custom methods declared in the interface, so that interaction between modules can be achieved

Custom classes passed across modules:

(located) at Public services After defining the custom classes that need to be passed across modules in (Service hit the target自定义方法 harmony EventBus hit the target事件实体类都可能需要用 up to自定义类), 就可以 pass (a bill or inspection) ARouter API , pass this custom object across modules between pages in each module (ARouter The requirement is that the URL 中 use Json Parameter passing custom objects must implement the SerializationService interface)

Tips: 建议 (located) at CommonService 中给每个需要提供服务 of业务模块都建立一个单独 of包, 然后 (located) at这个包 downwards放 Service 接口 harmony 需要跨模块传递 of自定义类, 这样更好管理

Mastering the usage of the public service layer is best done with an understanding of the ARouter API

Click for ARouter documentation

<a name="2.2.3.3"></a>

2.2.3.3 Basic SDK

Basic SDK be一个名 because of CommonSDK of Module , which contains a number of powerful SDK , provided to all modules in the overall architecture

<a name="2.2.3.3.1"></a> 2.2.3.3.1 MVPArms

MVPArms It is the most important module in the entire base layer, the heart of the component-based architecture, and provides a complete set of API harmony SDK, be整个 sports event of脚手架, 我用它来统一整个组件化方案 of基础设施, 使每一个模块更加健壮, Because of the MVPArms , making ArmsComponent It has become the only componentized solution that provides a complete base framework, so learn ArmsComponent You have to learn it before MVPArms

do MVPArms 时请按以 downwards排列顺序依次 do:

1. Click to learn Demo

2. Click for detailed documentation

3. Click to download One Click Code Generation Plugin

<a name="2.2.3.3.2"></a> 2.2.3.3.2 UI components

Basic SDK hit the target UI Components be一个名 because of CommonRes of Module, 主要放置一些 business layer可以 generic与 UI 有关 of资源供所有 business layer模块 use, 便于重用、管理 harmony规范已有 of资源

Tips: 值得注意 of be, business layer of某些模块如果出现有资源名命名相同 of情况 (如两个图片命名相同), 当 (located) at host layer集成所有模块时就会出现资源冲突 of问题, 这时注意 (located) at每个模块 of build.gradle 中 use resourcePrefix 标签给每个模块 downwards of资源名统一加上不同 of前缀即可解决此类问题

android {
    defaultConfig {
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        ...
    }
    resourcePrefix "public_"
}

The types of resources that can be placed are :

  • generic Style, Theme
  • generic Layout
  • generic Color, Dimen, String
  • generic Shape, Selector, Interpolator
  • generic Image Resources
  • generic Animation Resources
  • generic Customize View
  • Generic third party Customize View

<a name="2.2.3.3.3"></a> 2.2.3.3.3 Other SDKs

Other SDKs mainly Basic SDK Some of the business layers that depend on it can be generalized Third Party Library harmony Third Party SDK (e.g. ARouter, Tencent X5 Kernel ), to facilitate the reuse, management and standardization of existing SDK depend on

<a name="2.3"></a>

2.3 Cross-component communication

<a name="2.3.1"></a>

2.3.1 because of什么需要 Cross-component communication?

因 because of各个业务模块之间 be各自独立 of, 并不会存 (located) at相互 depend on of关系, 所以一个业务模块 be访问不了其他 of the business module code of, 如果想从 A of the business module A 页面跳转 up to B of the business module B 页面, 光靠模块自身 be不能实现 of, 所以这时必须依靠外界 of其他媒介提供这个 Cross-component communication of服务

<a name="2.3.2"></a>

2.3.2 Cross-component communication场景

There are two main scenarios for cross-component communication:

  • 第一种 be组件之间 of页面跳转 (Activity up to Activity, Fragment up to Fragment, Activity up to Fragment, Fragment up to Activity ) and data passing during jumps (base data types and serializable custom class types)
  • 第二种 be组件之间 of自定义类 harmony自定义方法 of调用(组件向外提供服务)

<a name="2.3.3"></a>

2.3.3 Cross-component communication方案

其实以上两种通信场景甚至其他更高阶 of功能 (located) at ARouter All have been implemented in , ARouter be Alibaba An open source Android 路 due to中间件, 可以满足很多组件化 of需求, 也 be作 because of本方案中 compare较重要 of一环, 需要认真看 downwards document, 了解 downwards基本 use

关于 Cross-component communication框架, 本方案不做封装, 专业 of事交给专业 of人做, 此类优秀框架 because of数众多, 各有特点, 可以根据您 of需求选择您喜欢 of框架, 不一定非得 be ARouter , Select ARouter 也 be因 because of Alibaba Product, open source for a long time, many users, relatively stable, problems are better to communicate

<a name="2.3.4"></a>

2.3.4 Analysis of cross-component communication schemes

第一种组件之间 of页面跳转不需要过多描述了, 算 be ARouter The most basic function in the API It is also relatively simple to jump to different types of data, and there are corresponding API (如果想 pass (a bill or inspection) (located) at URL 中 use Json Parameter passing custom objects, requires implementation of SerializationService , For more information, please refer to ARouter document);

The second type of custom class and custom method calls between components is a little more complicated, requiring ARouter 配合架构 hit the target Public services (CommonService) 实现, 主要流程 (located) at Public services (2.2.3.2) I've already described it in the following section, so I've drawn a diagram here to help you understand it better

Cross-component communication示意图

This type of service delivery is called Interface Down Please read along while looking at the pictures. Public services (2.2.3.2) hit the target主要流程便于理解

Also available in this program are EventBus 来作 because of服务提供 of另一种方式, 大家知道 EventBus Because of its decoupled nature, the project call hierarchy will be confusing if abused, which is not easy to maintain and debug, so this solution uses AndroidEventBus Its unique Tag, 可以 (located) at开发时更容易定位发送事件 harmony接受事件 of code, 如果以 Component Name来作 because of Tag prefixes, it is also possible to manage and view the events of each component in a uniform way, but it is not recommended to use too many EventBus

Tips: 每个 Cross-component communication框架提供服务 of方式都不同, 您也可以选择其他框架 of服务提供方式

<a name="2.3.5"></a>

2.3.5 Passing complex data formats across components

In general basic data types are sufficient for most cross-component data passing needs, but in some cases complex custom data types may need to be passed, and there are two ways to pass custom types provided in the scheme:

第一种 (located) at Public services (2.2.3.2) 中已提及, 就 be (located) at CommonService Define this custom class in

第二种方式也 compare较简单, 直接 pass (a bill or inspection)解析 Json The string can then be passed

<a name="2.4"></a>

2.4 Component life cycle

Each component (module) can be run independently during the test phase, where each component can specify its own Application , it is then easy for the component to manage its own lifecycle, for example, if it wants to add a onCreate 中初始化一些 code都可以轻松做 up to, 但 be当进入集成调试阶段, 组件自己 of Application What if each component needs to initialize its own unique code when it is no longer available and each component is dependent on the host's lifecycle?

<a name="2.4.1"></a>

2.4.1 Analysis of the problem

(located) at集成调试阶段, 宿主 depend on所有组件, 但 be每个组件却不能 depend on宿主, 意思 be每个组件根本不知道自己 of宿主 be谁, 当然也就不能 pass (a bill or inspection)访问 code of方式直接调用宿主 of方法, 从而 (located) at宿主 of生命周期里加入自己 of逻辑 code

If the initialization code of each module is copied directly into the host's lifecycle, this is too violent, not only the code coupling is not easy to extend, but also the code is very prone to conflicts, so it is not feasible to modify the host source code

So is there any way that each component can manage its own lifecycle alone during the integration debugging phase?

The solution is very simple, just let each component manage its own lifecycle independently at development time, and let each component's lifecycle merge with the host's lifecycle at runtime (without modifying or adding to the host code)

<a name="2.4.2"></a>

2.4.2 Analysis of feasible options

Trying to dynamically insert each component's code into the host's lifecycle without changing the host's code is a bit like AOP meaning

There are about three existing solutions:

  1. Each component manually registers its own lifecycle implementation class into this management class, and during integration debugging, the host registers its own lifecycle implementation class in its Application The corresponding lifecycle method can call all registered lifecycle implementation classes through the management class
  2. use AnnotationProcessor Parsing annotations automatically register all component lifecycle implementation classes during compilation by generating source code, which is then called by the host in the corresponding lifecycle methods
  3. use Javassist Dynamically modified at compile time class file, inserting the lifecycle logic for each component directly into the corresponding lifecycle method of the host

我最后还 be选择了第一种方法, 因 because of后面两种方法虽然 use简单, 还可以自动化 of完成所有操作, 非常炫酷, 但 be这两种方法技术实现复杂, (located) at不同 of Gradle Compatibility issues can also occur in the version that affects the overall project development schedule, is harder to maintain, and increases compilation time

The first method is simple and easy to understand and maintain, although it adds a few steps to the process. Gradle version, nor does it increase compilation time

<a name="2.4.3"></a>

2.4.3 Final implementation programme

第一种方案具体原理也没什么好说 of, compare较简单, 大概就 be (located) at base layer中定义有生命周期方法 (attachBaseContext(), onCreate() ...) of接口, 每个组件实现这个接口, 然后将实现类注册进 base layer of管理器, 宿主 pass (a bill or inspection)管理器 (located) at对应 of生命周期方法中调用所有 of接口实现类, 典型 of observer模式, 类似注册点击事件

(located) at MVPArms The class that implements this scheme in is called ConfigModule , each component can declare one or more ConfigModule 实现类, 内部实现较 because of复杂, 实现原理 be reflective + agent + observer, 这个类也 be整个 MVPArms 框架提供给开发者最重要 of类

It can give MVPArms 框架配置大量 of自定义参数, 包括 sports event中所有生命周期 of管理 (Application, Activity, Fragment), sports event中所有网络请求 of管理 (Retrofit, Okhttp, Glide ), which provides great extensibility to the framework, making it more flexible

<a name="3"></a>

3 Project explanation

ArmsComponent

Project Address : ArmsComponent

<a name="3.1"></a>

3.1 How do I make the components work independently?

In the project root directory of the gradle.properties Medium, Change isBuildModule The value of

#isBuildModule  because of true 时可以使每个组件独立运行, false 则可以将所有组件集成 up to宿主 App 中
isBuildModule=true

<a name="3.2"></a>

3.2 Configuring AndroidManifest

due to于组件 (located) at独立运行时 harmony集成 up to宿主时可能需要 AndroidManifest 配置不一样 of参数, compare如组件 (located) at独立运行时需要其 hit the target一个 Activity Configured <action android:name="android.intent.action.MAIN"/> 作 because of入口, 而当组件集成 up to宿主中时, 则 depend on于宿主 of入口, 所以不需要配置 <action android:name="android.intent.action.MAIN"/> , at which point we need two different AndroidManifest Responding to different situations

In the component's build.gradle 中加入以 downwards code, 即可指定不同 of AndroidManifest, 具体请看 sports event code

android {
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
            if (isBuildModule.toBoolean()) {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            } else {
                manifest.srcFile 'src/main/release/AndroidManifest.xml'
            }
        }
    }
}

<a name="3.3"></a>

3.3 Configuring ConfigModule(GlobalConfiguration)

ConfigModule (located) at Final implementation programme(2.4.3) 中提 up to过, GlobalConfiguration be实现类, 他可以给框架配置大量 of自定义参数

sports event CommonSDK There is a GlobalConfiguration 用于配置每个组件都会用 up to of公用配置信息, 但 be每个组件可能都需要有一些私有配置, compare如初始化一些特有属性 , so in每个组件中也需要实现 ConfigModule, 具体请看 sports event code

It is important to note that in Configuring AndroidManifest(3.2) 中提 up to过组件 (located) at独立运行时 harmony集成 up to宿主时所需要 of配置 be不一样 of, 当组件 (located) at独立运行时需要 (located) at AndroidManifest 中声明自己私有 of GlobalConfiguration harmony CommonSDK communal GlobalConfiguration , but when integrated into a host, since the host has declared CommonSDK publicly owned GlobalConfiguration , so in AndroidManifest Just declare your own private GlobalConfiguration , as also illustrated here AndroidManifest (located) at不同 of情况需要做出不同 of配置

<a name="3.4"></a>

3.4 RouterHub

RouterHub Used to define路 due to器 of路 due to地址 , with Component Name作 because of前缀, 对每个组件 of路 due to地址进行分组, 可以统一查看 harmony管理所有分组 of路 due to地址

RouterHub 存 (located) at于 base library, 可以被看作 be所有组件都需要遵守 of通讯协议, 里面不仅可以放路 due to地址常量, 还可以放跨组件传递数据时命名 of各种 Key values, with appropriate annotations, any component developer who relies on this protocol will know how to work together without prior communication, which increases efficiency and reduces the risk of error, and the agreement is naturally better than words

Tips: 如果您觉得把每个路 due to地址都写 (located) at base library of RouterHub 中, 太麻烦了, 也可以 (located) at每个组件内部建立一个私有 RouterHub, 将不需要跨组件 of路 due to地址放入私有 RouterHub 中管理, 只将需要跨组件 of路 due to地址放入 base library publicly owned RouterHub 中管理, 如果您不需要集中管理所有路 due to地址 of话, 这也 be compare较推荐 of一种方式

The naming convention for routing addresses is Component Name + page name, 如 order组件 of order详情页 of路 due to地址可以 named after "/order/OrderDetailActivity"

ARouter Set the first of the routing addresses '/' The character that follows is called Group , for example, in the example routing address above order 就 be Group , with order Addresses that begin with are assigned that Group downwards

Tips: 切记不同 of组件中不能出现名称一样 of Group, 否则会发生该 Group downwards of部分路 due to地址找不 up to of情况!!!

所以每个组件 use自己 of Component Name作 because of Group It's a better choice, after all the components won't be renamed

<a name="3.5"></a>

3.5 EventBusHub

AndroidEventBus As another way of cross-component communication provided by this solution (the first way of cross-component communication is Public services (2.2.3.2)), AndroidEventBus compare greenrobot of EventBus One more. Tag , easier to locate and manage events in componentization

EventBusHub Used to define AndroidEventBus of Tag string, with the component name as the Tag prefix, grouping events for each component

Tag The naming convention for Component Name + page name + movements, compare如需要 use AndroidEventBus Notify the order details page of the order component to be refreshed, you can set this refresh method's Tag named after "order/OrderDetailActivity/refresh"

Tips: base library hit the target EventBusHub 仅用来存放需要 Cross-component communication of事件 of Tag, 如果某个事件只想 (located) at组件内 use AndroidEventBus 进行通信, 那就让组件自行管理这个事件 of Tag

<a name="3.6"></a>

3.6 Using multiple different domains in a project

In the project, there are intimate knowledge Dry Goods Camp Rare Earth Nuggets Three modules, all three of which have different domain names for their web interfaces, can be used in the project using the same Retrofit To make a network request, how is this done? This is using another of my libraries RetrofitUrlManager , it can make Retrofit Multiple simultaneous support BaseUrl and dynamic changes BaseUrl


Hello My name is Jessyan, if you like my articles, you can follow me on the following platforms

-- The end


Recommended>>
1、Support Vector Machines Explained
2、Mind Mapping Software Comparison primary
3、Customizing the View Padding and Drawing Content
4、C read and write files
5、Tencent AILab Director Zhang Tong takes you to get new knowledge of AI easily

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号