This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
This article has launched the first wechat public account “Code Xiaosheng”, you can search for attention, focus on Android technology sharing.
This article suggests that
The last article has shared the route configuration, jump, principle, complete effect demo GIF and source code, and is a multi-module project demo, is a route ARouter entry, has not configured the use of you can go to see.
The content of this paper mainly involves the following two aspects:
- Route interceptor used
- Module runs independently
The former there is a application scenarios in our development, part of the default user can not login to browse pages, when click on the part of the page will need to login first, also is to jump to the login page, common practice is according to the requirements in turn to do the click event, it is very troublesome, if you need to jump when login pass parameters, that is super big changes; Routing ARouter’s interceptor function solves this problem well, and also supports custom interceptors, which are very flexible to use.
The use of the latter scenario is suitable for large project, multi-person development scenario, so that they can be responsible for a module, independent debugging operation, conducive to project management and code maintenance. This requires additional configuration from the previous article, which is covered in this article.
Module runs independently
Let’s take a look at the Module running independently, and then we’ll do a simulated jump page in each module that needs to verify the login example, so that it’s clearer.
Step 1: Configure gradle.properties
Add the following code to the gradle.properties file
# Whether a module needs to be run separatelytrue: indicates that a module does not use isSingleCircleModule= as a dependent librarytrue
#isSingleCircleModule=false
isSingleHomeModule=true
#isSingleHomeModule=false
Copy the code
Step 2: Configure the appbuild.gradle
Gradle file configuration in app
if(! isSingleCircleModule.toBoolean()) { implementationproject(path: ':circle')}if(! isSingleHomeModule.toBoolean()){ implementationproject(path: ':home')}Copy the code
And comment out the original dependencies
// implementation project(path: ':circle')
// implementation project(path: ':home')
Copy the code
Step 3: Configure for each independent modulebuild.gradle
Make the following changes at the top of the build.gradle file in the circle module:
//plugins {
// id 'com.android.library'
/ /}
if (isSingleCircleModule.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
Copy the code
Under the home module, the top of the build.gradle file changes as follows:
//plugins {
// id 'com.android.library'
/ /}
if (isSingleHomeModule.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
Copy the code
Step 4: Look at the results
After the above configuration is complete, click Sync Project with Gradle Files and wait for the compilation to complete. You can see the following state:
At this time, we select one of the modules to run, we will find the following error:
Could not identify launch activity: Default Activity not found
Error while Launching activity
Copy the code
Obviously, we all know that the main entry to an Android application is configured from a manifest file, but none of our modules has done that yet.
In the manifest file under the Circle module, the configuration is as follows:
<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>
Copy the code
Icon, label, and theme can all be defined in baselib, so that any module configuration can be directly referenced without having to copy each module. In addition, everything in the values folder can be moved to Baselib for reference by other modules. This is what baselib is for. If you want to subdivide, you can also put common resources in a separate module, usually called CommonLib, depending on the case.
After configuring the manifest file, an APP icon will appear on the desktop after running, and only one page will be opened, which is the main page of our CircleModule. The list configuration of the home module is not shown. Here is the effect:
At this time, cut back to run the APP module. If there is a problem, uninstall it and run it again. However, there is a problem that the function of jumping to other modules can not be jumped now, which is quite normal, because in the componential development mode, each module is an independent APP, so it cannot be directly jumped to.
So how do you do that?
In two steps, change the code in gradle.properties to the following:
#isSingleCircleModule=true
isSingleCircleModule=false
#isSingleHomeModule=true
isSingleHomeModule=false
Copy the code
Then delete the Application property and default startup configuration items from the manifest files of the Circle and Home modules and run again.
If you want to use one of these libraries as a dependent library, set it to false.
AndroidManifest merging issues between components
In fact, this can be in formal packaging, comment out the relevant code in the Module, after all, in component mode. Is there a way to get around the problem of commenting every time? The answer is yes.
The general idea is as follows:
In the res->main folder of the module that can run independently, create a new folder (custom name) and make a copy of the corresponding manifest file. The name does not need to be changed, but the content difference is mentioned above. Remove the Application property and default startup configuration items.
Then specify the path to the form in build.gradle of the corresponding module as follows:
sourceSets {
main {
if (isSingleCircleModule.toBoolean()) {
manifest.srcFile 'src/main/module/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/AndroidManifest.xml'}}}Copy the code
This will read different Androidmanifest.xml in different development modes, and then we need to modify the contents of the two forms to serve our different development modes.
Single module independent operation summary
Advantages:
- The coupling degree of the project is low, the development efficiency is high, and the problems are easy to troubleshoot
- Conducive to project schedule management, clear division of labor
- Suitable for many projects
Disadvantages:
- The initial configuration is complex, and some configurations need to be modified during development
- Stability is not easy to grasp, after all, it is not the official framework of Google, and it is not easy to deal with problems in the late period
In fact, there are many problems, practice should understand, every project has its own unique, there will be all kinds of strange problems, but generally we can find solutions online.
Route interceptor used
First, we need to add a few configurations. Add the following line to the build.gradle file under the project:
classpath 'com. Alibaba: arouter - register: 1.0.2'
Copy the code
In the build.gradle file of the app module, the configuration changes are as follows:
plugins {
id 'com.android.application'
id 'com.alibaba.arouter' // Interceptors must be configured
}
Copy the code
After configuring these two steps, it’s time to compile, by convention.
To demonstrate, I will create a new page named LoginActivity under the APP. The page content only has a prompt text after the job login interception. Here is the subsidy code.
Then jump to the login page in the host module APP, function module Circle and home respectively to see whether our interceptor plays a blocking role, and then start to define the interceptor.
To run a module on its own, I won’t go into details here; you can modify the configuration yourself.
The complete interceptor code is as follows:
/** * Description: Login interceptor * Date: 2021/10/9 10:42 * * Interceptors are executed between jumps, and multiple interceptors are executed in order of priority **
** priority THE lower the value, the higher the permission */
@interceptor (priority = 2, name = "login ARouter Interceptor ")
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()) {
// The required login is intercepted
case ARouterPath.APP_MY_INFO:
ARouter.getInstance().build(ARouterPath.LOGIN_PAGE).with(postcard.getExtras()).navigation();
break;
default:
callback.onContinue(postcard);
break; }}}/** * Interceptor initialization, this method is called when the SDK initializes, only once **@param context
*/
@Override
public void init(Context context) { mContext = context; }}Copy the code
Note that interceptor initialization does not take effect until it is reinstalled. Interceptors do not require us to display the calls manually, but are used by the framework through annotations, so we just need to write the logic.
The above code can be implemented within the module and across the module jump interception, local login state I have no processing logic here, so every time will be intercepted. Here is the result:
To enter the MyInfoActivity page, you need to log in first, and jump from three modules to demonstrate.
conclusion
Componentized modules are a bit more cumbersome to run independently and merge, but the advantages are obvious. ARouter’s interceptor is very simple to use. In fact, interceptors can be used directly after learning the last article. If the component-based multi-module independent operation of the actual project can not be used, you can skip it first and simply understand the process.
Android framework evolved quickly, “three” technology in the special fire two years ago, almost everyone in the discussion, but did not last long was replaced by the new technology, and as a developer, you need to master basic skills: a framework built from scratch from a project, and the sustainable development of this framework as far as possible to keep up with the project.
This article all code has been package: follow wechat public account code xiao Sheng reply to arouter