This page looks best with JavaScript enabled

一种 Android 项目的模块化开发机制

 ·  ☕ 2 min read

在 Android 开发中,当项目增加一定规模之后,一般都会采用多模块的项目结构。当然也能采用插件化的开发模式,具体采用什么开发模式,开发者可以自行定夺。这里将介绍下我们项目中采用的模块化开发机制。本质是基于 gradle 的 Multi-Project 构建和 Java 的动态代理机制。

// settings.gradle

1
include ':app', ':feature_a', ':feature_b', ':feature_c'

利用这套机制时:比如开发者开发 A 功能时,只会涉及 feature_a 模块中代码的修改,那么开发者就可以让 gradle 不编译其他模块中的代码(在 settings.gradle 中注释不需要的模块),从而能够减少本地开发时的编译耗时,也能够让某些代码只会在开发期间存在(比如为了方便测试单独提供的 admin 模块)。

选择性编译

同时为了避免因为模块间彼此依赖,导致个别模块不编译的目标无法实现。于是要求各个模块之间不能直接依赖具体实现,只能依赖某个公共模块(比如 app 模块)提供的接口。

具体实现是:

在 app 模块中定义模块 feature_a 能够提供的功能,比如打开 feature_a 中的一个 Activity.

1
2
3
4
public interface IFeatureA {
    @Nullable
    public Class<Activity> getActivityOfA();
}

然后在 feature_a 中实现这个接口:

1
2
3
4
5
public class FeatureA implements IFeatureA {
    public Class<Activity> getActivityOfA() {
        return FeatureAActivity.class;
    }
}

然后在 app 中注入 IFeatureA 接口的具体实现:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class FeatureRegister {

    private static IFeatureA featureA;

    @Nullable
    public static IFeatureA getFeatureA() {
        if (featureA == null) {
            try {
                // 这里使用反射能够在即使 feature_a 模块未加入编译之后也能够成功编译
                featureA = (IFeatureA) Class.forName("io.github.stefanji.feature_a.FeatureA").newInstance();
            } catch (ClassNotFoundException e) {
                // 如果 feature_a 未加入编译,则会触发异常
            }
        }
        return featureA;
    }
}

然后 feature_b 需要获取 feature_a 的 FeatureAActivity:

1
2
3
4
5
IFeatureA featureA = FeatureRegister.getFeatureA();
Class<Activity> activityClass = null;
if (featureA != null) {
    activityClass = featureA.getActivityOfA();
};

动态代理未加入编译的模块

在上面我们已经能够实现不编译某些模块,并且项目整体编译不会出现问题。但是每次访问其他模块提供的功能时,从 app 模块获取实现之后需要进行判空处理。不是太优雅。

于是可以利用动态代理,如果目标模块没有被编译,那么就返回一个实现了目标模块功能接口的代理对象。
修改 app 中代码如下:

 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
@NotNull
public static IFeatureA getFeatureA() {
    if (featureA == null) {
        try {
            featureA = (IFeatureA) Class.forName("io.github.stefanji.feature_a.FeatureA").newInstance();
        } catch (ClassNotFoundException e) {
        }
        // 如果 feature_a 模块未没编译,FeatureA 类将找不到,就动态生成一个 Proxy 类
        if (featureA == null) {
            featureA = (IFeatureA) Proxy.newProxyInstance(FeatureRegister.class.getClassLoader(),
                    new Class[]{IFeatureA.class},
                    new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            Class returnType = method.getReturnType();
                            // 让原始类型返回零值
                            if (returnType == boolean.class) {
                                return false;
                            }
                            if (returnType == int.class) {
                                return 0;
                            }
                            if (returnType == float.class) {
                                return 0f;
                            }
                            //...
                            // 让引用类型返回 null
                            return null;
                        }
                    });
        }
    }
    return featureA;
}

这样就不用做如下判空了:

1
2
3
if (featureA != null) {
    activityClass = featureA.getActivityOfA();
};

自动化

每新增一个模块,我们就需要重复上面的步骤,在 FeatureRegister 中注册新的 Feature 接口。这种重复的操作,当然可以利用注解处理之或 gradle transform 之类的代码生成机制,让编译器去自动生成模板代码。

Support the author with
alipay QR Code
wechat QR Code

Yang
WRITTEN BY
Yang
Developer