Repository: HCDarren/DRouter Branch: master Commit: a2b02ee10977 Files: 106 Total size: 117.8 KB Directory structure: gitextract_w0jpccmc/ ├── .gitignore ├── .idea/ │ ├── gradle.xml │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations.xml │ └── vcs.xml ├── README.md ├── app/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── darren/ │ │ └── drouter/ │ │ └── ExampleInstrumentedTest.kt │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── darren/ │ │ │ └── drouter/ │ │ │ ├── BaseApplication.java │ │ │ └── MainActivity.java │ │ └── res/ │ │ ├── drawable/ │ │ │ └── ic_launcher_background.xml │ │ ├── drawable-v24/ │ │ │ └── ic_launcher_foreground.xml │ │ ├── layout/ │ │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26/ │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ └── values/ │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test/ │ └── java/ │ └── com/ │ └── darren/ │ └── drouter/ │ └── ExampleUnitTest.kt ├── base-core/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── base/ │ │ └── core/ │ │ └── ExampleInstrumentedTest.java │ ├── main/ │ │ ├── AndroidManifest.xml │ │ └── res/ │ │ └── values/ │ │ └── strings.xml │ └── test/ │ └── java/ │ └── com/ │ └── base/ │ └── core/ │ └── ExampleUnitTest.java ├── build.gradle ├── circle-module/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── example/ │ │ └── circle_module/ │ │ └── ExampleInstrumentedTest.java │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── example/ │ │ │ └── circle_module/ │ │ │ ├── CircleAction.java │ │ │ ├── CircleActivity.java │ │ │ ├── CircleInterceptor.java │ │ │ ├── CircleInterceptor1.java │ │ │ └── CircleInterceptor2.java │ │ └── res/ │ │ ├── layout/ │ │ │ └── activity_circle.xml │ │ └── values/ │ │ └── strings.xml │ └── test/ │ └── java/ │ └── com/ │ └── example/ │ └── circle_module/ │ └── ExampleUnitTest.java ├── drouter-api/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── drouter/ │ │ └── api/ │ │ └── ExampleInstrumentedTest.java │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── drouter/ │ │ │ └── api/ │ │ │ ├── action/ │ │ │ │ ├── IRouterAction.java │ │ │ │ ├── IRouterInterceptor.java │ │ │ │ └── IRouterModule.java │ │ │ ├── core/ │ │ │ │ ├── DRouter.java │ │ │ │ └── RouterForward.java │ │ │ ├── exception/ │ │ │ │ ├── InitException.java │ │ │ │ ├── RouterActionException.java │ │ │ │ └── RouterModuleException.java │ │ │ ├── extra/ │ │ │ │ ├── ActionWrapper.java │ │ │ │ ├── Consts.java │ │ │ │ ├── DefaultLogger.java │ │ │ │ ├── ErrorActionWrapper.java │ │ │ │ └── ILogger.java │ │ │ ├── interceptor/ │ │ │ │ ├── ActionInterceptor.java │ │ │ │ ├── ActionInterceptorChain.java │ │ │ │ ├── CallActionInterceptor.java │ │ │ │ └── ErrorActionInterceptor.java │ │ │ ├── result/ │ │ │ │ ├── ActionCallback.java │ │ │ │ └── RouterResult.java │ │ │ ├── thread/ │ │ │ │ ├── ActionPost.java │ │ │ │ ├── ActionPostQueue.java │ │ │ │ ├── AsyncPoster.java │ │ │ │ ├── BackgroundPoster.java │ │ │ │ ├── HandlerPoster.java │ │ │ │ ├── Poster.java │ │ │ │ └── PosterSupport.java │ │ │ └── utils/ │ │ │ ├── ClassUtils.java │ │ │ └── MapUtils.java │ │ └── res/ │ │ └── values/ │ │ └── strings.xml │ └── test/ │ └── java/ │ └── com/ │ └── drouter/ │ └── api/ │ └── ExampleUnitTest.java ├── drouter-base/ │ ├── .gitignore │ ├── build.gradle │ └── src/ │ └── main/ │ └── java/ │ └── com/ │ └── drouter/ │ └── base/ │ ├── ThreadMode.java │ └── annotation/ │ ├── Action.java │ └── Interceptor.java ├── drouter-compiler/ │ ├── .gitignore │ ├── build.gradle │ └── src/ │ └── main/ │ └── java/ │ └── com/ │ └── drouter/ │ └── compiler/ │ ├── Consts.java │ ├── InterceptorProcessor.java │ ├── ModuleProcessor.java │ └── util/ │ └── TextUtils.java ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat ├── login-module/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── login/ │ │ └── module/ │ │ └── ExampleInstrumentedTest.java │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── login/ │ │ │ └── module/ │ │ │ ├── LoginAction.java │ │ │ └── LoginActivity.java │ │ └── res/ │ │ ├── layout/ │ │ │ └── activity_login.xml │ │ └── values/ │ │ └── strings.xml │ └── test/ │ └── java/ │ └── com/ │ └── login/ │ └── module/ │ └── ExampleUnitTest.java └── settings.gradle ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *.iml .gradle /local.properties /.idea/workspace.xml /.idea/libraries .DS_Store /build /captures .externalNativeBuild ================================================ FILE: .idea/gradle.xml ================================================ ================================================ FILE: .idea/misc.xml ================================================ 1.8 ================================================ FILE: .idea/modules.xml ================================================ ================================================ FILE: .idea/runConfigurations.xml ================================================ ================================================ FILE: .idea/vcs.xml ================================================ ================================================ FILE: README.md ================================================ # Android 平台多模块多组件开发的路由库 ### **一. DRouter 基本介绍** **1.** 该库所涉及到的类大概在 30 个左右,源码并不多相信我们都能读懂里面的内容,这里罗列一下源码中所涉及到的一些知识点:   (1) 编译时注解自动生成 Module、Action 和 Intercepter   (2) 线程、线程池、线程同步异步和 Handler   (3) 责任链模式、享元模式、策略模式、模板模式 ... **2.** 作为一个多模块的路由通信库,相信它已支持了所有跨模块通信的使用场景,功能介绍如下:   (1) 支持依赖注入,可单独作为依赖注入框架使用   (2) 支持线程切换和调度(原始线程,主线程,同步,异步)   (3) 支持多模块工程下的所有跨模块通信使用场景   (4) 支持添加多个拦截器,可根据优先级自定义拦截顺序   (5) 支持权限和网络检测、登录拦截跳转和数据埋点等功能 **3.** 笔者阅读了大量的开源库源码,本库的所有代码思想都来自其中,很感激这些大牛的开源和分享精神:   [(1) ARouter](https://github.com/alibaba/ARouter)   [(2) butterknife](https://github.com/JakeWharton/butterknife)   [(3) okhttp](https://github.com/square/okhttp)   [(4) EventBus](https://github.com/greenrobot/EventBus)   [(5) RxJava](https://github.com/ReactiveX/RxJava)   [(6) retrofit](https://github.com/square/retrofit) ### **二. DRouter 基本使用** 1. 在需要跨模块通信的Module中添加依赖和配置 ``` defaultConfig { ...... javaCompileOptions { annotationProcessorOptions { arguments = [moduleName: project.getName()] } } } dependencies { ....... annotationProcessor project(':drouter-compiler') } ``` 2. 初始化 SDK ``` public class BaseApplication extends Application{ @Override public void onCreate() { super.onCreate(); // 开启 debug DRouter.openDebug(); // 初始化且只能初始化一次,参数必须是 Application DRouter.getInstance().init(this); } } ``` 3. 在 Module 中创建需要执行的 Action ``` // path 必须是以在 gradle 中配置的 moduleName + "/" 开头,否则编译通不过。 // threadMode 支持 POSTING 、MAIN、BACKGROUND、ASYNC 默认情况下是 POSTING(原始线程) @Action(path = "login/action", threadMode = ThreadMode.MAIN) public class LoginAction implements IRouterAction { @Override public RouterResult invokeAction(Context context, Map requestData) { // 通信执行方法支持所有场景,启动 Activity,Service,Provider,弹框,缓存数据,获取 Fragment 等等等等 Intent intent = new Intent(context, LoginActivity.class); intent.putExtra("key", (String) requestData.get("key")); context.startActivity(intent); return new RouterResult.Builder().success().object(100).build(); } } ``` 4. 可在任意 Module 中执行跳转 ``` // 根据 action 查询只执行对应方法,不处理返回回调,参数携带随意 DRouter.getInstance() .action("login/action") .context(this) .param("key", "value") .invokeAction(); // 根据 action 查询执行对应方法,并处理返回回调 DRouter.getInstance() .action("circlemodule/test") .context(this) .invokeAction(new ActionCallback() { @Override public void onInterrupt() { Log.e("TAG", "被拦截了"); } @Override public void onResult(RouterResult result) { // 注意该方法的执行线程是由 Action 的 threadMode 决定的,也就是说和 Action 在同一个线程 Log.e("TAG", "result = " + result.toString()); } }); ``` 5. 在任意模块下都可添加拦截 ``` // priority 优先级越高,拦截器执行越优先 @Interceptor(priority = 18) public class CircleInterceptor implements ActionInterceptor { @Override public void intercept(ActionChain chain) { ActionPost actionPost = chain.action(); // 圈子详情页必须是要登录,如果没有登录即可拦截跳转到登录页面,否则继续往下执行。 if (chain.actionPath().equals("circlemodule/test")) { Toast.makeText(actionPost.context, "拦截圈子,跳转到登录", Toast.LENGTH_LONG).show(); // 跳转到登录页面 DRouter.getInstance() .action("login/action") .context(actionPost.context) .invokeAction(); // 这个方法调用后便会拦截整条链 chain.onInterrupt(); } // 继续向下转发 chain.proceed(actionPost); } } ``` 6.混淆配置 ``` -keep public class com.drouter.assist.**{*;} ``` ### **三. 其他** 1. 简书详细介绍地址:https://www.jianshu.com/p/d0e1320704e4 2. 视频详细讲解地址:https://pan.baidu.com/s/1kWoIA95 ================================================ FILE: app/.gitignore ================================================ /build ================================================ FILE: app/build.gradle ================================================ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 defaultConfig { applicationId "com.darren.drouter" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" javaCompileOptions { annotationProcessorOptions { arguments = [ moduleName : project.getName() ] } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' /*compile 'com.alibaba:arouter-api:1.2.1.1' annotationProcessor 'com.alibaba:arouter-compiler:1.0.3'*/ implementation project(':login-module') implementation project(':circle-module') } ================================================ FILE: app/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # You can control the set of applied configuration files using the # proguardFiles setting in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile ================================================ FILE: app/src/androidTest/java/com/darren/drouter/ExampleInstrumentedTest.kt ================================================ package com.darren.drouter import android.support.test.InstrumentationRegistry import android.support.test.runner.AndroidJUnit4 import org.junit.Test import org.junit.runner.RunWith import org.junit.Assert.* /** * Instrumented test, which will execute on an Android device. * * See [testing documentation](http://d.android.com/tools/testing). */ @RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { @Test fun useAppContext() { // Context of the app under test. val appContext = InstrumentationRegistry.getTargetContext() assertEquals("com.darren.drouter", appContext.packageName) } } ================================================ FILE: app/src/main/AndroidManifest.xml ================================================ ================================================ FILE: app/src/main/java/com/darren/drouter/BaseApplication.java ================================================ package com.darren.drouter; import android.app.Application; import com.drouter.api.core.DRouter; /** * description: * author: Darren on 2018/1/22 11:27 * email: 240336124@qq.com * version: 1.0 */ public class BaseApplication extends Application{ @Override public void onCreate() { super.onCreate(); DRouter.openDebug(); DRouter.getInstance().init(this); } } ================================================ FILE: app/src/main/java/com/darren/drouter/MainActivity.java ================================================ package com.darren.drouter; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import com.drouter.api.core.DRouter; import com.drouter.api.result.ActionCallback; import com.drouter.api.result.RouterResult; /** * description: * author: Darren on 2018/1/22 09:43 * email: 240336124@qq.com * version: 1.0 */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void jumpLogin(View view) { DRouter.getInstance() .action("login/action") .context(this) .param("key", "value") .invokeAction(); } public void jumpCircle(View view) { DRouter.getInstance() .action("circlemodule/test") .context(this) .param("key", "value") .invokeAction(new ActionCallback() { @Override public void onInterrupt() { Log.e("TAG", "被拦截了"); } @Override public void onResult(RouterResult result) { Log.e("TAG", "result = " + result.toString()); } }); } } ================================================ FILE: app/src/main/res/drawable/ic_launcher_background.xml ================================================ ================================================ FILE: app/src/main/res/drawable-v24/ic_launcher_foreground.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_main.xml ================================================