> rules);
9 | }
10 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/router/IRouterInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.router;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 |
6 |
7 | public interface IRouterInterceptor {
8 | boolean intercept(Context context, String url, Intent matchIntent);
9 | }
10 |
--------------------------------------------------------------------------------
/appbox/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/module_a/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/module_b/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/appbox/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/module_a/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/module_b/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/module_service/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/module_service/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/module_a/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/module_b/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/apt/src/main/java/com/easy/moduler/apt/util/NoPackageNameException.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.apt.util;
2 |
3 | import javax.lang.model.element.TypeElement;
4 |
5 | public class NoPackageNameException extends Exception {
6 |
7 | public NoPackageNameException(TypeElement typeElement) {
8 | super("The package of " + typeElement.getSimpleName() + " has no name");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/apt/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | sourceCompatibility = JavaVersion.VERSION_1_7
4 | targetCompatibility = JavaVersion.VERSION_1_7
5 |
6 | dependencies {
7 | compile project(':annotation')
8 | compile 'com.google.auto.service:auto-service:1.0-rc3'
9 | compile 'com.squareup:javapoet:1.9.0'
10 | }
11 |
12 | tasks.withType(JavaCompile) {
13 | options.encoding = "UTF-8"
14 | }
15 |
--------------------------------------------------------------------------------
/annotation/src/main/java/com/easy/moduler/annotation/RouterRule.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.annotation;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.CLASS)
9 | @Target(ElementType.TYPE)
10 | public @interface RouterRule {
11 | String[] value();
12 | }
13 |
--------------------------------------------------------------------------------
/apt/src/main/java/com/easy/moduler/apt/inter/IProcessor.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.apt.inter;
2 |
3 |
4 | import com.easy.moduler.apt.AnnotationProcessor;
5 |
6 | import javax.annotation.processing.RoundEnvironment;
7 |
8 | /**
9 | * Created by baixiaokang on 16/10/8.
10 | * 注解处理器接口
11 | */
12 |
13 | public interface IProcessor {
14 | void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor);
15 | }
16 |
--------------------------------------------------------------------------------
/appbox/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/module_a/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/module_b/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/module_service/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/IModule.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | /**
4 | * Created by baixiaokang on 18/3/6.
5 | */
6 |
7 | public interface IModule {
8 | /**
9 | * 模块初始化,只有组建时才调用,用于开启子线程轮训消息
10 | */
11 | void init();
12 |
13 | /**
14 | * 模块ID
15 | *
16 | * @return 模块ID
17 | */
18 | int getModuleId();
19 |
20 | /**
21 | * 模块注册并连接成功后,可以做以下事情:
22 | *
23 | * 1、注册监听事件
24 | * 2、发送事件
25 | * 3、注册服务
26 | * 4、调用服务
27 | */
28 | void afterConnected();
29 | }
30 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/utils/StringUtils.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.utils;
2 |
3 | /**
4 | * Created by baixiaokang on 18/3/6.
5 | */
6 |
7 | public class StringUtils {
8 | /**
9 | * 比较字符串不记大小写是否相同
10 | *
11 | * @param string1
12 | * @param string2
13 | * @return
14 | */
15 | public static boolean equalsIgnoreCase(String string1, String string2) {
16 | if (string1 == string2) return true;
17 | if (string1 == null) return false;
18 | return string1.equalsIgnoreCase(string2);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/module_b/src/main/java/com/easy/moduler/module_b/Module.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.module_b;
2 |
3 | import com.easy.moduler.lib.Constants;
4 | import com.easy.moduler.lib.okbus.BaseModule;
5 | import com.easy.moduler.lib.okbus.IModule;
6 | import com.google.auto.service.AutoService;
7 |
8 | /**
9 | * Created by baixiaokang on 18/3/6.
10 | */
11 |
12 | @AutoService(IModule.class)
13 | public class Module extends BaseModule {
14 | @Override
15 | public void afterConnected() {
16 |
17 |
18 | }
19 |
20 | @Override
21 | public int getModuleId() {
22 | return Constants.MODULE_B;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/module_service/src/main/java/com/easy/moduler/module_service/Module.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.module_service;
2 |
3 | import com.easy.moduler.lib.Constants;
4 | import com.easy.moduler.lib.okbus.BaseModule;
5 | import com.easy.moduler.lib.okbus.IModule;
6 | import com.google.auto.service.AutoService;
7 |
8 | /**
9 | * Created by baixiaokang on 18/3/6.
10 | */
11 |
12 | @AutoService(IModule.class)
13 | public class Module extends BaseModule {
14 | @Override
15 | public void afterConnected() {
16 |
17 | }
18 |
19 | @Override
20 | public int getModuleId() {
21 | return Constants.MODULE_S;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/module_service/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/annotation/src/main/java/com/easy/moduler/annotation/Bus.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.annotation;
2 |
3 | /**
4 | * Created by baixiaokang on 16/11/15.
5 | */
6 |
7 | import java.lang.annotation.ElementType;
8 | import java.lang.annotation.Retention;
9 | import java.lang.annotation.RetentionPolicy;
10 | import java.lang.annotation.Target;
11 |
12 |
13 | @Retention(RetentionPolicy.RUNTIME)
14 | @Target(ElementType.METHOD)
15 | public @interface Bus {
16 | int DEFAULT = -1;
17 | int UI = 0;
18 | int BG = 1;
19 |
20 | /**
21 | * 事件订阅的线程
22 | *
23 | * @return
24 | */
25 | int thread() default DEFAULT;
26 |
27 | /**
28 | * 事件id
29 | *
30 | * @return
31 | */
32 | int value();
33 | }
34 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
19 |
20 | isDebug=false
--------------------------------------------------------------------------------
/appbox/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/appbox/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/module_a/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/module_b/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/module_service/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/module_service/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | module_service
3 |
4 |
5 | Email
6 | Password (optional)
7 | Sign in or register
8 | Sign in
9 | This email address is invalid
10 | This password is too short
11 | This password is incorrect
12 | This field is required
13 | "Contacts permissions are needed for providing email
14 | completions."
15 |
16 |
17 |
--------------------------------------------------------------------------------
/appbox/src/main/java/com/easy/moduler/appbox/App.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.appbox;
2 |
3 | import android.app.Application;
4 |
5 | import com.easy.moduler.lib.okbus.IModule;
6 | import com.easy.moduler.lib.router.IRouterRulesCreator;
7 | import com.easy.moduler.lib.router.Router;
8 |
9 | import java.util.ServiceLoader;
10 |
11 | /**
12 | * Created by baixiaokang on 18/3/6.
13 | */
14 |
15 | public class App extends Application {
16 | @Override
17 | public void onCreate() {
18 | super.onCreate();
19 |
20 | //SPI自动注册路由
21 | ServiceLoader rules = ServiceLoader.load(IRouterRulesCreator.class);
22 | for (IRouterRulesCreator rule : rules) Router.addRouterRule(rule);
23 |
24 | //SPI自动注册服务
25 | ServiceLoader modules = ServiceLoader.load(IModule.class);
26 | for (IModule module : modules) module.afterConnected();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/module_a/src/main/java/com/easy/moduler/module_a/Module.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.module_a;
2 |
3 | import com.easy.moduler.lib.Constants;
4 | import com.easy.moduler.lib.okbus.BaseModule;
5 | import com.easy.moduler.lib.okbus.IModule;
6 | import com.easy.moduler.lib.okbus.ServiceBus;
7 | import com.easy.moduler.lib.utils.LogUtils;
8 | import com.google.auto.service.AutoService;
9 |
10 | /**
11 | * Created by baixiaokang on 18/3/6.
12 | */
13 |
14 | @AutoService(IModule.class)
15 | public class Module extends BaseModule {
16 |
17 | @Override
18 | public void afterConnected() {
19 |
20 | ServiceBus.getInstance().registerService(Constants.SERVICE_A_UID, msg -> {
21 | LogUtils.logOnUI(Constants.TAG, "afterConnected a 进程收到[服务请求]消息:ServiceMessage-->hello: " + Integer.toHexString(Math.abs(msg.what)));
22 | return "10086";
23 | });
24 | }
25 |
26 | @Override
27 | public int getModuleId() {
28 | return Constants.MODULE_A;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/appbox/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 |
6 |
7 | defaultConfig {
8 | applicationId "com.easy.moduler.appbox"
9 | minSdkVersion 17
10 | targetSdkVersion 26
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | javaCompileOptions {
15 | annotationProcessorOptions {
16 | includeCompileClasspath false
17 | }
18 | }
19 | }
20 |
21 |
22 | compileOptions {
23 | sourceCompatibility 1.8
24 | targetCompatibility 1.8
25 | }
26 |
27 | buildTypes {
28 | release {
29 | minifyEnabled false
30 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
31 | }
32 | }
33 |
34 | }
35 |
36 | dependencies {
37 | if (isDebug.toBoolean()) {//调试阶段,只保证基本逻辑不报错
38 | implementation project(":lib")
39 | } else {//打包阶段,才真正的引入业务逻辑模块
40 | implementation project(":module_a")
41 | implementation project(":module_b")
42 | implementation project(":module_service")
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion '26.0.2'
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 17
10 | targetSdkVersion 26
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | javaCompileOptions {
15 | annotationProcessorOptions {
16 | includeCompileClasspath false
17 | }
18 | }
19 |
20 | }
21 |
22 | buildTypes {
23 | release {
24 | minifyEnabled false
25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
26 | }
27 | }
28 |
29 | compileOptions {
30 | sourceCompatibility JavaVersion.VERSION_1_8
31 | targetCompatibility JavaVersion.VERSION_1_8
32 | }
33 | }
34 |
35 | dependencies {
36 | implementation fileTree(dir: 'libs', include: ['*.jar'])
37 |
38 | compile 'com.google.auto.service:auto-service:1.0-rc3'
39 | compile 'com.android.support:appcompat-v7:26.1.0'
40 | compile 'com.android.support:design:26.1.0'
41 | compile project(":annotation")
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/module_a/src/main/res/layout/activity_amodule.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
21 |
22 |
25 |
26 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/module_b/src/main/debug/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
14 |
15 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/module_a/src/main/debug/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
14 |
15 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/module_b/src/main/res/layout/activity_bmodule.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
21 |
22 |
23 |
30 |
31 |
32 |
33 |
36 |
37 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/module_service/src/main/res/layout/activity_login.xml:
--------------------------------------------------------------------------------
1 |
12 |
13 |
20 |
21 |
22 |
29 |
30 |
33 |
34 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/appbox/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
19 |
20 |
21 |
28 |
29 |
36 |
37 |
38 |
41 |
42 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/module_a/build.gradle:
--------------------------------------------------------------------------------
1 | if (isDebug.toBoolean()) {
2 | apply plugin: 'com.android.application'
3 | } else {
4 | apply plugin: 'com.android.library'
5 | }
6 |
7 | android {
8 | compileSdkVersion 26
9 |
10 | defaultConfig {
11 | minSdkVersion 17
12 | targetSdkVersion 26
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | javaCompileOptions {
17 | annotationProcessorOptions {
18 | includeCompileClasspath false
19 | arguments = [ moduleName : project.getName() ]
20 | }
21 | }
22 |
23 | }
24 |
25 | compileOptions {
26 | sourceCompatibility 1.8
27 | targetCompatibility 1.8
28 | }
29 |
30 | sourceSets {
31 | main {
32 | //默认的作为application运行时Manifest文件路径
33 | if (isDebug.toBoolean()) {
34 | manifest.srcFile 'src/main/debug/AndroidManifest.xml'
35 | } else {
36 | manifest.srcFile 'src/main/AndroidManifest.xml'
37 | //集成开发模式下自动排除debug文件夹中的所有Java文件
38 | // 可以将debug代码放在这个包内,例如:Application子类
39 | java {
40 | exclude 'debug/**'
41 | }
42 | }
43 | }
44 | }
45 |
46 | buildTypes {
47 |
48 |
49 | release {
50 |
51 | minifyEnabled false
52 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
53 | }
54 | }
55 |
56 | }
57 |
58 | dependencies {
59 | implementation fileTree(dir: 'libs', include: ['*.jar'])
60 |
61 | compile project(":lib")
62 | annotationProcessor project(':apt')
63 | }
64 |
--------------------------------------------------------------------------------
/module_b/build.gradle:
--------------------------------------------------------------------------------
1 | if (isDebug.toBoolean()) {
2 | apply plugin: 'com.android.application'
3 | } else {
4 | apply plugin: 'com.android.library'
5 | }
6 |
7 | android {
8 | compileSdkVersion 26
9 |
10 | defaultConfig {
11 | minSdkVersion 17
12 | targetSdkVersion 26
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | javaCompileOptions {
17 | annotationProcessorOptions {
18 | includeCompileClasspath false
19 | arguments = [ moduleName : project.getName() ]
20 | }
21 | }
22 |
23 | }
24 |
25 | compileOptions {
26 | sourceCompatibility 1.8
27 | targetCompatibility 1.8
28 | }
29 |
30 | sourceSets {
31 | main {
32 | //默认的作为application运行时Manifest文件路径
33 | if (isDebug.toBoolean()) {
34 | manifest.srcFile 'src/main/debug/AndroidManifest.xml'
35 | } else {
36 | manifest.srcFile 'src/main/AndroidManifest.xml'
37 | //集成开发模式下自动排除debug文件夹中的所有Java文件
38 | // 可以将debug代码放在这个包内,例如:Application子类
39 | java {
40 | exclude 'debug/**'
41 | }
42 | }
43 | }
44 | }
45 |
46 | buildTypes {
47 |
48 |
49 | release {
50 |
51 | minifyEnabled false
52 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
53 | }
54 | }
55 |
56 | }
57 |
58 | dependencies {
59 | implementation fileTree(dir: 'libs', include: ['*.jar'])
60 |
61 | compile project(":lib")
62 | annotationProcessor project(':apt')
63 | }
64 |
--------------------------------------------------------------------------------
/module_service/build.gradle:
--------------------------------------------------------------------------------
1 | if (isDebug.toBoolean()) {
2 | apply plugin: 'com.android.application'
3 | } else {
4 | apply plugin: 'com.android.library'
5 | }
6 |
7 | android {
8 | compileSdkVersion 26
9 |
10 | defaultConfig {
11 | minSdkVersion 17
12 | targetSdkVersion 26
13 | versionCode 1
14 | versionName "1.0"
15 | javaCompileOptions {
16 | annotationProcessorOptions {
17 | includeCompileClasspath false
18 | arguments = [ moduleName : project.getName() ]
19 | }
20 | }
21 | }
22 |
23 | compileOptions {
24 | sourceCompatibility 1.8
25 | targetCompatibility 1.8
26 | }
27 |
28 | sourceSets {
29 | main {
30 | //默认的作为application运行时Manifest文件路径
31 | if (isDebug.toBoolean()) {
32 | manifest.srcFile 'src/main/debug/AndroidManifest.xml'
33 | } else {
34 | manifest.srcFile 'src/main/AndroidManifest.xml'
35 | //集成开发模式下自动排除debug文件夹中的所有Java文件
36 | // 可以将debug代码放在这个包内,例如:Application子类
37 | java {
38 | exclude 'debug/**'
39 | }
40 | }
41 | }
42 | }
43 |
44 | buildTypes {
45 |
46 |
47 | release {
48 |
49 | minifyEnabled false
50 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
51 | }
52 | }
53 |
54 | }
55 |
56 | dependencies {
57 | implementation fileTree(dir: 'libs', include: ['*.jar'])
58 |
59 |
60 | compile project(":lib")
61 | annotationProcessor project(':apt')
62 | }
63 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/NoticeService.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | import android.app.Service;
4 | import android.content.Intent;
5 | import android.os.IBinder;
6 | import android.os.Messenger;
7 | import android.support.annotation.Nullable;
8 |
9 | import com.easy.moduler.lib.Constants;
10 | import com.easy.moduler.lib.utils.LogUtils;
11 |
12 | import java.util.concurrent.CountDownLatch;
13 | import java.util.concurrent.TimeUnit;
14 | import java.util.concurrent.atomic.AtomicReference;
15 |
16 | /**
17 | * Created by baixiaokang on 18/3/7.
18 | * 客户端的唤醒服务
19 | */
20 |
21 | public class NoticeService extends Service {
22 |
23 |
24 | private CountDownLatch latch = new CountDownLatch(1);
25 | private AtomicReference resultRef = new AtomicReference<>();
26 |
27 |
28 | /**
29 | * 收到唤醒通知之后,初始化模块,并自动去服务器注册
30 | *
31 | * @param intent
32 | * @return
33 | */
34 | @Nullable
35 | @Override
36 | public IBinder onBind(Intent intent) {
37 | LogUtils.logOnUI(Constants.TAG, getPackageName() + " 收到唤醒通知");
38 | BaseModule mBaseModule = BaseAppModuleApp.getBaseApplication().mBaseModule;
39 | if (!mBaseModule.isConnected.get()) {
40 | LogUtils.logOnUI(Constants.TAG, getPackageName() + " 我被唤醒啦");
41 | mBaseModule.init(latch, resultRef);
42 | mBaseModule.afterConnected();
43 | try {
44 | latch.await(2000, TimeUnit.SECONDS);
45 | } catch (Exception e) { //等待中断
46 | e.printStackTrace();
47 | }
48 | }
49 | return mBaseModule.mWorkThread.clientHandler.getBinder();
50 | }
51 | }
--------------------------------------------------------------------------------
/module_service/src/main/debug/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
17 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/ClientHandler.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | import android.os.Bundle;
4 | import android.os.Handler;
5 | import android.os.Message;
6 |
7 | import com.easy.moduler.lib.Constants;
8 | import com.easy.moduler.lib.utils.LogUtils;
9 |
10 |
11 | /**
12 | * Created by baixiaokang on 18/3/1.
13 | * 客户端消息处理器
14 | *
15 | * 1、普通消息,转发给自己的OkBus
16 | * 2、模块注册消息,打印
17 | */
18 |
19 | public class ClientHandler extends Handler {
20 |
21 | @Override
22 | public void handleMessage(Message msg) {
23 | super.handleMessage(msg);
24 | Bundle bundle = msg.getData();
25 | boolean noticeFlag = bundle.getBoolean(Constants.NOTICE_MSG, false);
26 | BaseModule mBaseModule = BaseAppModuleApp.getBaseApplication().mBaseModule;
27 | if (noticeFlag && !mBaseModule.isConnected.get()) {//唤醒通知,自动注册
28 | OkBus.getInstance().initModule(mBaseModule, msg.replyTo, mBaseModule.getModuleId(), mBaseModule.mWorkThread.clientHandler);
29 | return;
30 | }
31 | int resCode = bundle.getInt(Constants.REGISTER_RES, -1);
32 | if (resCode < 0) {//收到普通消息
33 | String hex = Integer.toHexString(Math.abs(msg.what));
34 | LogUtils.i(Constants.TAG + " ClientHandler", "handleMessage: msg = [hello:收到" + (msg.what > 0 ? "普通" : "服务") + "事件类型的消息:" + hex + "]-->[转发给自己的OkBus]: " + msg);
35 | OkBus.getInstance().onLocalEvent(msg.what, bundle.getSerializable(Constants.MESSAGE_DATA));
36 | } else {//收到模块注册结果消息
37 | boolean isRegisterSec = resCode == Constants.REGISTER_SEC;
38 | if (isRegisterSec) {
39 | LogUtils.logOnUI(Constants.TAG, "handleMessage() : reply = [注册成功]");
40 | }
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/appbox/src/main/java/com/easy/moduler/appbox/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.appbox;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.View;
7 | import android.widget.TextView;
8 |
9 | import com.easy.moduler.annotation.Bus;
10 | import com.easy.moduler.annotation.RouterRule;
11 | import com.easy.moduler.lib.Constants;
12 | import com.easy.moduler.lib.okbus.Event;
13 | import com.easy.moduler.lib.okbus.OkBus;
14 | import com.easy.moduler.lib.router.Router;
15 |
16 |
17 | @RouterRule("moduleHost://com.module.main")
18 | public class MainActivity extends AppCompatActivity {
19 |
20 | Event mLogEvent;
21 |
22 | @SuppressLint("SetTextI18n")
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_main);
27 | TextView tvLog = findViewById(R.id.tv_log);
28 | OkBus.getInstance().register(Constants.MODULE_PRINT_LOG, mLogEvent = msg -> {
29 | String log = tvLog.getText().toString();
30 | tvLog.setText(msg.obj + "\n" + log);
31 | }, Bus.UI);
32 | }
33 |
34 | @Override
35 | protected void onDestroy() {
36 | super.onDestroy();
37 | OkBus.getInstance().unRegister(mLogEvent);
38 | }
39 |
40 | public void onclick(View view) {
41 | switch (view.getId()) {
42 | case R.id.bt_a:
43 | Router.open(this, "moduleHost://com.module.moduleA");
44 | break;
45 | case R.id.bt_b:
46 | Router.open(this, "moduleHost://com.module.moduleB");
47 | break;
48 | case R.id.bt_s:
49 | Router.open(this, "moduleHost://com.module.login");
50 | break;
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/BaseAppModuleApp.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | import android.app.Application;
4 | import android.content.Intent;
5 |
6 | import com.easy.moduler.lib.Constants;
7 | import com.easy.moduler.lib.router.IRouterRulesCreator;
8 | import com.easy.moduler.lib.router.Router;
9 | import com.easy.moduler.lib.utils.LogUtils;
10 |
11 | import java.util.ServiceLoader;
12 |
13 | /**
14 | * Created by baixiaokang on 18/3/6.
15 | * 组件的独立运行期间的Application,最终打包时根本没有这个类
16 | */
17 |
18 | public class BaseAppModuleApp extends Application {
19 |
20 | public BaseModule mBaseModule;
21 | public static BaseAppModuleApp mBaseAppModuleApp;
22 |
23 | @Override//只有当是组建单独运行时,才当Application运行,才会走onCreate,最终打包时根本没有这个类
24 | public void onCreate() {
25 | super.onCreate();
26 | mBaseAppModuleApp = this;
27 | //自动注册路由器
28 | ServiceLoader loader = ServiceLoader.load(IRouterRulesCreator.class);
29 | for (IRouterRulesCreator aLoader : loader) Router.addRouterRule(aLoader);
30 |
31 | //自动注册服务器
32 | ServiceLoader modules = ServiceLoader.load(IModule.class);
33 | mBaseModule = (BaseModule) modules.iterator().next();
34 |
35 | //模块初始化
36 | mBaseModule.init();
37 |
38 | //连接服务
39 | connectService();
40 | }
41 |
42 | /**
43 | * 连接服务器
44 | */
45 | public void connectService() {
46 | Intent intent = new Intent(MessengerService.class.getCanonicalName());// 5.0+ need explicit intent
47 | intent.setPackage(Constants.SERVICE_PACKAGE_NAME); // the package name of Remote Service
48 | boolean mIsBound = bindService(intent, mBaseModule.mConnection, BIND_AUTO_CREATE);
49 | LogUtils.i(Constants.TAG + " connectService", " ServiceConnection-->bindService mIsBound: " + mIsBound);
50 | }
51 |
52 | public static BaseAppModuleApp getBaseApplication() {
53 | return mBaseAppModuleApp;
54 | }
55 | }
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/Constants.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib;
2 |
3 | /**
4 | * Created by baixiaokang on 18/3/6.
5 | */
6 |
7 | public class Constants {
8 |
9 |
10 | //==================URL========================//
11 | //组件的包名前缀
12 | public static final String MODULE_PACKAGE_PRE = "com.easy.moduler.module_";
13 | //服务器的包名
14 | public static final String SERVICE_PACKAGE_NAME = "com.easy.moduler.module_service";
15 |
16 | //==================MessagerConstants============//
17 | public static final String TAG = "Message";
18 |
19 | public static final String MODULE_NAME = "module";
20 | public static final String MESSAGE_DATA = "message_data";
21 | public static final String REGISTER_ID = "registerId";
22 | public static final String REGISTER_RES = "registerRes";//注册结果 0 失败 1 成功
23 | public static final String NOTICE_MSG = "notice_message";
24 |
25 | public static final int REGISTER_SEC = 1;
26 | public static final int REGISTER_FAIL = 0;
27 |
28 |
29 | //==========模块以及模块下的事件============//
30 | /**
31 | * 模块定义说明:
32 | *
33 | * 模块暂定为0x1 ->0xf 例如:0xa
34 | *
35 | * 模块下的事件 暂定为:模块名+事件id 例如:0xa001
36 | */
37 | public static final int ROUTER_OPEN_URL = 0x0000;//打开制定url
38 |
39 | public static final int MODULE_S = 0x0;//服务器标识
40 |
41 | public static final int MODULE_PRINT_LOG = 0x0001;//log打印事件
42 |
43 |
44 | public static final int MODULE_A = 0xa;
45 | public static final int MODULE_A_EVENT001 = 0xa001;//a模块事件1
46 |
47 |
48 | public static final int MODULE_B = 0xb;
49 | public static final int MODULE_B_EVENT001 = 0xb001;//b模块事件1
50 |
51 | //==================模块间的服务定义============//
52 | /**
53 | * 服务定义规则:
54 | * 1、服务的请求ID必须是负值(正值表示事件)
55 | * 2、服务的请求ID必须是奇数,偶数表示该服务的返回事件,
56 | * 即: requestID-1 = returnID
57 | * 例如 -0xa001表示服务请求 -0xa002表示-0xa001的服务返回
58 | */
59 | public static final int SERVICE_A_UID = -0xa001;
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/module_a/src/main/java/com/easy/moduler/module_a/AModuleActivity.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.module_a;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.widget.EditText;
7 | import android.widget.TextView;
8 |
9 | import com.easy.moduler.annotation.Bus;
10 | import com.easy.moduler.annotation.RouterRule;
11 | import com.easy.moduler.lib.Constants;
12 | import com.easy.moduler.lib.okbus.Event;
13 | import com.easy.moduler.lib.okbus.OkBus;
14 | import com.easy.moduler.lib.okbus.ServiceBus;
15 | import com.easy.moduler.lib.utils.LogUtils;
16 |
17 | @RouterRule("moduleHost://com.module.moduleA")
18 | public class AModuleActivity extends AppCompatActivity {
19 | Event mBEvent, mLogEvent;
20 |
21 |
22 | @SuppressLint("SetTextI18n")
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_amodule);
27 | EditText editText = findViewById(R.id.et_uid);
28 |
29 | ServiceBus.getInstance().registerService(Constants.SERVICE_A_UID, msg -> {
30 | LogUtils.logOnUI(Constants.TAG, "a 进程收到[服务请求]消息:ServiceMessage-->hello: " + Integer.toHexString(Math.abs(msg.what)));
31 | return editText.getText().toString();
32 | });
33 |
34 | TextView tvLog = findViewById(R.id.tv_log);
35 | OkBus.getInstance().register(Constants.MODULE_B_EVENT001, mBEvent = msg -> LogUtils.logOnUI(Constants.TAG, "a 进程收到消息:Message-->a: " + msg.obj));
36 | OkBus.getInstance().register(Constants.MODULE_PRINT_LOG, mLogEvent = msg -> {
37 | String log = tvLog.getText().toString();
38 | tvLog.setText(msg.obj + "\n" + log);
39 | }, Bus.UI);
40 | }
41 |
42 | @Override
43 | protected void onDestroy() {
44 | super.onDestroy();
45 | OkBus.getInstance().unRegister(mBEvent);
46 | OkBus.getInstance().unRegister(mLogEvent);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/appbox/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/module_a/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/module_b/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/module_service/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/module_service/src/main/java/com/easy/moduler/module_service/LoginActivity.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.module_service;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.View;
7 | import android.widget.TextView;
8 |
9 | import com.easy.moduler.annotation.Bus;
10 | import com.easy.moduler.annotation.RouterRule;
11 | import com.easy.moduler.lib.Constants;
12 | import com.easy.moduler.lib.okbus.Event;
13 | import com.easy.moduler.lib.okbus.OkBus;
14 | import com.easy.moduler.lib.router.Router;
15 | import com.easy.moduler.lib.utils.LogUtils;
16 |
17 |
18 | @RouterRule("moduleHost://com.module.login")
19 | public class LoginActivity extends AppCompatActivity {
20 | Event mEvent, mLogEvent;
21 |
22 | @SuppressLint("SetTextI18n")
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_login);
27 |
28 |
29 | OkBus.getInstance().register(Constants.MODULE_B_EVENT001, mEvent = msg -> LogUtils.logOnUI(Constants.TAG, "service进程收到消息:Message-->service: " + msg.obj));
30 |
31 | TextView tvLog = findViewById(R.id.tv_log);
32 | OkBus.getInstance().register(Constants.MODULE_PRINT_LOG, mLogEvent = msg -> {
33 | String log = tvLog.getText().toString();
34 | tvLog.setText(msg.obj + "\n" + log);
35 | }, Bus.UI);
36 |
37 | }
38 |
39 |
40 | @Override
41 | protected void onDestroy() {
42 | super.onDestroy();
43 | OkBus.getInstance().unRegister(mEvent);
44 | OkBus.getInstance().unRegister(mLogEvent);
45 | }
46 |
47 | public void onClick(View view) {
48 | int i = view.getId();
49 | if (i == R.id.bt_a) {
50 | Router.open(this, "moduleHost://com.module.moduleA?module=a");//子组件打开url时需要告知模块id,用于自动唤醒,主app则不需要
51 | } else if (i == R.id.bt_b) {
52 | Router.open(this, "moduleHost://com.module.moduleB?module=b");
53 | }
54 | }
55 | }
56 |
57 |
--------------------------------------------------------------------------------
/apt/src/main/java/com/easy/moduler/apt/AnnotationProcessor.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.apt;
2 |
3 | import com.easy.moduler.annotation.RouterRule;
4 | import com.easy.moduler.apt.processor.RouterRuleProcessor;
5 | import com.google.auto.service.AutoService;
6 |
7 | import java.util.LinkedHashSet;
8 | import java.util.Set;
9 |
10 | import javax.annotation.processing.AbstractProcessor;
11 | import javax.annotation.processing.Filer;
12 | import javax.annotation.processing.Messager;
13 | import javax.annotation.processing.ProcessingEnvironment;
14 | import javax.annotation.processing.Processor;
15 | import javax.annotation.processing.RoundEnvironment;
16 | import javax.annotation.processing.SupportedOptions;
17 | import javax.annotation.processing.SupportedSourceVersion;
18 | import javax.lang.model.SourceVersion;
19 | import javax.lang.model.element.TypeElement;
20 | import javax.lang.model.util.Elements;
21 |
22 | import static com.easy.moduler.apt.AnnotationProcessor.KEY_MODULE_NAME;
23 |
24 | @AutoService(Processor.class)//自动生成 javax.annotation.processing.IProcessor 文件
25 | @SupportedSourceVersion(SourceVersion.RELEASE_8)//java版本支持
26 | @SupportedOptions(KEY_MODULE_NAME)//支持的配置参数
27 | public class AnnotationProcessor extends AbstractProcessor {
28 | public static final String KEY_MODULE_NAME = "moduleName";
29 | public Filer mFiler; //文件相关的辅助类
30 | public Elements mElements; //元素相关的辅助类
31 | public Messager mMessager; //日志相关的辅助类
32 | public ProcessingEnvironment mProcessingEnv;
33 |
34 | @Override
35 | public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
36 | mMessager = processingEnv.getMessager();
37 | mFiler = processingEnv.getFiler();
38 | mElements = processingEnv.getElementUtils();
39 | mProcessingEnv = processingEnv;
40 | new RouterRuleProcessor().process(roundEnv, this);
41 | return true;
42 | }
43 |
44 | @Override
45 | public Set getSupportedAnnotationTypes() {
46 | Set types = new LinkedHashSet<>();
47 | types.add(RouterRule.class.getCanonicalName());
48 | return types;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/apt/src/main/java/com/easy/moduler/apt/util/Utils.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.apt.util;
2 |
3 | import com.squareup.javapoet.ClassName;
4 |
5 | import javax.annotation.processing.Messager;
6 | import javax.lang.model.element.ElementKind;
7 | import javax.lang.model.element.PackageElement;
8 | import javax.lang.model.element.TypeElement;
9 | import javax.lang.model.util.Elements;
10 | import javax.tools.Diagnostic;
11 |
12 | import static javax.lang.model.element.Modifier.ABSTRACT;
13 | import static javax.lang.model.element.Modifier.PUBLIC;
14 |
15 | /**
16 | * Created by baixiaokang on 16/8/3.
17 | */
18 | public class Utils {
19 | public static final String PackageName = "com.apt";
20 | public static final String ANNOTATION = "@";
21 |
22 |
23 | public static boolean isPublic(TypeElement element) {
24 | return element.getModifiers().contains(PUBLIC);
25 | }
26 |
27 | public static boolean isAbstract(TypeElement element) {
28 | return element.getModifiers().contains(ABSTRACT);
29 | }
30 |
31 | public static boolean isValidClass(Messager messager, TypeElement element) {
32 | if (element.getKind() != ElementKind.CLASS) {
33 | return false;
34 | }
35 |
36 | if (!isPublic(element)) {
37 | String message = String.format("Classes annotated with %s must be public.", ANNOTATION);
38 | messager.printMessage(Diagnostic.Kind.ERROR, message, element);
39 | return false;
40 | }
41 |
42 | if (isAbstract(element)) {
43 | String message = String.format("Classes annotated with %s must not be abstract.", ANNOTATION);
44 | messager.printMessage(Diagnostic.Kind.ERROR, message, element);
45 | return false;
46 | }
47 |
48 | return true;
49 | }
50 |
51 | public static String getPackageName(Elements elements, TypeElement typeElement) throws NoPackageNameException {
52 | PackageElement pkg = elements.getPackageOf(typeElement);
53 | if (pkg.isUnnamed()) {
54 | throw new NoPackageNameException(typeElement);
55 | }
56 | return pkg.getQualifiedName().toString();
57 | }
58 |
59 |
60 | public static String getClassName(TypeElement typeElement) throws ClassNotFoundException {
61 | return ClassName.get(typeElement).simpleName();
62 | }
63 |
64 | public static ClassName getType(String className) {
65 | return ClassName.get(className.substring(0, className.lastIndexOf(".")),
66 | className.substring(className.lastIndexOf(".") + 1, className.length()));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | Android
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | 1.8
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/module_b/src/main/java/com/easy/moduler/module_b/BModuleActivity.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.module_b;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.widget.TextView;
7 | import android.widget.Toast;
8 |
9 | import com.easy.moduler.annotation.Bus;
10 | import com.easy.moduler.annotation.RouterRule;
11 | import com.easy.moduler.lib.Constants;
12 | import com.easy.moduler.lib.okbus.Event;
13 | import com.easy.moduler.lib.okbus.OkBus;
14 | import com.easy.moduler.lib.okbus.ServiceBus;
15 | import com.easy.moduler.lib.utils.LogUtils;
16 |
17 | @RouterRule("moduleHost://com.module.moduleB")
18 | public class BModuleActivity extends AppCompatActivity {
19 |
20 | private static final String TAG = "Message :BModuleActivity";
21 | private Event mLogEvent;
22 |
23 | @SuppressLint("SetTextI18n")
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_bmodule);
28 |
29 | TextView tvLog = findViewById(R.id.tv_log);
30 | OkBus.getInstance().register(Constants.MODULE_PRINT_LOG, mLogEvent = msg -> {
31 | String log = tvLog.getText().toString();
32 | tvLog.setText(msg.obj + "\n" + log);
33 | }, Bus.UI);
34 |
35 | LogUtils.logOnUI(Constants.TAG, "b 进程发送消息");
36 | OkBus.getInstance().onEvent(Constants.MODULE_B_EVENT001, "b-->Message:恭喜发财!BBB");
37 |
38 | /**
39 | * 异步调用远端服务
40 | */
41 | findViewById(R.id.bt_1).setOnClickListener(v -> {
42 |
43 | ServiceBus.getInstance().fetchService(Constants.SERVICE_A_UID, msg -> {
44 | LogUtils.logOnUI(Constants.TAG, "b 进程收到[异步服务返回]消息: 获取到的UID-->" + msg.obj);
45 | Toast.makeText(BModuleActivity.this,
46 | "b 进程收到[异步服务返回]消息: 获取到的UID-->" + msg.obj,
47 | Toast.LENGTH_SHORT).show();
48 | });
49 | });
50 |
51 | /**
52 | * 同步调用远端服务
53 | */
54 | findViewById(R.id.bt_2).setOnClickListener(v -> {
55 | try {
56 | String uid = ServiceBus.getInstance().fetchService(Constants.SERVICE_A_UID);
57 | LogUtils.logOnUI(Constants.TAG, "b 进程收到[同步服务返回]消息: 获取到的UID-->" + uid);
58 | Toast.makeText(BModuleActivity.this, "b 进程收到[同步服务返回]消息: 获取到的UID-->" + uid, Toast.LENGTH_SHORT).show();
59 | } catch (Exception e) {
60 | e.printStackTrace();
61 | }
62 | });
63 | }
64 |
65 | @Override
66 | protected void onDestroy() {
67 | super.onDestroy();
68 | OkBus.getInstance().unRegister(mLogEvent);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/MessengerService.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | import android.app.Service;
4 | import android.content.Intent;
5 | import android.os.IBinder;
6 | import android.os.Looper;
7 | import android.os.Messenger;
8 | import android.support.annotation.Nullable;
9 |
10 | import com.easy.moduler.lib.Constants;
11 | import com.easy.moduler.lib.utils.LogUtils;
12 |
13 | import java.util.concurrent.CountDownLatch;
14 | import java.util.concurrent.TimeUnit;
15 | import java.util.concurrent.atomic.AtomicReference;
16 |
17 |
18 | /**
19 | * Created by baixiaokang on 18/2/27.
20 | *
21 | * 让客户端和服务端能互相发送和接受消息。
22 | *
23 | * 首相我们回想一下:
24 | *
25 | * 发送消息必须要得到远端的Binder对象来构造Messenger;
26 | * 处理消息必须新建一个Handler来构造Messenger;
27 | * 就以上这两点我们来重新写一下service端和Client端的代码:
28 | */
29 |
30 | public class MessengerService extends Service {
31 | public WorkThread mWorkThread;
32 | final CountDownLatch latch = new CountDownLatch(1);
33 | final AtomicReference resultRef = new AtomicReference<>();
34 |
35 | @Nullable
36 | @Override
37 | public IBinder onBind(Intent intent) {
38 | try {
39 | latch.await(10, TimeUnit.SECONDS); //最多等待10秒
40 | } catch (Exception e) { //等待中断
41 | e.printStackTrace();
42 | }
43 | Messenger mMessenger = resultRef.get();
44 | return mMessenger.getBinder();
45 | }
46 |
47 | @Override
48 | public void onCreate() {
49 | super.onCreate();
50 | LogUtils.i(Constants.TAG + " essengerService", "MessengerService -->onCreate");
51 | mWorkThread = new WorkThread();
52 | mWorkThread.start();
53 |
54 | }
55 |
56 | @Override
57 | public void onDestroy() {
58 | super.onDestroy();
59 | LogUtils.i(Constants.TAG + " essengerService", "MessengerService -->onDestroy quit");
60 | mWorkThread.quit();
61 | }
62 |
63 |
64 | public class WorkThread extends Thread {
65 | public ServiceHandler mHandler;
66 |
67 | @Override
68 | public void run() {
69 | Looper.prepare();
70 | LogUtils.i(Constants.TAG + " essengerService", "MessengerService -->new ServiceHandler");
71 | mHandler = new ServiceHandler();
72 | Messenger mMessenger = new Messenger(mHandler);
73 | // OkBus.getInstance().mServiceMessenger = mMessenger;
74 | try {
75 | resultRef.set(mMessenger);
76 | } catch (Exception e) {
77 | e.printStackTrace();
78 | } finally {
79 | latch.countDown();
80 | }
81 | Looper.loop();
82 | }
83 |
84 | public void quit() {
85 | mHandler.getLooper().quit();
86 | }
87 | }
88 | }
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/ServiceHandler.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | import android.os.Bundle;
4 | import android.os.Handler;
5 | import android.os.Message;
6 | import android.os.Messenger;
7 |
8 | import com.easy.moduler.lib.Constants;
9 | import com.easy.moduler.lib.utils.LogUtils;
10 |
11 | import java.util.Enumeration;
12 | import java.util.concurrent.ConcurrentHashMap;
13 |
14 |
15 | /**
16 | * Created by baixiaokang on 18/3/1.
17 | * 服务消息处理器
18 | *
19 | * a)、发送事件类型的消息
20 | * 1 、转发给自己的OkBus
21 | * 2.转发给其他模块的OkBus,来源模块除外
22 | *
23 | * b)、模块注册消息
24 | * 1、存储Client端接受处理消息的Messenger来发送Message到Client
25 | * 2、通知Client模块注册成功
26 | */
27 |
28 | public class ServiceHandler extends Handler {
29 | /**
30 | * 存储所有的模块id以及对应的客户端信使
31 | */
32 | private ConcurrentHashMap mClientMessengers = new ConcurrentHashMap<>();
33 |
34 | @Override
35 | public void handleMessage(Message msg) {
36 | super.handleMessage(msg);
37 | try {
38 | Bundle bundle = msg.getData();
39 | int registerId = bundle.getInt(Constants.REGISTER_ID, -1);
40 | if (registerId > 0) {//注册模块类型的消息
41 | LogUtils.logOnUI(Constants.TAG, "handleMessage: msg = [收到注册模块类型的消息]: registerId: " + Integer.toHexString(registerId));
42 |
43 | Messenger client = msg.replyTo;
44 | mClientMessengers.put(registerId, client);//存储Client端接受处理消息的Messenger来发送Message到Client
45 |
46 | Message data = Message.obtain();
47 | Bundle mBundle = new Bundle();
48 | mBundle.putInt(Constants.REGISTER_RES, Constants.REGISTER_SEC); //通知Client模块注册成功
49 | data.setData(mBundle);
50 | try {
51 | client.send(data);
52 | } catch (Exception e) {
53 | e.printStackTrace();
54 | }
55 | } else {//发送事件类型的消息
56 | String hex = Integer.toHexString(Math.abs(msg.what));
57 | LogUtils.i(Constants.TAG, "handleMessage: msg = [Service:收到" + (msg.what > 0 ? "普通" : "服务") + "类型的消息:" + hex + "]-->[转发给自己的OkBus]");
58 |
59 | //1、转发给自己的OkBus:
60 | OkBus.getInstance().onLocalEvent(msg.what, bundle.getSerializable(Constants.MESSAGE_DATA));
61 |
62 | //2.转发给其他模块的OkBus,来源模块除外
63 | Enumeration keys = mClientMessengers.keys();
64 | while (keys.hasMoreElements()) {
65 | int moduleId = (int) keys.nextElement();
66 | Messenger mMessenger = mClientMessengers.get(moduleId);
67 | if (moduleId != msg.arg1) {//不是目标来源模块,进行分发
68 | LogUtils.i(Constants.TAG, "handleMessage:转发给其他模块的OkBus: 消息Id-->: " + (msg.what > 0 ? "普通" : "服务") + hex + "消息 -->模块Id: " + Integer.toHexString(moduleId));
69 | Message _msg = Message.obtain(msg);
70 | try {
71 | mMessenger.send(_msg);
72 | } catch (Exception e) {
73 | e.printStackTrace();
74 | }
75 | }
76 | }
77 | }
78 | } catch (Exception e) {
79 | e.printStackTrace();
80 | LogUtils.logOnUI(Constants.TAG, "handleMessage:Exception");
81 | }
82 | }
83 | }
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/router/RouteUtils.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.router;
2 |
3 | import android.content.Intent;
4 | import android.net.Uri;
5 | import android.text.TextUtils;
6 |
7 | public class RouteUtils {
8 |
9 | /**
10 | * 判断intent来源是否和指定的rule匹配
11 | *
12 | * @param intent
13 | * @param rule
14 | * @return
15 | */
16 | public static boolean matchRule(Intent intent, String rule) {
17 | String stringUrl = getRouteUrl(intent);
18 | if (stringUrl == null || rule == null) {
19 | return false;
20 | }
21 |
22 | Uri parse = Uri.parse(stringUrl);
23 | Uri ruleParse = Uri.parse(rule);
24 |
25 | return TextUtils.equals(parse.getScheme(), ruleParse.getScheme())
26 | && TextUtils.equals(parse.getHost(), ruleParse.getHost())
27 | && TextUtils.equals(parse.getPath(), ruleParse.getPath());
28 | }
29 |
30 | /**
31 | * 获取传递过来的url
32 | *
33 | * @param intent
34 | * @return
35 | */
36 | public static String getRouteUrl(Intent intent) {
37 | String url = "";
38 | if (intent != null) {
39 | url = intent.getStringExtra(Router.INTENT_KEY_ROUTE_URL);
40 | }
41 | return url;
42 | }
43 |
44 | /**
45 | * 从intent中获取指定key对应的值
46 | *
47 | * @param intent
48 | * @param key
49 | * @param t 返回值类型
50 | * @param
51 | * @return
52 | */
53 | public static T getQueryParameter(Intent intent, String key, Class t) {
54 | String url = getRouteUrl(intent);
55 | return getQueryParameter(url, key, t);
56 | }
57 |
58 | /**
59 | * 获取指定key对应的值
60 | *
61 | * @param url
62 | * @param key
63 | * @param t
64 | * @param
65 | * @return
66 | */
67 | public static T getQueryParameter(String url, String key, Class t) {
68 | if (TextUtils.isEmpty(url) || TextUtils.isEmpty(key)) return getDefaultValue(t);
69 |
70 | Uri parse = Uri.parse(url);
71 | String str = parse.getQueryParameter(key);
72 | if (t == String.class) { // string
73 | return (T) str;
74 |
75 | } else if (t == Integer.class || t == int.class) { // int
76 | int result = -1;
77 | try {
78 | result = Integer.parseInt(str);
79 | } catch (Exception e) {
80 | e.printStackTrace();
81 | }
82 | return (T) Integer.valueOf(result);
83 |
84 | } else if (t == Long.class) { // long
85 | long result = -1;
86 | try {
87 | result = Long.parseLong(str);
88 | } catch (Exception e) {
89 | e.printStackTrace();
90 | }
91 | return (T) Long.valueOf(result);
92 |
93 | } else {
94 | throw new IllegalArgumentException(" unsupported parameter ! ");
95 | }
96 | }
97 |
98 | private static T getDefaultValue(Class t) {
99 | if (t == String.class) {
100 | return (T) "";
101 | } else if (t == Integer.class || t == int.class) {
102 | return (T) Integer.valueOf(-1);
103 | } else if (t == Long.class) {
104 | return (T) Long.valueOf(-1);
105 | }
106 | return (T) "";
107 | }
108 | }
109 |
110 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/BaseModule.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | import android.content.ComponentName;
4 | import android.content.ServiceConnection;
5 | import android.os.Handler;
6 | import android.os.IBinder;
7 | import android.os.Looper;
8 | import android.os.Message;
9 | import android.os.Messenger;
10 |
11 | import com.easy.moduler.annotation.Bus;
12 | import com.easy.moduler.lib.Constants;
13 | import com.easy.moduler.lib.router.Router;
14 | import com.easy.moduler.lib.utils.LogUtils;
15 |
16 | import java.util.concurrent.CountDownLatch;
17 | import java.util.concurrent.atomic.AtomicBoolean;
18 | import java.util.concurrent.atomic.AtomicReference;
19 |
20 | /**
21 | * Created by baixiaokang on 18/3/6.
22 | * 模块基类
23 | */
24 |
25 | public abstract class BaseModule implements IModule {
26 |
27 | public WorkThread mWorkThread;
28 |
29 | public AtomicBoolean isConnected = new AtomicBoolean(false);// 是否连接上服务器
30 |
31 | private BaseModule mBaseModule;
32 |
33 | private CountDownLatch latch;
34 | private AtomicReference resultRef;
35 |
36 | public void init(CountDownLatch latch, AtomicReference resultRef) {
37 | this.latch = latch;
38 | this.resultRef = resultRef;
39 | init();
40 | }
41 |
42 | @Override
43 | public void init() {
44 | mBaseModule = this;
45 | mWorkThread = new WorkThread();
46 | mWorkThread.start();
47 |
48 | OkBus.getInstance().register(Constants.ROUTER_OPEN_URL, new Event() {
49 | @Override
50 | public void call(Message msg) {
51 | String url = (String) msg.obj;
52 | Router.openLocalUrl(BaseAppModuleApp.getBaseApplication(), url);
53 | }
54 | }, Bus.UI);
55 | }
56 |
57 | public class WorkThread extends Thread {
58 | Handler mHandler;
59 | public Messenger clientHandler;
60 |
61 | @Override
62 | public void run() {
63 | Looper.prepare();
64 | mHandler = new ClientHandler();
65 | clientHandler = new Messenger(mHandler);
66 | if(resultRef!=null){
67 | try {
68 | resultRef.set(clientHandler);
69 | } catch (Exception e) {
70 | e.printStackTrace();
71 | } finally {
72 | latch.countDown();
73 | }
74 | }
75 | Looper.loop();
76 | }
77 |
78 | public void quit() {
79 | mHandler.getLooper().quit();
80 | }
81 | }
82 |
83 | public ServiceConnection mConnection = new ServiceConnection() {
84 | @Override
85 | public void onServiceConnected(ComponentName name, IBinder service) {
86 | LogUtils.logOnUI(Constants.TAG, "ServiceConnection-->onServiceConnected 已自动唤醒服务器");
87 | Messenger mServiceMessenger = new Messenger(service);
88 | OkBus.getInstance().initModule(mBaseModule, mServiceMessenger, getModuleId(), mWorkThread.clientHandler);
89 | afterConnected();
90 | }
91 |
92 |
93 | @Override
94 | public void onServiceDisconnected(ComponentName name) {
95 | isConnected.set(false);
96 | LogUtils.logOnUI(Constants.TAG, "ServiceConnection-->onServiceDisconnected 服务器断开");
97 | //BaseAppModuleApp.getBaseApplication().connectService();断开后自动重连
98 | }
99 | };
100 | }
101 |
--------------------------------------------------------------------------------
/apt/src/main/java/com/easy/moduler/apt/processor/RouterRuleProcessor.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.apt.processor;
2 |
3 |
4 | import com.easy.moduler.annotation.RouterRule;
5 | import com.easy.moduler.apt.AnnotationProcessor;
6 | import com.easy.moduler.apt.inter.IProcessor;
7 | import com.google.auto.service.AutoService;
8 | import com.squareup.javapoet.AnnotationSpec;
9 | import com.squareup.javapoet.ClassName;
10 | import com.squareup.javapoet.JavaFile;
11 | import com.squareup.javapoet.MethodSpec;
12 | import com.squareup.javapoet.ParameterSpec;
13 | import com.squareup.javapoet.ParameterizedTypeName;
14 | import com.squareup.javapoet.TypeSpec;
15 | import com.squareup.javapoet.WildcardTypeName;
16 |
17 | import java.io.IOException;
18 | import java.util.Map;
19 | import java.util.Set;
20 |
21 | import javax.annotation.processing.Messager;
22 | import javax.annotation.processing.RoundEnvironment;
23 | import javax.lang.model.element.Element;
24 | import javax.lang.model.element.ElementKind;
25 | import javax.lang.model.element.Modifier;
26 | import javax.tools.Diagnostic;
27 |
28 | import static com.easy.moduler.apt.AnnotationProcessor.KEY_MODULE_NAME;
29 |
30 | /**
31 | * routerRule注解处理器
32 | * Created by evan on 2017/6/7.
33 | */
34 | public class RouterRuleProcessor implements IProcessor {
35 |
36 | @Override
37 | public void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor) {
38 | try {
39 | ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(
40 | ClassName.get(Map.class),
41 | ClassName.get(String.class),
42 | ParameterizedTypeName.get(
43 | ClassName.get(Class.class),
44 | WildcardTypeName.subtypeOf(ClassName.get("android.app", "Activity"))
45 | )
46 | );
47 | ParameterSpec parameterSpec = ParameterSpec.builder(parameterizedTypeName, "rules")
48 | .build();
49 |
50 | MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder("initRule")
51 | .addAnnotation(Override.class)
52 | .addModifiers(Modifier.PUBLIC)
53 | .addParameter(parameterSpec);
54 |
55 | Set extends Element> elements = roundEnv.getElementsAnnotatedWith(RouterRule.class);
56 | if (elements == null || elements.size() == 0) {
57 | return;
58 | }
59 | for (Element element : elements) {
60 | if (element == null || element.getKind() != ElementKind.CLASS) {
61 | error(mAbstractProcessor.mMessager, "Only Classes can be annotated with " + RouterRule.class.getCanonicalName());
62 | return;
63 | }
64 |
65 | RouterRule routerRule = element.getAnnotation(RouterRule.class);
66 | String[] value = routerRule.value();
67 | if (value != null) {
68 | for (String url : value) {
69 | methodSpecBuilder.addStatement("rules.put($S, $T.class)", url, element);
70 | }
71 | }
72 | }
73 |
74 | MethodSpec methodSpec = methodSpecBuilder.build();
75 |
76 | String moduleName = mAbstractProcessor.mProcessingEnv.getOptions().get(KEY_MODULE_NAME);
77 | TypeSpec typeSpec = TypeSpec.classBuilder(moduleName + "_AutoRouterRuleCreator")
78 | .addModifiers(Modifier.PUBLIC)
79 | .addAnnotation(AnnotationSpec.builder(AutoService.class)
80 | .addMember("value", "$T.class", mAbstractProcessor.mElements.getTypeElement("com.easy.moduler.lib.router.IRouterRulesCreator"))
81 | .build())
82 | .addSuperinterface(ClassName.get(mAbstractProcessor.mElements.getTypeElement("com.easy.moduler.lib.router.IRouterRulesCreator")))
83 | .addMethod(methodSpec)
84 | .build();
85 |
86 | JavaFile.builder(getClass().getPackage().getName(), typeSpec).build().writeTo(mAbstractProcessor.mFiler);
87 |
88 | // elements.clear();
89 |
90 | } catch (IOException e) {
91 | e.printStackTrace();
92 | } catch (Exception e) {
93 | error(mAbstractProcessor.mMessager, e.getMessage());
94 | }
95 | }
96 |
97 | private void error(Messager messager, String error) {
98 | messager.printMessage(Diagnostic.Kind.ERROR, this.getClass().getCanonicalName() + " : " + error);
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/router/Router.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.router;
2 |
3 | import android.app.Activity;
4 | import android.app.Application;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.net.Uri;
8 |
9 | import com.easy.moduler.lib.Constants;
10 | import com.easy.moduler.lib.okbus.OkBus;
11 | import com.easy.moduler.lib.okbus.ServiceBus;
12 | import com.easy.moduler.lib.utils.StringUtils;
13 |
14 | import java.util.HashMap;
15 | import java.util.HashSet;
16 | import java.util.Iterator;
17 | import java.util.Map;
18 | import java.util.Set;
19 |
20 | /**
21 | * 自定义url路由
22 | *
23 | * 规则:
24 | * scheme host path params
25 | * app://app.com/login?liveid={liveid}&name={name}
26 | *
27 | * eg.
28 | * app://app.com/login?liveid=100&name=aaa
29 | *
30 | * usage:
31 | * 1、注册添加规则 {@link Router#addRouterRule(IRouterRulesCreator)}
32 | * 2、打开指定的url {@link Router#open(android.content.Context, java.lang.String)}
33 | *
34 | * Created by evan on 2017/6/2.
35 | */
36 |
37 | public class Router {
38 | public static final String INTENT_KEY_ROUTE_URL = "router_route_url";
39 | /**
40 | * 存储规则的集合
41 | * url --- activity clazz 映射
42 | */
43 | private static Map> rules = new HashMap<>();
44 | // private static Map interceptors = new HashMap<>(); // 拦截器不需要多个,统一在一个拦截器里实现
45 | private static IRouterInterceptor routerInterceptor;
46 | private static Set allRuleKeys = new HashSet<>();
47 |
48 | /**
49 | * 注册添加规则
50 | *
51 | * @param creator
52 | */
53 | public static void addRouterRule(IRouterRulesCreator creator) {
54 | creator.initRule(rules);
55 | allRuleKeys.addAll(rules.keySet());
56 | }
57 |
58 | public static void setInterceptor(IRouterInterceptor interceptor) {
59 | routerInterceptor = interceptor;
60 | }
61 |
62 | public static Intent getMatchIntent(Context context, String url) {
63 | String matchRuleKey = getMatchRuleKey(url);
64 | Class extends Activity> activityClazz = rules.get(matchRuleKey);
65 | if (activityClazz != null) {
66 | Intent intent = new Intent(context, activityClazz);
67 | intent.putExtra(INTENT_KEY_ROUTE_URL, url);
68 | return intent;
69 | }
70 | return null;
71 | }
72 |
73 | /**
74 | * 打开指定url
75 | *
76 | * @param url
77 | * @return
78 | */
79 | public static boolean open(Context context, String url) {
80 | if (OkBus.getInstance().isModule()) {
81 | if (!openLocalUrl(context, url)) {
82 | String module_name = RouteUtils.getQueryParameter(url, Constants.MODULE_NAME, String.class);
83 | ServiceBus.getInstance().noticeModule(module_name,0,url);
84 | return true;
85 | }
86 | return true;
87 | } else {
88 | return openLocalUrl(context, url);
89 | }
90 | }
91 |
92 |
93 | /**
94 | * 打开本地的指定url
95 | *
96 | * @param url
97 | * @return
98 | */
99 | public static boolean openLocalUrl(Context context, String url) {
100 | if (context == null || url == null) return false;
101 | try {
102 | // 1 是否注册url - class
103 | String matchRuleKey = getMatchRuleKey(url);
104 | Class extends Activity> activityClazz = rules.get(matchRuleKey);
105 | if (activityClazz != null) {
106 | // 2 匹配到已注册的url - class,构造intent
107 | Intent intent = new Intent(context, activityClazz);
108 | intent.putExtra(INTENT_KEY_ROUTE_URL, url);
109 | if (context instanceof Application) {
110 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111 | }
112 | // 3 拦截器处理
113 | if (routerInterceptor == null || !routerInterceptor.intercept(context, url, intent)) {
114 | // 4 无拦截器,或者拦截器未完全拦截,则启动对应的页面
115 | context.startActivity(intent);
116 | }
117 | return true;
118 | }
119 | } catch (Exception e) {
120 | e.printStackTrace();
121 | }
122 | return false;
123 | }
124 |
125 |
126 | private static String getMatchRuleKey(String url) {
127 | if (url == null) return "";
128 |
129 | Uri checkUrl = Uri.parse(url);
130 | String checkScheme = checkUrl.getScheme();
131 | String checkHost = checkUrl.getHost();
132 | String checkPath = checkUrl.getPath();
133 | Set checkParameterNames = checkUrl.getQueryParameterNames();
134 |
135 | Set ruleKeys = allRuleKeys;
136 | for (String ruleKey : ruleKeys) {
137 | Uri ruleUri = Uri.parse(ruleKey);
138 | if (!StringUtils.equalsIgnoreCase(ruleUri.getScheme(), checkScheme)
139 | || !StringUtils.equalsIgnoreCase(ruleUri.getHost(), checkHost)
140 | || !StringUtils.equalsIgnoreCase(ruleUri.getPath(), checkPath)) {
141 | continue;
142 | }
143 |
144 |
145 | Set queryParameterNames = ruleUri.getQueryParameterNames();
146 |
147 | Set ruleParameterNames = new HashSet<>(queryParameterNames); // queryParameterNames不可编辑
148 | Iterator iterator = ruleParameterNames.iterator();
149 | while (iterator.hasNext()) {
150 | String key = iterator.next();
151 | // 如果是可选key,则从规则中移除
152 | if (key != null && key.startsWith("[") && key.endsWith("]")) {
153 | iterator.remove();
154 | }
155 | }
156 |
157 | if (checkParameterNames.containsAll(ruleParameterNames)) {
158 | return ruleKey;
159 | }
160 | }
161 |
162 | return "";
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/utils/LogUtils.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.utils;
2 |
3 | import android.text.TextUtils;
4 | import android.util.Log;
5 |
6 | import com.easy.moduler.lib.Constants;
7 | import com.easy.moduler.lib.okbus.BaseAppModuleApp;
8 | import com.easy.moduler.lib.okbus.OkBus;
9 |
10 |
11 | public class LogUtils {
12 |
13 | private static boolean isDebug = true;
14 |
15 | static String mTag = "LogUtils";
16 |
17 | //for error log
18 | public static void error(String msg) {
19 | if (isDebug) {
20 | Log.e(mTag, msg);
21 | }
22 | }
23 |
24 | //for warming log
25 | public static void warn(String msg) {
26 | if (isDebug) {
27 | Log.w(mTag, msg);
28 | }
29 | }
30 |
31 | //for info log
32 | public static void info(String msg) {
33 | if (isDebug) {
34 | Log.i(mTag, msg);
35 | }
36 | }
37 |
38 | //for debug log
39 | public static void debug(String msg) {
40 | if (isDebug) {
41 | Log.d(mTag, msg);
42 | }
43 | }
44 |
45 | //for verbose log
46 | public static void verbose(String msg) {
47 | if (isDebug) {
48 | Log.v(mTag, msg);
49 | }
50 | }
51 |
52 | //for error log
53 | public static void e(String msg) {
54 | if (isDebug) {
55 | Log.e(mTag, msg);
56 | }
57 | }
58 |
59 | //for warming log
60 | public static void w(String msg) {
61 | if (isDebug) {
62 | Log.w(mTag, msg);
63 | }
64 | }
65 |
66 | //for info log
67 | public static void i(String msg) {
68 | if (isDebug) {
69 | Log.i(mTag, msg);
70 | }
71 | }
72 |
73 | //for debug log
74 | public static void logOnUI(String msg) {
75 | if (isDebug) {
76 | Log.d(mTag, msg);
77 | }
78 | }
79 |
80 | //for verbose log
81 | public static void v(String msg) {
82 | if (isDebug) {
83 | Log.v(mTag, msg);
84 | }
85 | }
86 |
87 |
88 | //for warming log
89 | public static void w(String tag, String msg) {
90 | if (isDebug) {
91 | if (tag == null || "".equalsIgnoreCase(tag.trim())) {
92 | tag = mTag;
93 | }
94 | Log.w(tag, msg);
95 | }
96 | }
97 |
98 | //for info log
99 | public static void i(String tag, String msg) {
100 | if (isDebug) {
101 | if (tag == null || "".equalsIgnoreCase(tag.trim())) {
102 | tag = mTag;
103 | }
104 | Log.i(tag, msg);
105 | }
106 | }
107 |
108 | //for debug log
109 | public static void logOnUI(String tag, String msg) {
110 | if (isDebug) {
111 | if (tag == null || "".equalsIgnoreCase(tag.trim())) {
112 | tag = mTag;
113 | }
114 | Log.d(tag, msg);
115 | if (TextUtils.equals(tag, Constants.TAG)) {
116 | String processName;
117 | if (OkBus.getInstance().isModule()) {
118 | processName = BaseAppModuleApp.getBaseApplication().getPackageName().split("_")[1];
119 | } else {
120 | processName = "app";
121 | }
122 | OkBus.getInstance().onEvent(Constants.MODULE_PRINT_LOG, System.nanoTime() + " " +
123 | processName + " : " + msg + "\n");
124 | }
125 | }
126 | }
127 |
128 |
129 | //for debug log
130 | public static void d(String tag, String msg) {
131 | if (isDebug) {
132 | if (tag == null || "".equalsIgnoreCase(tag.trim())) {
133 | tag = mTag;
134 | }
135 | Log.d(tag, msg);
136 | }
137 | }
138 |
139 | //for verbose log
140 | public static void v(String tag, String msg) {
141 | if (isDebug) {
142 | if (tag == null || "".equalsIgnoreCase(tag.trim())) {
143 | tag = mTag;
144 | }
145 | Log.v(tag, msg);
146 | }
147 | }
148 |
149 | //for verbose log
150 | public static void e(String tag, String msg) {
151 | if (isDebug) {
152 | if (tag == null || "".equalsIgnoreCase(tag.trim())) {
153 | tag = mTag;
154 | }
155 | Log.e(tag, msg);
156 | }
157 | }
158 |
159 | public static void setDebug(boolean isDebug) {
160 | LogUtils.isDebug = isDebug;
161 | }
162 |
163 | public static boolean isDebug() {
164 | return isDebug;
165 | }
166 |
167 | /**
168 | * 点击Log跳转到指定源码位置
169 | *
170 | * @param tag
171 | * @param msg
172 | */
173 | public static void showLog(String tag, String msg) {
174 | if (isDebug && !TextUtils.isEmpty(msg)) {
175 | if (TextUtils.isEmpty(tag)) tag = mTag;
176 | StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
177 | int currentIndex = -1;
178 | for (int i = 0; i < stackTraceElement.length; i++) {
179 | if (stackTraceElement[i].getMethodName().compareTo("showLog") == 0) {
180 | currentIndex = i + 1;
181 | break;
182 | }
183 | }
184 | if (currentIndex >= 0) {
185 | String fullClassName = stackTraceElement[currentIndex].getClassName();
186 | String className = fullClassName.substring(fullClassName
187 | .lastIndexOf(".") + 1);
188 | String methodName = stackTraceElement[currentIndex].getMethodName();
189 | String lineNumber = String
190 | .valueOf(stackTraceElement[currentIndex].getLineNumber());
191 |
192 | Log.i(tag, msg + "\n ---->at " + className + "." + methodName + "("
193 | + className + ".java:" + lineNumber + ")");
194 | } else {
195 | Log.i(tag, msg);
196 | }
197 | }
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/appbox/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/module_a/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/module_b/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/module_service/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/调研.MD:
--------------------------------------------------------------------------------
1 | ##1、百链 CC https://github.com/luckybilly/CC/wiki
2 | ####核心代码量:3K(±) line (&插件)
3 | ####源码阅读难度(10分制):8(Socket&ASM)
4 |
5 | ####组件自动注册方案: 自动注册
6 | TrasnformAPI + ASM扫描组件类(IComponent接口实现类)并注册到ComponentMananger中,
7 |
8 | 无需手动维护组件列表
9 |
10 | ####模块间通信机制:
11 | 事件总线
12 |
13 | ####组建间通信机制: Socket
14 | 广播 + Service + LocalSocket
15 |
16 | ####组件向外提供服务: 自动注册
17 | 在IComponent中实现,自动注册到ComponentMananger
18 |
19 | ####组件依赖隔离:
20 | 无需依赖、完全隔离
21 |
22 | ####特点:
23 | 1. 可以跨app调用,初期改造时即可单独编译组件运行
24 | 2. 提供统一的组件调用及实现方式(不管是否跨app调用、页面跳转、服务调用、同步/异步调用)
25 | 3. 组件自动注册,无需维护
26 | 4. 提供了ActionProcessor按需加载的支持
27 |
28 | ####综合成本:中
29 | 侵入性低
30 | 无混淆
31 | 改造成本中
32 | 学习成本低
33 | 维护成本中
34 |
35 | ####缺点:
36 | 1、使用Socket实现组件间通信,逻辑略复杂,且只支持string
37 | 2、Socket未使用NIO,只能一对一通信
38 | 3、组件与非组件状态下通信机制不统一
39 | 4、无法自动唤醒目标,目标组件被杀后无法通信
40 |
41 |
42 | ##2、得到 DDComponentForAndroid https://www.jianshu.com/p/1b1d77f58e84
43 |
44 | ####核心代码量:1.2K(±) line (&插件)
45 | ####源码阅读难度:5(Javassisit)
46 |
47 | ####组件自动注册方案: 自动注册
48 |
49 | apt生成各module的路由表
50 |
51 | TrasnformAPI + javassist将IApplicationLike的注册代码生成到自定义application.onCreate方法中,
52 |
53 | 无需手动维护组件列表
54 |
55 | ####模块间通信机制:
56 | 路由 + 接口下沉
57 |
58 | ####组建间通信机制:
59 | 不支持
60 |
61 | ####组件向外提供服务: 手动注册
62 | 接口下沉到base中,组件中实现接口并在IApplicationLike中手动注册到Router中
63 |
64 | ####组件依赖隔离:
65 | 通过插件实现只在打apk包时才添加依赖,编码期间不能直接调用其它组件的代码
66 |
67 | ####特点:
68 | 自动添加依赖,只在运行assemble任务的才会添加依赖,因此在开发期间组件之间是完全感知不到的,这是做到完全隔离的关键
69 | 支持两种语法:module或者groupId:artifactId:version(@aar),前者之间引用module工程,后者使用maven中已经发布的aar
70 |
71 | 1. 编码期间组件依赖通过插件进行隔离,避免直接调用其它组件的代码
72 | 2. 提供了兼容ARouter的方案
73 | 3. 组件自动注册,无需维护
74 |
75 | ####综合成本:中
76 | 侵入性一般
77 | 混淆所有下沉接口、框架中相关接口的实现类等
78 | 改造成本一般
79 | 学习成本一般
80 | 维护成本一般
81 |
82 |
83 | ####缺点:
84 | 1、不支持组件间的跨进程通信,这个期望后续能有进展
85 | 2、javassisit经常会占用代码资源无法释放,导致编译失败(mac下不会,wins下经常,无解)
86 | 3、组件与组件联合测试时需要集成,而不是两个独立的组件通信,多个组件联合测试时,需要打到同一个包
87 | 4、接口暴涨
88 |
89 |
90 | ##3、58赶集 ModularizationArchitecture http://blog.spinytech.com/2016/12/28/android_modularization/
91 |
92 | ####核心代码量:2K(±) line
93 | ####源码阅读难度:7(AIDL)
94 |
95 | ####组件自动注册方案: 手动注册
96 |
97 | 1. Action列表在其所属的Provider中注册
98 | 2. Provider在其所属的ApplicationLogic中注册
99 | 3. ApplicationLogic在主app的Application中注册
100 |
101 | ####模块间通信机制:
102 | 事件总线
103 |
104 | ####组建间通信机制: AIDL
105 | 组件同时安装在设备上即可,实际开发中一般是当前正在开发的组件和主app中的组件互相调用.
106 |
107 | ####组件向外提供服务: 手动注册
108 | 实现一个对应的Action并在其所属的Provider中手动注册
109 |
110 | ####组件依赖隔离:
111 | 无需依赖、完全隔离
112 |
113 | ####特点:
114 | 1. 可以跨app、app内跨进程调用
115 | 2. 组件运行在各自进程中,单独运行与联合打包切换时需要修改进程名称
116 | 3. 组件需指定同步实现还是异步实现,调用组件时统一拿到RouterResponse作为返回值,可以自行决定同步还是异步方式调用RouterResponse.getData()来获取结果,但异步获取时需要自己维护线程
117 |
118 | ####综合成本:高
119 | 侵入性高
120 | 混淆所有下沉接口、框架中相关接口的实现类等
121 | 改造成本高
122 | 学习成本高
123 | 维护成本高
124 |
125 |
126 | ####缺点:
127 | 1、使用AIDL实现组件间通信,逻辑略复杂
128 | 2、组件与非组件状态下通信机制不统一
129 |
130 |
131 | ##4、阿里Arouter https://yq.aliyun.com/articles/71687?spm=5176.100240.searchblog.7.8os9Go
132 |
133 | ####组件自动注册方案: 半自动+反射
134 | 1. apt生成各module的路由表
135 | 2. Arouter初始化时扫描所有dex找出指定包名下的路由表,通过反射进行统一注册
136 |
137 | ####模块间通信机制:
138 | 路由 + 接口下沉
139 |
140 | ####组建间通信机制:
141 | urlScheme来统一转发
142 |
143 | ####组件向外提供服务: 反射
144 | 接口继承IProvider并下沉到base中,组件中实现接口并通过注解来暴露服务
145 |
146 | ####组件依赖隔离:
147 | 未隔离
148 |
149 | ####特点:
150 | 1. 阿里出品,使用者众多,QQ群里交流比较活跃
151 | 2. 自动注册插件正式启用之前扫描所有dex完成注册的方式效率较低且有加固厂商兼容性问题
152 | 3. 分级按需加载
153 |
154 | ####综合成本:一般
155 | 侵入性高
156 | 混淆框架中的所有类及框架相关接口的实现类
157 | 改造成本一般
158 | 学习成本一般
159 | 维护成本低
160 |
161 |
162 |
163 | ##5、美团 https://www.jianshu.com/p/d372cc6802e5
164 |
165 | ####组件自动注册方案: SPI+javassit
166 | 1. 使用servieloader进行解耦---非显式的调用服务实现类
167 | 2. javassit预处理:大体流程是
168 | 在build的某个阶段拿到所有编译后的class文件(夹)和jar包。
169 | 使用javassit确定哪些类被@autoService修饰,配置文件中如果不存在,在其添加。
170 | 查看serviceConfig配置文件里面的格式是不是正确。
171 | 通过javassit来确定serviceConfig配置文件里面的类是不是在项目中存在,接口类是不是实现了Iprovider接口。
172 |
173 | ####模块间通信机制:
174 | LocalBroadcast取代eventbus
175 |
176 | ####组建间通信机制:
177 | contentProvider
178 |
179 | ####组件向外提供服务: 反射
180 | 接口继承IProvider并下沉到base中,组件中实现接口并通过注解来暴露服务
181 |
182 | ####组件依赖隔离:
183 | 无需依赖、完全隔离
184 |
185 | ####特点:
186 | 1. 解耦使用serviceloader,而不是路由进行
187 | 2. 可以无侵入式的配置各种服务
188 | 3. lib快速便捷多端使用
189 |
190 | ####综合成本:高
191 | 侵入性低
192 | 混淆框架中的所有类及框架相关接口的实现类
193 | 改造成本高
194 | 学习成本高
195 | 维护成本高
196 |
197 |
198 | ##6、其它
199 |
200 | ###聚美Router https://juejin.im/post/5a4b4425518825128654eef4
201 | APT+反射
202 |
203 | ###51信用卡路由方案OkDeepLink https://www.jianshu.com/p/8a3eeeaf01e8
204 | 使用aspectJ来实现路由表的自动注册
205 |
206 | ###美柚路由方案RouterKit https://github.com/gybin02/RouterKit
207 | 通过apt生成每个module的路由表,然后复制到app的assets目录,
208 | 运行的时候遍历asset目录,反射对应的activity
209 |
210 |
211 | ##7、思考总结
212 | ####1、组件自动注册方案:
213 | 现有APT实现的Router 只需要完成中央注册即可
214 | 可选方案复杂度:10分制
215 | javassisit 7
216 | ASM 8
217 | SPI 0
218 | AspectJ 9
219 | 反射 6
220 |
221 |
222 | ####2、组件间通信方案:
223 | 可选方案复杂度:
224 | ContentProvider 7
225 | Socket 8
226 | AIDL 7
227 | Messenger 3
228 |
229 | 由于现在的组建内通信方案OkBus 传递的是Message
230 |
231 | 进程间通信可以使用Messenger(IPC)来扩展OkBus实现组建间的Message的双向传递 几乎0成本
232 |
233 |
234 |
235 |
236 | ##8、最终实现 [Moduler](https://github.com/north2016/Moduler)
237 |
238 | 原理篇: https://www.jianshu.com/p/a73fd5e4cad1
239 |
240 |
241 | ####核心代码量:1K(±) line
242 | ####源码阅读难度(10分制):3(Messenger)
243 |
244 | ####组件自动注册方案: 自动注册
245 | APT+SPI
246 |
247 | ####模块间通信机制:
248 | OkBus
249 |
250 | ####组建间通信机制: Messenger扩展的 OkBus
251 | OkBus
252 |
253 | ####组件向外提供服务: 自动注册
254 | OkBus+SPI
255 |
256 | ####组件依赖隔离:
257 | 无需依赖、完全隔离
258 |
259 | ####特点:可插拔,按需加载,自动唤醒
260 | 1、最小代价组件化,最简单的配置、最灵活的切换,组件可插拔
261 | 2、组件路由自动化注册,中央路由自动化采集
262 | 3、服务自动注册,兼容同异步,兼容跨/同进程,经典C/S架构,一对多通信
263 | 4、OkBus实现的通信机制,兼容同异步,兼容跨/同进程,与传统使用方式完全一样,无感知无差别
264 | 5、跨组件调用时自动唤醒,单个组件调试时无需手动打开目标组件,即使目标开启后被杀掉进程,同样可以唤醒加通信一步到位
265 |
266 |
267 |
268 | ####综合成本:低
269 | 侵入性低
270 | 无混淆
271 | 改造成本低
272 | 学习成本低
273 | 维护成本低
274 |
275 | ###QQ群:AndroidMVP [555343041](http://shang.qq.com/wpa/qunwpa?idkey=14f9009a0276624f6abf3221fe131c57ff05b70b5b4b922ed2c4aa4156155e73)
276 |
277 | ###参考&致谢:
278 | [总结一波安卓组件化开源方案](https://juejin.im/entry/5a82bbd85188257a8462392c)
279 |
280 | [美团猫眼电影android模块化实战](https://juejin.im/entry/5a6940c651882573385feb0c)
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/ServiceBus.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | import android.content.ComponentName;
4 | import android.content.Intent;
5 | import android.content.ServiceConnection;
6 | import android.os.Bundle;
7 | import android.os.IBinder;
8 | import android.os.Message;
9 | import android.os.Messenger;
10 | import android.text.TextUtils;
11 |
12 | import com.easy.moduler.lib.Constants;
13 | import com.easy.moduler.lib.utils.LogUtils;
14 |
15 | import java.util.concurrent.CountDownLatch;
16 | import java.util.concurrent.ExecutorService;
17 | import java.util.concurrent.Executors;
18 | import java.util.concurrent.TimeUnit;
19 | import java.util.concurrent.atomic.AtomicReference;
20 |
21 | import static android.content.Context.BIND_AUTO_CREATE;
22 |
23 | /**
24 | * Created by baixiaokang on 18/3/2.
25 | * 服务总线
26 | */
27 | @SuppressWarnings("unchecked")
28 | public class ServiceBus {
29 |
30 | private OkBus okBus;
31 |
32 | private static class Holder {
33 | public static final ServiceBus instance = new ServiceBus();
34 | }
35 |
36 | public static ServiceBus getInstance() {
37 | return Holder.instance;
38 | }
39 |
40 | private ServiceBus() {
41 | okBus = OkBus.getInstance();
42 | service = Executors.newFixedThreadPool(threadNum);
43 | }
44 |
45 |
46 | private ExecutorService service;
47 | private static final int threadNum = 5;//执行任务的子线程数量
48 |
49 | /**
50 | * 异步调用服务
51 | *
52 | * @param serviceId 服务id
53 | * @param callback 回调
54 | */
55 | public void fetchService(final int serviceId, final Event callback) {
56 | if (serviceId > 0 || serviceId % 2 == 0) {
57 | assert false : "请求ID必须是负奇值!";
58 | return;
59 | }
60 | if (okBus.isModule() && !okBus.isModuleConnected()) {
61 | LogUtils.logOnUI(Constants.TAG, "请求失败,服务已经断开链接,尝试重新打开服务,进行请求");
62 | BaseAppModuleApp.getBaseApplication().connectService();
63 | return;
64 | }
65 |
66 | //自动唤醒目标进程
67 | if (okBus.isModule()) {
68 | String module_name = Integer.toHexString(Math.abs(serviceId)).substring(0, 1);
69 | noticeModule(module_name, serviceId, null);
70 | }
71 |
72 | //1、先注册回调
73 | okBus.register(serviceId - 1, msg -> {
74 | callback.call(msg);
75 | okBus.unRegister(serviceId - 1);//服务是单次调用,触发后即取消注册
76 | });
77 | //2、通知目标模块
78 | okBus.onEvent(serviceId);
79 | }
80 |
81 |
82 | /**
83 | * 唤醒目标进程
84 | *
85 | * @param module_name 模块名
86 | * @param serviceId 服务ID
87 | * @param url 要打开的url
88 | */
89 | public void noticeModule(String module_name, int serviceId, String url) {
90 | Intent ait = new Intent(NoticeService.class.getCanonicalName());// 5.0+ need explicit intent //唤醒目标进程的服务Action名
91 | ait.setPackage(Constants.MODULE_PACKAGE_PRE + module_name); // the package name of Remote Service //唤醒目标进程的包名
92 | BaseAppModuleApp.getBaseApplication().bindService(ait, new ServiceConnection() {
93 | @Override
94 | public void onServiceConnected(ComponentName name, IBinder service) {
95 | if (service != null) {
96 | LogUtils.logOnUI(Constants.TAG, "已经自动唤醒" + module_name);
97 | Messenger moduleNameMessenger = new Messenger(service);
98 | Message _msg = Message.obtain();
99 | Bundle _data = new Bundle();
100 | _data.putBoolean(Constants.NOTICE_MSG, true);
101 | _msg.setData(_data);
102 | _msg.replyTo = okBus.mServiceMessenger;//把服务器的信使给目标组件的信使,让他俩自己联系,这里仅仅是通知
103 | try {
104 | moduleNameMessenger.send(_msg);
105 | } catch (Exception e) {
106 | e.printStackTrace();
107 | }
108 |
109 | try {
110 | Thread.sleep(200);//给服务器和目标组件500ms联系的时间
111 | } catch (Exception e) {
112 | e.printStackTrace();
113 | }
114 |
115 | } else {
116 | LogUtils.logOnUI(Constants.TAG, module_name + "进程,本来就是醒的");
117 | }
118 |
119 | if (serviceId < 0) { //唤醒成功,继续发送异步请求,通知目标模块
120 | okBus.onEvent(serviceId);
121 | }
122 | if (!TextUtils.isEmpty(url)) { //目标url不为空,继续打开目标
123 | OkBus.getInstance().onEvent(Constants.ROUTER_OPEN_URL, url);
124 | }
125 | }
126 |
127 | @Override
128 | public void onServiceDisconnected(ComponentName name) {
129 | LogUtils.logOnUI(Constants.TAG, "自动唤醒目标进程失败 module_name:" + module_name);
130 | }
131 | }, BIND_AUTO_CREATE);
132 | }
133 |
134 |
135 | /**
136 | * 同步调用服务
137 | *
138 | * @param serviceId
139 | * @param
140 | * @return
141 | */
142 | public T fetchService(int serviceId) {
143 | return fetchService(serviceId, 3);
144 | }
145 |
146 | /**
147 | * 同步调用服务
148 | *
149 | * @param serviceId 服务ID
150 | * @param timeout 超时时间
151 | * @return
152 | */
153 | public synchronized T fetchService(final int serviceId, int timeout) {
154 | if (okBus.isModule() && !okBus.isModuleConnected()) {
155 | LogUtils.i(Constants.TAG, "请求失败,服务已经断开链接,尝试重新打开服务,进行请求");
156 | BaseAppModuleApp.getBaseApplication().connectService();
157 | return null;
158 | }
159 | final CountDownLatch latch = new CountDownLatch(1);
160 | final AtomicReference resultRef = new AtomicReference<>();
161 | service.execute(new Runnable() {
162 | @Override
163 | public void run() {
164 | fetchService(serviceId, new Event() {
165 | @Override
166 | public void call(Message msg) {
167 | try {
168 | resultRef.set((T) msg.obj);
169 | } catch (Exception e) {
170 | e.printStackTrace();
171 | } finally {
172 | latch.countDown();
173 | }
174 | }
175 | });
176 | }
177 | });
178 | try {
179 | latch.await(timeout, TimeUnit.SECONDS); //最多等待timeout秒
180 | } catch (Exception e) { //等待中断
181 | e.printStackTrace();
182 | }
183 | return resultRef.get();
184 | }
185 |
186 |
187 | /**
188 | * 注册服务
189 | *
190 | * @param serviceId 服务id
191 | * @param callback 服务调用的回调
192 | * @param 服务返回的数据范型
193 | */
194 | public void registerService(final int serviceId, final CallBack callback) {
195 | LogUtils.logOnUI(Constants.TAG, "注册服务 " + Integer.toHexString(Math.abs(serviceId)));
196 | okBus.unRegister(serviceId);//服务提供者只能有一个
197 | okBus.register(serviceId, msg -> {
198 | //TODO 优化到子线程
199 | okBus.onEvent(serviceId - 1, callback.onCall(msg));
200 | });
201 | }
202 | }
203 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/easy/moduler/lib/okbus/OkBus.java:
--------------------------------------------------------------------------------
1 | package com.easy.moduler.lib.okbus;
2 |
3 | import android.os.Bundle;
4 | import android.os.Handler;
5 | import android.os.Looper;
6 | import android.os.Message;
7 | import android.os.Messenger;
8 | import android.util.SparseArray;
9 |
10 | import com.easy.moduler.annotation.Bus;
11 | import com.easy.moduler.lib.Constants;
12 | import com.easy.moduler.lib.utils.LogUtils;
13 |
14 | import java.io.Serializable;
15 | import java.util.Enumeration;
16 | import java.util.concurrent.ConcurrentHashMap;
17 | import java.util.concurrent.CopyOnWriteArrayList;
18 | import java.util.concurrent.Executors;
19 | import java.util.concurrent.ScheduledExecutorService;
20 | import java.util.concurrent.atomic.AtomicBoolean;
21 |
22 | /**
23 | * Created by baixiaokang on 16/11/15.
24 | */
25 | @SuppressWarnings("unchecked")
26 | public class OkBus {
27 | private ConcurrentHashMap>> mEventList = new ConcurrentHashMap<>();//存储所有事件ID以及其回调
28 | private ConcurrentHashMap mStickyEventList = new ConcurrentHashMap<>();//存储粘连事件ID以及其数据
29 | private ScheduledExecutorService mPool = Executors.newScheduledThreadPool(5);
30 | private Handler mHandler = new Handler(Looper.getMainLooper());
31 |
32 | private OkBus() {
33 | }
34 |
35 | private static class Holder {
36 | public static OkBus eb = new OkBus();
37 | }
38 |
39 | public static OkBus getInstance() {
40 | return Holder.eb;
41 | }
42 |
43 | public OkBus register(int tag, Event ev) {
44 | register(tag, ev, Bus.DEFAULT);
45 | return this;
46 | }
47 |
48 | public OkBus register(int tag, final Event ev, int thread) {
49 | SparseArray mEvent = new SparseArray<>();
50 | mEvent.put(thread, ev);
51 | if (mEventList.get(tag) != null) {
52 | mEventList.get(tag).add(mEvent);
53 | } else {
54 | CopyOnWriteArrayList> mList = new CopyOnWriteArrayList<>();
55 | mList.add(mEvent);
56 | mEventList.put(tag, mList);
57 | }
58 | LogUtils.i(Constants.TAG, "Bus register " + tag + " :" + mEventList.get(tag).size());
59 | if (mStickyEventList.get(tag) != null) {//注册时分发粘连事件
60 | final Message msg = Message.obtain();
61 | msg.obj = mStickyEventList.get(tag);
62 | msg.what = tag;
63 | callEvent(msg, ev, thread);
64 | LogUtils.i(Constants.TAG, "mStickyEvent register and onEvent " + tag + " :" + mEventList.get(tag).size());
65 | }
66 | return this;
67 | }
68 |
69 | private void callEvent(final Message msg, final Event ev, int thread) {
70 | switch (thread) {
71 | case Bus.DEFAULT:
72 | ev.call(msg);
73 | break;
74 | case Bus.UI:
75 | mHandler.post(() -> ev.call(msg));
76 | break;
77 | case Bus.BG:
78 | mPool.execute(() -> ev.call(msg));
79 | break;
80 | }
81 | }
82 |
83 | /**
84 | * 一次性注销所有当前事件监听器
85 | *
86 | * @param ev
87 | * @return
88 | */
89 | public OkBus unRegister(Event ev) {
90 | Enumeration keys = mEventList.keys();
91 | while (keys.hasMoreElements()) {
92 | int key = (int) keys.nextElement();
93 | CopyOnWriteArrayList> list = mEventList.get(key);
94 | for (SparseArray item : list) {
95 | if (item.indexOfValue(ev) >= 0) {
96 | list.remove(item);
97 | LogUtils.i(Constants.TAG, "remove Event " + "key :" + key + " keys:" + item.toString());
98 | }
99 | }
100 | }
101 | return this;
102 | }
103 |
104 | public OkBus unRegister(int tag) {
105 | if (mEventList.get(tag) != null)
106 | mEventList.remove(tag);
107 | return this;
108 | }
109 |
110 |
111 | public OkBus onEvent(int tag) {
112 | onEvent(tag, null);
113 | return this;
114 | }
115 |
116 | public OkBus onStickyEvent(int tag, Object data) {
117 | LogUtils.i(Constants.TAG, "Bus onStickyEvent " + tag + " ");
118 | mStickyEventList.put(tag, (data == null ? tag : data));
119 | onEvent(tag, data);
120 | return this;
121 | }
122 |
123 | public OkBus onStickyEvent(int tag) {
124 | onStickyEvent(tag, null);
125 | return this;
126 | }
127 |
128 | /**
129 | * @param tag 发送消息的事件ID
130 | * @param data 发送消息的数据
131 | * @return
132 | */
133 | public OkBus onEvent(int tag, Object data) {
134 |
135 | String hex = Integer.toHexString(Math.abs(tag));
136 | LogUtils.i("Message OkBus", "onEvent " + (tag > 0 ? "[普通]" : "[服务]") + " tag: " + hex);
137 |
138 | //1、本地先处理非服务消息
139 | if (tag >= 0) onLocalEvent(tag, data);
140 |
141 | //2、如果是组建化,向服务器发消息
142 | if (isModule.get()) {
143 | if (!isModuleConnected()) {
144 | LogUtils.i("Message OkBus", "发消息失败,服务已经断开链接,尝试重新打开服务,进行发消息");
145 | BaseAppModuleApp.getBaseApplication().connectService();
146 | return this;
147 | }
148 | if (data == null || data instanceof Serializable) {
149 | Message newMsg = new Message();
150 | if (data != null) {
151 | Bundle bundle = new Bundle();
152 | bundle.putSerializable(Constants.MESSAGE_DATA, (Serializable) data);
153 | newMsg.setData(bundle);
154 | }
155 | newMsg.arg1 = mModuleId;
156 | newMsg.what = tag;
157 | try {
158 | mServiceMessenger.send(newMsg);
159 | } catch (Exception e) {
160 | e.printStackTrace();
161 | }
162 | } else {
163 | assert false : "跨进程时,你传递的对象没有序列化!";
164 | }
165 | } else if (tag < 0) {//非组件化时本地处理服务消息
166 | onLocalEvent(tag, data);
167 | }
168 | return this;
169 | }
170 |
171 | /**
172 | * 触发本地消息事件
173 | *
174 | * @param tag
175 | * @param data
176 | */
177 | public void onLocalEvent(int tag, Object data) {
178 | Message msg = Message.obtain();
179 | msg.obj = data;
180 | msg.what = tag;
181 | //1、本地先处理消息
182 |
183 | CopyOnWriteArrayList> mEvents = mEventList.get(tag);
184 | if (mEvents != null) {
185 | LogUtils.i(Constants.TAG + " OkBus", "Bus onEvent " + tag + " :" + mEvents.size());
186 | for (SparseArray ev : mEvents)
187 | callEvent(msg, ev.valueAt(0), ev.keyAt(0));
188 | }
189 | }
190 |
191 |
192 | private AtomicBoolean isModule = new AtomicBoolean(false);// 是否是模块化
193 | public Messenger mServiceMessenger;
194 | private BaseModule mBaseModule;
195 | public int mModuleId;
196 |
197 | public void initModule(BaseModule mBaseModule, Messenger mServiceMessenger, int mModuleId, Messenger mClientMessenger) {
198 | this.mServiceMessenger = mServiceMessenger;
199 | this.mModuleId = mModuleId;
200 | this.mBaseModule = mBaseModule;
201 | isModule.set(true);
202 | mBaseModule.isConnected.set(true);
203 |
204 | Message msg = Message.obtain();
205 | Bundle data = new Bundle();
206 | data.putInt(Constants.REGISTER_ID, mModuleId);//注册模块
207 | msg.setData(data);
208 | msg.replyTo = mClientMessenger; //将处理消息的Messenger绑定到消息上带到服务端
209 | try {
210 | mServiceMessenger.send(msg);
211 | } catch (Exception e) {
212 | e.printStackTrace();
213 | }
214 | }
215 |
216 | public boolean isModule() {
217 | return isModule.get();
218 | }
219 |
220 | public boolean isModuleConnected() {
221 | return mBaseModule.isConnected.get();
222 | }
223 | }
224 |
--------------------------------------------------------------------------------
/README.MD:
--------------------------------------------------------------------------------
1 | 关于组件化,相信很多人耳熟能详,网上的组件化框架也如雨后春笋,最近做了一些 [组件化调研](https://www.jianshu.com/p/0d45f2a894ba),开始着手探索适合自己项目的一条组件化之路,在此分享一下,欢迎指正交流。
2 |
3 | ###组件化之核心技术点
4 |
5 | 在阅读了大部分组件化相关的文章和框架之后,大致能总结出以下几点:
6 | 1、组件的路由的注册和中央路由的采集
7 | 2、组件间的通信(跨进程)和打包之后的模块间通信(同进程),以及其兼容(跨/同进程)
8 | 3、组件向外提供服务,以及组建间的服务的同异步互调,以及其兼容(跨/同进程)
9 |
10 |
11 | 我们的目标以及达到的成果:
12 |
13 | 1、最小代价组件化,最简单的配置、最灵活的切换
14 | 2、组件路由自动化注册,中央路由自动化采集
15 | 3、服务自动注册,兼容同异步,兼容跨/同进程
16 | 4、OkBus实现的通信机制,兼容同异步,兼容跨/同进程,与传统使用方式完全 一样,无感知无差别
17 | 5、跨组件调用时自动唤醒,单个组件调试时无需手动打开目标组件,即使目标开启后被杀掉进程,同样可以唤醒加通信一步到位
18 | 6、代码量少的可怜,实在一句多余的代码都不想写的懒人体验
19 |
20 |
21 |
22 |
23 |
24 | ###一、组建自动注册和中央路由自动收集APT+SPI
25 |
26 | ####第一步,路由Map自动化注册交给(APT)annotationProcessor
27 |
28 | 具体细节无需多言,注解标识目标url+注解处理器集中处理生成代码,借助javapoet和auto-service,实现自动注册路由到组件Map。
29 |
30 |
31 | 注意、这一步,生成的代码类都会被标注上@AutoService,成为路由注册服务的提供者
32 |
33 | ####第二步,中央路由采用SPI自动收集
34 |
35 | Java提供的SPI全名就是Service Provider Interface,下面是一段官方的解释,,其实就是为某个接口寻找服务的机制,有点类似IOC的思想,将装配的控制权移交给ServiceLoader。
36 |
37 | SPI在平时我们用到的会比较少,但是在Android模块开发中就会比较有用,不同的模块可以基于接口编程,每个模块有不同的实现service provider,然后通过SPI机制自动注册到一个配置文件中,就可以实现在程序运行时扫描加载同一接口的不同service provider。这样模块之间不会基于实现类硬编码,可插拔。
38 |
39 |
40 | 注上@AutoService的接口实现类,会在META-INF下自动生成接口的服务实现列表
41 | 
42 |
43 |
44 | 在最终打包的Application自动采集子组件的路由器:
45 | ```
46 | ServiceLoader loader = ServiceLoader.load(IRouterRulesCreator.class);
47 | for (IRouterRulesCreator rules : loader) Router.addRouterRule(rules);
48 |
49 | ```
50 |
51 | 在独立运行的组件Application也是如此。
52 |
53 | ####第三步,Messager扩展OkBus实现跨/同进程的无差别操作
54 |
55 | 在未组件化之前,使用OkBus的APP架构图为:
56 |
57 | 
58 |
59 |
60 | 在同一进程中,使用OKBus在不同模块间传递Message数据。
61 |
62 |
63 | 组件化之后的架构图为:
64 |
65 |
66 | 
67 |
68 |
69 |
70 | 特点:
71 |
72 | 1、组件化和非组件化对OkBus来说使用方式完全一样,无感知无差别,旧代码基本不用改
73 | 2、Messenger 相对于ContentProvider、Socket、AIDL。操作最简单,它是对AIDL的Message传递做了封装,Message可以作为任何序列数据的载体
74 | 3、只有一个服务器,再多组件整体架构也不冗乱
75 | 4、自动判断单组件运行和多组件打包状态,
76 | 5、模块自动化注册,数据自动经过服务器转发,可以到达APP内的组件的任何一环
77 |
78 |
79 |
80 | 实现原理:
81 | 服务器保存客户端注册的信使,收到消息时,遍历转发
82 | ```
83 | //1.根据模块ID保存所有的客户端信使
84 | private ConcurrentHashMap mClientMessengers = new ConcurrentHashMap<>();
85 |
86 | ```
87 | ```
88 | //2.收到消息时转发给其他模块的处理器,来源模块除外
89 | Enumeration keys = mClientMessengers.keys();
90 | while (keys.hasMoreElements()) {
91 | int moduleId = (int) keys.nextElement();
92 | Messenger mMessenger = mClientMessengers.get(moduleId);
93 | if (moduleId != msg.arg1) {//不是目标来源模块,进行分发
94 | Message _msg = Message.obtain(msg);
95 | try {
96 | mMessenger.send(_msg);
97 | } catch (Exception e) {
98 | e.printStackTrace();
99 | }
100 | }
101 | }
102 | ```
103 |
104 |
105 |
106 |
107 |
108 |
109 | ####第四步,OkBus实现同异步服务互调
110 |
111 | 服务互调就是OkBus的两个消息,触发服务一个消息,返回结果一个消息
112 |
113 |
114 | 异步互调就是两个消息,对应两个回调
115 |
116 | ```
117 | /**
118 | * 服务规则:
119 | * 1、服务的请求ID必须是负值(正值表示事件)
120 | * 2、服务的请求ID必须是奇数,偶数表示该服务的返回事件,
121 | * 即: requestID-1 = returnID
122 | * 例如 -0xa001表示服务请求 -0xa002表示-0xa001的服务返回
123 | */
124 |
125 | ```
126 |
127 | ```
128 |
129 | /**
130 | * 注册服务
131 | *
132 | * @param serviceId 服务id
133 | * @param callback 服务调用的回调
134 | * @param 服务返回的数据范型
135 | */
136 | public void registerService(final int serviceId, final CallBack callback) {
137 | okBus.unRegister(serviceId);//服务提供者只能有一个
138 | okBus.register(serviceId, new Event() {
139 | @Override
140 | public void call(Message msg) {
141 | //TODO 优化到子线程
142 | OkBus.getInstance().onEvent(serviceId - 1, callback.onCall(msg));
143 | }
144 | });
145 | }
146 |
147 |
148 |
149 | /**
150 | * 异步调用服务
151 | *
152 | * @param serviceId 服务id
153 | * @param callback 回调
154 | */
155 | public void fetchService(final int serviceId, final Event callback) {
156 | if (serviceId > 0 || serviceId % 2 == 0) {
157 | assert false : "请求ID必须是负奇值!";
158 | return;
159 | }
160 | //1、先注册回调
161 | okBus.register(serviceId - 1, new Event() {
162 | @Override
163 | public void call(Message msg) {
164 | callback.call(msg);
165 | okBus.unRegister(serviceId - 1);//服务是单次调用,触发后即取消注册
166 | }
167 | }, Bus.BG);
168 | //2、通知目标模块
169 | okBus.onEvent(serviceId);
170 | }
171 | ```
172 |
173 |
174 | 两个即时的消息一来一回,就完成了服务的互调,
175 | 服务因为是实时调用,因此调用完之后立马注销回调即可。
176 |
177 |
178 | 同步调用则是在异步调用的基础上加了锁:
179 |
180 | ```
181 | /**
182 | * 同步调用服务
183 | *
184 | * @param serviceId 服务ID
185 | * @param timeout 超时时间
186 | * @return
187 | */
188 | public synchronized T fetchService(final int serviceId, int timeout) {
189 | final CountDownLatch latch = new CountDownLatch(1);
190 | final AtomicReference resultRef = new AtomicReference<>();
191 | service.execute(new Runnable() {
192 | @Override
193 | public void run() {
194 | fetchService(serviceId, new Event() {
195 | @Override
196 | public void call(Message msg) {
197 | try {
198 | resultRef.set((T) msg.obj);
199 | } catch (Exception e) {
200 | e.printStackTrace();
201 | } finally {
202 | latch.countDown();
203 | }
204 | }
205 | });
206 | }
207 | });
208 | try {
209 | latch.await(timeout, TimeUnit.SECONDS); //最多等待timeout秒
210 | } catch (Exception e) { //等待中断
211 | e.printStackTrace();
212 | }
213 | return resultRef.get();
214 | }
215 | ```
216 |
217 |
218 | 由于主线程加锁来实现的同步,所以要根据不同组件的ANR触发上限传入timeout
219 |
220 | 同时,所有数据接收的处理必须放到子线程,否则就是死锁:
221 |
222 | ```
223 | private class WorkThread extends Thread {
224 | Handler mHandler;
225 |
226 | @Override
227 | public void run() {
228 | Looper.prepare();
229 | mHandler = new ServiceHandler();
230 | mMessenger = new Messenger(mHandler);
231 | Looper.loop();
232 | }
233 |
234 | public void quit() {
235 | mHandler.getLooper().quit();
236 | }
237 | }
238 | ```
239 |
240 | 注意:子线程Handler需要自己Looper.prepare
241 |
242 |
243 |
244 |
245 |
246 | ####第五步,组件和服务的自动化注册
247 |
248 | 原理也是SPI,1、声明一个组件:
249 | ```
250 | @AutoService(IModule.class)
251 | public class Module extends BaseModule {
252 | @Override
253 | public void afterConnected() {
254 |
255 | }
256 |
257 | @Override
258 | public int getModuleIdId() {
259 | return Constants.MODULE_B;
260 | }
261 | }
262 | ```
263 |
264 |
265 | 2、SPI自动注册
266 | ```
267 | //自动注册组件服务
268 | ServiceLoader modules = ServiceLoader.load(IModule.class);
269 | for (IModule module : modules) module.init();
270 | ```
271 |
272 | ####第六步,自动唤醒,按需加载
273 |
274 |
275 | 单个组件调试时,自动唤醒服务器,需要调用某个组件服务时,自动唤醒目标组件,服务器和目标组件打不打开,有没有被杀死,都能正常唤醒继续通信
276 |
277 | 实现方案:
278 | 1、任意组件打开时,自动唤醒服务器
279 |
280 |
281 | BaseAppModuleApp里面:
282 |
283 | ```
284 | Intent intent = new Intent(MessengerService.class.getCanonicalName());// 5.0+ need explicit intent
285 | intent.setPackage(Constants.SERVICE_PACKAGE_NAME); // the package name of Remote Service
286 | boolean mIsBound = bindService(intent, mBaseModule.mConnection, BIND_AUTO_CREATE);
287 |
288 | ```
289 |
290 |
291 | 2、调用目标组件的服务时,自动唤醒目标组件
292 | ```
293 |
294 | Intent ait = new Intent(NoticeService.class.getCanonicalName()); //唤醒目标进程的服务Action名
295 | ait.setPackage(Constants.MODULE_PACKAGE_PRE + module_name); //唤醒目标进程的包名
296 | BaseAppModuleApp.getBaseApplication().bindService(ait, new ServiceConnection() {
297 | @Override
298 | public void onServiceConnected(ComponentName name, IBinder service) {
299 | if (service != null) {
300 | LogUtils.logOnUI(Constants.TAG, "已经自动唤醒" + module_name);
301 | Messenger moduleNameMessenger = new Messenger(service);
302 | Message _msg = Message.obtain();
303 | Bundle _data = new Bundle();
304 | _data.putBoolean(Constants.NOTICE_MSG, true);
305 | _msg.setData(_data);
306 | _msg.replyTo = okBus.mServiceMessenger;//把服务器的信使给目标组件的信使,让他俩自己联系,这里仅仅是通知
307 | try {
308 | moduleNameMessenger.send(_msg);
309 | } catch (Exception e) {
310 | e.printStackTrace();
311 | }
312 | //唤醒成功,继续发送异步请求,通知目标模块
313 | okBus.onEvent(serviceId);
314 | } else {
315 | LogUtils.logOnUI(Constants.TAG, module_name + "进程,本来就是醒的");
316 | }
317 | }
318 |
319 | @Override
320 | public void onServiceDisconnected(ComponentName name) {
321 | LogUtils.logOnUI(Constants.TAG, "自动唤醒目标进程失败 module_name:" + module_name);
322 | }
323 | }, BIND_AUTO_CREATE);
324 | }
325 | ```
326 | ####其它
327 |
328 | 1、主app壳的处理:
329 | ```
330 | dependencies {
331 | if (isDebug.toBoolean()) {//调试阶段,只保证基本逻辑不报错
332 | implementation project(":lib")
333 | } else {//打包阶段,才真正的引入业务逻辑模块
334 | implementation project(":module_a")
335 | implementation project(":module_b")
336 | implementation project(":module_service")
337 | }
338 | }
339 | ```
340 |
341 | 开发阶段,模块不稳定,直接引用,避免各种麻烦。
342 |
343 | 稍微稳定之后,可以直接使用一个编译好的aar包,减少编译工作量提升编译速度。
344 |
345 | 对于完全稳定,基本不会改的模块,直接引用仓库上的内容,在gradle中声明依赖就行了。
346 |
347 | 2、组件只有当做单独APP运行时才有自己的application
348 | ```
349 | sourceSets {
350 | main {
351 | if (isDebug.toBoolean()) {
352 | manifest.srcFile 'src/main/debug/AndroidManifest.xml'//这里面才有application
353 | } else {
354 | manifest.srcFile 'src/main/AndroidManifest.xml'
355 | }
356 | }
357 | }
358 | ```
359 |
360 | 3、全局组件开关 gradle.properties设置isDebug,gradle自动切换
361 | ```
362 | if (isDebug.toBoolean()) {
363 | apply plugin: 'com.android.application'
364 | } else {
365 | apply plugin: 'com.android.library'
366 | }
367 | ```
--------------------------------------------------------------------------------