I. Basic introduction

1. Take this opportunity to share some of my personal experiences in developing architecture from the beginning to the present. I will eventually open source this code and maintain it in the future. However, it is currently in beta so no official version has been released. We can download the code to understand, if there are any problems can be Issues at any time, this will be my first open source library, I hope to help you.

2. The library involves about 30 classes, source code is not much I believe we can read the contents of the inside, here lists the source code involved in some knowledge points: (2) Thread, thread pool, thread synchronization async, and Handler (3) Responsibility chain mode, share mode, policy mode, template mode…

3. As a multi-module routing and communication library, it is believed that it has supported all the use scenarios of cross-module communication, and its functions are described as follows: (1) Support dependency injection, which can be used as a dependency injection framework alone (2) support thread switching and scheduling (original thread, main thread, synchronous, asynchronous) (3) support all cross-module communication scenarios under the multi-module project (4) support to add multiple interceptors, You can customize the interception sequence based on the priority. (5) Supports permissions and network detection, login interception jump, and data burying point

4. I have read a lot of source code of open source library, and all the code ideas of this library come from them. I am very grateful to these great people for their open source and sharing spirit: (1) ARouter (2) butterknife (3) okhttp (4) EventBus (5) RxJava (6) retrofit

Ii. Evolution of the framework

First of all, I would like to share with you some of my inner journey in developing architecture from the beginning to the present. From the beginning of the program, there is no turning back (life is a turning back). It has been several years. When I first started working in a small company, IT looked like this:

At that time, all classes were written under the same package, all activities were inherited from android.app.Activity under the system, and the network framework was directly used by Android-Async-HTTP, so overtime was quite serious. On the one hand, I did not have enough ability and experience. The other is that without architecture a lot of code is redundant over and over again. After graduation, I went to a listed company and made some changes:

This has been improved, and all activities are no longer directly inherited from Android.app. Activity, and no longer directly use Android-async-HTTP, so development life is naturally easier. On the one hand, problems can be solved immediately when they come up with experience and ability. On the other hand, many codes based on architecture need not be repeatedly written and can be easily maintained in the later stage. The most important thing is that there are more people in large companies. As the number of developers increased, and there were basically festive events, after which the code and resources had to be removed, and a new version had to be iterated every two months, it became more and more complicated. Later, there are many derivatives such as hot updates, plug-ins, multi-module multi-component development and so on. Of course, the initial multi-module multi-component was not based on routing, it was a spider web state.

Can it be this messy? As the business logic becomes more complex, it should be more confusing because the functional modules exist in their own modules, but Module1 may have to call Module2’s code during development, which would certainly be confusing if dependencies were added directly. Let’s take a look at multi-module, multi-component development based on routing:

Three. DRouter basic use

  1. Add dependencies and configurations to modules that need to communicate across modules
    defaultConfig {
        ......
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [moduleName: project.getName()]
            }
        }
    }

    dependencies {
        .......
        annotationProcessor project(':drouter-compiler')}Copy the code
  1. Create the actions you want to execute in the Module
// Path must be moduleName + configured in Gradle"/"Start, otherwise the compilation fails. // threadMode supports POSTING, MAIN, BACKGROUND, and ASYNC. By default, POSTING is @action (path =)"login/action". threadMode = ThreadMode.MAIN) public class LoginAction implements IRouterAction { @Override public RouterResult InvokeAction (Context Context, Map<String, Object> requestData) {// invokeAction(Context Context, Map<String, Object> requestData) { Intent Intent = new Intent(context, loginactivity.class); intent.putExtra("key", (String) requestData.get("key"));
        context.startActivity(intent);
        returnnew RouterResult.Builder().success().object(100).build(); }}Copy the code
  1. To initialize the SDK
public class BaseApplication extends Application{
    @Override
    public void onCreate() { super.onCreate(); // Enable debug drouter.openDebug (); Application drouter.getInstance ().init(this); }}Copy the code
  1. Jumps can be performed in any Module
Drouter.getinstance ().action();"login/action")
                .context(this)
                .param("key"."value") .invokeAction(); // Execute the corresponding method according to the action query and handle the return callback drouter.getInstance ().action("circlemodule/test")
                .context(this)
                .invokeAction(new ActionCallback() {
                    @Override
                    public void onInterrupt() {
                        Log.e("TAG"."Intercepted."); } @override public void onResult(RouterResult result) {} @override public void onResult(RouterResult result) { Log.e() is on the same thread as the Action."TAG"."result = "+ result.toString()); }});Copy the code
  1. Intercepts can be added under any module
// The higher the priority is, @interceptor (priority = 18) public class CircleInterceptor implements ActionInterceptor {@override public void  intercept(ActionChain chain) { ActionPost actionPost = chain.action(); // The circle details page must be a login page. If there is no login, the jump to the login page can be intercepted, otherwise proceed.if (chain.actionPath().equals("circlemodule/test")) {
            Toast.makeText(actionPost.context, "Block circles, jump to login.", Toast.LENGTH_LONG).show(); Drouter.getinstance ().action()"login/action") .context(actionPost.context) .invokeAction(); // This method calls and intercepts the entire chain chain.oninterrupt (); } // proceed with chain.proceed(actionPost); }}Copy the code

6. Confuse the configuration

-keep public class com.drouter.assist.**{*; }Copy the code

Source address: https://github.com/HCDarren/DRouter

Video tutorial: https://pan.baidu.com/s/1kWoIA95