Using ARouter Interceptors and Enabling Independent Module Execution in Android Projects
This article explains how to configure Gradle and AndroidManifest files to run individual modules independently in a multi‑module Android project and demonstrates the implementation of ARouter login interceptors to control navigation across modules.
Introduction
The previous article covered basic ARouter routing configuration, navigation, principles, and a complete demo; this continuation focuses on two topics: using ARouter interceptors and running modules independently.
Independent Module Execution
Step 1: Configure gradle.properties
# Whether a module should run independently
isSingleCircleModule=true
#isSingleCircleModule=false
isSingleHomeModule=true
#isSingleHomeModule=falseStep 2: Modify the app module's build.gradle
if (!isSingleCircleModule.toBoolean()) {
implementation project(path: ':circle')
}
if (!isSingleHomeModule.toBoolean()) {
implementation project(path: ':home')
}Comment out the original dependencies:
// implementation project(path: ':circle')
// implementation project(path: ':home')Step 3: Adjust each module's build.gradle
For the circle module:
if (isSingleCircleModule.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}For the home module:
if (isSingleHomeModule.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}After syncing Gradle, run a selected module; you may encounter a "Could not identify launch activity" error because each module lacks a proper AndroidManifest.xml entry.
Add a manifest for the circle module with a launcher activity:
Similar adjustments apply to the home module. After these changes, each module can be launched independently, though cross‑module navigation will stop because each module now runs as a separate application.
To restore navigation, set isSingleCircleModule and isSingleHomeModule back to false in gradle.properties and remove the application attributes from the module manifests.
Using ARouter Interceptors
Add the ARouter register classpath in the project‑level build.gradle :
classpath 'com.alibaba:arouter-register:1.0.2'In the app module's build.gradle , apply the ARouter plugin:
plugins {
id 'com.android.application'
id 'com.alibaba.arouter' // interceptor must be configured
}Create a LoginActivity as the destination for intercepted navigation. Then implement a login interceptor:
/**
* Description: Login interceptor
*/
@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;
}
}
}
@Override
public void init(Context context) {
mContext = context;
}
}After rebuilding the project, the interceptor will automatically block navigation to pages that require login and redirect to LoginActivity . Demonstrations show the interceptor working across the host app, circle , and home modules.
Conclusion
Running modules independently in a componentized Android project adds configuration complexity but brings lower coupling, clearer responsibility division, and easier debugging for large teams. ARouter's interceptor mechanism provides a straightforward way to enforce login checks across modules. Developers should understand both techniques to build flexible, maintainable mobile architectures.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.