Mobile Architecture 05 - Componentization and Arouter Routing Framework

Mobile Architecture 05 - Componentization and Arouter

Componentization refers to allowing modules to run independently and freely call classes from other modules.

There are usually many modules in a large-scale project, and these modules are usually in the form of a library, so the entire project must be run when debugging, which is very inconvenient. Componentization is to allow modules to freely switch between library and application to facilitate debugging. In addition, the two modules cannot reference each other, so they cannot call each other. Componentization allows them to call each other through the routing function.

1. Independent operation

Let the module run independently is to let the module switch library and application freely, which needs to be configured in gradle.

//根据isModule(定义在rootProject中)的值设置当前模式:集成或组件
if (isModule) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}

android {
    defaultConfig {
        ...
        //添加一条 boolean类型的变量,用来指定当前模式:集成或组件.通过BuildConfig.isModule获取
        buildConfigField("boolean", "isModule", String.valueOf(isModule))

        //组件模式下,指定applicationId
        if (isModule) {
            applicationId appId['module1']
        }

        //资源配置
        sourceSets {
            main {
                //指定manifest文件和java文件
                if (isModule) {
                    manifest.srcFile 'src/main/module/AndroidManifest.xml'
                    java.srcDirs 'src/main/module/java', 'src/main/java'
                } else {
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                }
            }
        }
    }
}

In general, it is through a switch isModule to modify the configuration of gradle, switch modes and resources.

2. Arouter routing framework

The routing function uses Ali's Arouter .

When configuring Arouter, you need to configure both the application module (eg: DemoComponent1) and the library module it depends on (eg: DemoComponent2).

1. Add dependencies and configuration

Add the following configuration to the build.gradle of the application module and library module:

android {
    defaultConfig {
    ...
    //设置moduleName,不添加会报错
    javaCompileOptions {
        annotationProcessorOptions {
        arguments = [ moduleName : project.getName() ]
        }
    }
    }
}

dependencies {
    // 替换成最新版本, 需要注意的是api
    // 要与compiler匹配使用,均使用最新版可以保证兼容
    compile 'com.alibaba:arouter-api:x.x.x'
    annotationProcessor 'com.alibaba:arouter-compiler:x.x.x'
    ...
}

2. Initialize SDK

Initialize ARouter in Application of application module

if (isDebug()) {           // 这两行必须写在init之前,否则这些配置在init过程中将无效
    ARouter.openLog();     // 打印日志
    ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
}
ARouter.init(mApplication); // 尽可能早,推荐在Application中初始化

3. Add annotations

Add the Route annotation to the Activity that needs routing in the application module and the library module. The annotation must be set to two levels, because the initialization of the routing module is carried out in groups.

// 在支持路由的页面上添加注解(必选)
// 这里的路径需要注意的是至少需要有两级,/xx/xx
@Route(path = "/demo1/activityArouterDemo1")
public class ActivityArouterDemo1 extends AppCompatActivity {
...
}

4. Initiate a routing operation

// 1. 应用内简单的跳转(通过URL跳转在'进阶用法'中)
ARouter.getInstance().build("/test/activity").navigation();

// 2. 跳转并携带参数
ARouter.getInstance().build("/test/1")
            .withLong("key1", 666L)
            .withString("key3", "888")
            .withObject("key4", new Test("Jack", "Rose"))
            .navigation();

5. Add obfuscation rules

-keep public class com.alibaba.android.arouter.routes.**{*;}
-keep class * implements com.alibaba.android.arouter.facade.template.ISyringe{*;}

# 如果使用了 byType 的方式获取 Service,需添加下面规则,保护接口
-keep interface * implements com.alibaba.android.arouter.facade.template.IProvider

# 如果使用了 单类注入,即不定义接口实现 IProvider,需添加下面规则,保护实现
-keep class * implements com.alibaba.android.arouter.facade.template.IProvider

6. Use the Gradle plugin to automatically load the routing table

Add the arouter-register plugin in the root project's build.gradle

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath "com.alibaba:arouter-register:1.0.0"
    }
}

Apply arouter-register plugin in build.gradle of application module and library module

apply plugin: 'com.alibaba.arouter'

Optionally , the routing table is automatically loaded through the registration plug-in provided by ARouter. By default, it is loaded by scanning dex. Automatic registration (registration at compile time) through the gradle plug-in can shorten the initialization time and solve the problem that the dex file cannot be directly accessed due to application reinforcement, and the initialization fails. It should be noted that this plugin must be used with api 1.3.0!

The code has been uploaded to gitee, and students who like it can download it. By the way, give a like!

Previous: Mobile Architecture 04-In-depth Analysis of Dynamic Skinning Framework

Next: Mobile Architecture 06-Teach you to write Arouter routing framework by hand

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525794&siteId=291194637