头图
This article has been published on the WeChat public account "Code Xiaosheng" for the first time. You can search and follow and focus on Android technology sharing.

This article explains

The previous article has shared the routing configuration, jump, principle, complete effect demonstration gif and source code, and it is a multi-module project demonstration. It is an introduction to routing ARouter. If you haven't configured it yet, you can check it out first.

The content of this article mainly involves the following two:

  • Route interceptor use
  • module runs independently

The former has such an application scenario in our development. By default, users can browse part of the page without logging in. When clicking on part of the page, they need to log in first, that is, jump to the login page. The common practice is to do it one by one according to the needs. Click event, which is very troublesome. If you need to pass parameters or something when jumping to login, then the changes will be super big; and the interceptor function of routing ARouter solves this problem very well, and it also supports custom interceptors. It is very flexible to use.

The use scenario of the latter is suitable for large projects and multi-person development scenarios, so that each module can be responsible for independent debugging and operation, which is conducive to project management and code maintenance. This piece needs additional configuration under the premise of the previous article, this article will talk about it.

module runs independently

Let's take a look at the independent operation of the module, and then we will make a simulated jump page in each module to verify the login example, which is clearer.

Step 1: Configure gradle.properties

Add the following code in the gradle.properties

#是否需要单独运行某个模块 true:表示某个模块不作为依赖库使用
isSingleCircleModule=true
#isSingleCircleModule=false
isSingleHomeModule=true
#isSingleHomeModule=false

Step 2: Configure build.gradle

build.gradle file configuration under app

if (!isSingleCircleModule.toBoolean()) {
    implementation project(path: ':circle')
}
if (!isSingleHomeModule.toBoolean()){
    implementation project(path: ':home')
}

And comment out the original dependency

//    implementation project(path: ':circle')
//    implementation project(path: ':home')

Step 3: Configure build.gradle

The top changes of file circle under the module build.gradle

//plugins {
//    id 'com.android.library'
//}

if (isSingleCircleModule.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}

The top changes of the build.gradle home module are as follows:

//plugins {
//    id 'com.android.library'
//}

if (isSingleHomeModule.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}

Step 4: See the effect

After the above configuration is completed, click Sync Project with Gradle Files wait for the compilation to complete, you can see the following status:

Select Run弹窗

At this time, when we select one of the modules to run, we will find that the following error is reported:

Could not identify launch activity: Default Activity not found
Error while Launching activity

Obviously, we all know that the main entrance of the Android program is configured from the manifest file, but our modules have not yet done this work.

In circle module, the configuration is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gs.circle">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/app_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".CircleActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Among them, icon , label and theme can be defined in baselib , so that we can directly reference any module configuration without having to copy a copy of each; in addition, values folder can be moved to baselib This is baselib module. If you want to subdivide, you can also put the public resources in a separate module. This module is usually called: commonlib , depending on the specific situation.

After configuring the manifest file, after running, there will be an additional APP icon on the desktop, and only one page will be opened, which is the circle page of our 0617a1946259e6 module. home module will not be shown, let's see the effect:

module独立运行

At this time, app back and run the 0617a194625a34 module. If there is a problem, uninstall it and then run it. But there will be a problem. Originally, you can jump to the functions of other modules, but now you can’t jump. This is actually normal, because in the component development mode, each module is independent app , so it must not be jumped directly. .

How to realize normal jump?

Two steps are gradle.properties . Modify the code in 0617a194625a72 as follows:

#isSingleCircleModule=true
isSingleCircleModule=false
#isSingleHomeModule=true
isSingleHomeModule=false

Then application attribute and default startup configuration items in the manifest files of the circle and home modules, and then run it again.

If you want to use one of them as a dependent library, just set it to false.

About the AndroidManifest merge problem between components

In fact, you can comment out the relevant code in the module when it is officially packaged. After all, it is in the component mode. Is there a way to solve the problem of having to comment every time? The answer is yes.

The general idea is as follows:

Create a new folder (named custom) under the res->main folder of the independently runable module, and then copy the corresponding manifest file. The name does not need to be modified. The difference in content is mentioned above, remove the application attribute and default Start the configuration item. <br/>
Then specify the path of the form in the build.gradle of the corresponding module, the code is as follows:
sourceSets {
    main {
        if (isSingleCircleModule.toBoolean()) {
            manifest.srcFile 'src/main/module/AndroidManifest.xml'
        } else {
            manifest.srcFile 'src/main/AndroidManifest.xml'
        }
    }
}

In this way, different AndroidManifest.xml will be read in different development modes, and then we need to modify the content of these two forms to serve our different development modes.

Single module independent operation summary

advantage:

  • Project coupling is low, development efficiency is high, and problems are easy to troubleshoot
  • Conducive to project schedule management, clear division of labor
  • Suitable for multi-person big projects

shortcoming:

  • The early configuration is more complicated, and some configurations need to be modified during the development process
  • Stability is not easy to grasp, after all, it is not the official framework of Google, and it is difficult to deal with problems later

In fact, there are still many problems. Those who have practiced should understand that each project has its own uniqueness and there will be various strange problems, but generally we can find solutions on the Internet.

Route interceptor use

First, you still need to add a few configurations. Add the following line of code build.gradle

classpath 'com.alibaba:arouter-register:1.0.2'

Under the build.gradle app module, the configuration changes are as follows:

plugins {
    id 'com.android.application'
    id 'com.alibaba.arouter' // 拦截器必须配置
}

After configuring these two steps, by convention, it is time to compile.

To demonstrate, here I create a new app LoginActivity , the page that jumps after job login interception, the page content only has a prompt text, here is the subsidy code.

Then jump to the login page in the host module app , function modules circle and home to see if our interceptor plays a role in interception, and start to define the interceptor below.

If you want to run a module independently, I won't repeat it here, you can modify the configuration by yourself.

The complete code of the interceptor is as follows:

/**
 * Description: 登录拦截器
 * Date: 2021/10/9 10:42
 * <p>
 * 拦截器会在跳转之间执行,多个拦截器会按优先级顺序依次执行
 * * <p>
 * * priority 数值越小权限越高
 */
@Interceptor(priority = 2, name = "登录ARouter拦截器")
public class LoginInterceptor implements IInterceptor {

    private Context mContext;

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        boolean isLogin = mContext.getSharedPreferences("arouterdata", mContext.MODE_PRIVATE).getBoolean("isLogin", false);
        if (isLogin) {
            callback.onContinue(postcard);
        } else {
            switch (postcard.getPath()) {
                // 需要登录的拦截下来
                case ARouterPath.APP_MY_INFO:
                    ARouter.getInstance().build(ARouterPath.LOGIN_PAGE).with(postcard.getExtras()).navigation();
                    break;
                default:
                    callback.onContinue(postcard);
                    break;
            }
        }
    }

    /**
     * 拦截器的初始化,会在sdk初始化的时候调用该方法,仅会调用一次
     *
     * @param context
     */
    @Override
    public void init(Context context) {
        mContext = context;
    }

}

The interceptor initialization needs to be reinstalled to take effect. This should be noted. The interceptor does not require us to display the call manually, but the framework uses annotations, so we only need to write the logic code.

The above code can realize the interception of intra-module and cross-module jumps. I have no processing logic for the local login status, so it will be intercepted every time. Let's see the effect:

拦截器效果

Demonstration effect simulation MyInfoActivity page, and jump demos are made from the three modules.

Summarize

The independent operation and merging of componentized modules are relatively cumbersome, but the advantages are also obvious. The interceptor of the routing framework ARouter is very simple to use. In fact, the interceptor can be used directly after completing the previous article. If the componentized multi-module independent operation of the actual project cannot be used, you can skip it first and simply understand the process. Can.

The framework of Android has also evolved rapidly. The "three modernizations" technology was particularly popular two years ago, and almost everyone was discussing it, but it did not last long before it was replaced by new technologies. As a developer, you need Master a basic skill: build a project framework from scratch, and this framework should keep up with the continuous development of the project as much as possible .

All the code in this article has been packaged: follow the WeChat public code Xiaosheng reply arouter

code小生
165 声望18 粉丝