Android模块化路由组件

模块化是基于BaseLibrary开发,日常开发是个app,打包时是个Library;这样便于日常开发时编译快,且可以快速把模块化放入其它apk;限制就是只能有主App向各种业务模块Library跳转,这也是为什么模块化的前提,需要在熟悉业务的情况模块化;如果各种页面之间随便跳也会让模块化失去意义
本篇需要的知识点是注解、动态代理 、隐式跳转等知识

1、首先在BaseLibrary定义跳转的路由注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FullUrl {

    String value();
}

2、在BaseLibrary定义跳转的参数注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface IntentExtrasParam {

    String value();
}

3、在BaseLibrary定义动态代理

public class Router {

    private Context context;
    private static final String TAG = "Router";

    public Router(Context context) {
        this.context = context;
    }

    @SuppressWarnings("unchecked")
    public  T create(Class service){
        return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class[]{service}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                StringBuilder builder = new StringBuilder();
                FullUrl fullUrl = method.getAnnotation(FullUrl.class);
                if(fullUrl instanceof FullUrl){
                    builder.append(fullUrl.value()).append("?");
                }else {
                    throw new IllegalArgumentException("");
                }

                Annotation[][] parameterAnnotations = method.getParameterAnnotations();

                HashMap serializedParams = new HashMap<>();
                for (int i=0;i serializedParams) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(routerUri));

        Log.e(TAG, "performJump: "+ Uri.parse(routerUri));
        Bundle bundle = new Bundle();
        for (Map.Entry entry:serializedParams.entrySet()){
            String key = entry.getKey();
            Object value = entry.getValue();
//可以根据自己的项目需求自由扩展支持数据类型,这里只提供了String和Parcelable类型
            if(value instanceof String){
                bundle.putString(key,(String) value);
                Log.e(TAG, "string: "+(String) value);
            }else if(value instanceof Parcelable){
                bundle.putParcelable(key, (Parcelable) value);
                Log.e(TAG, "Parcelable: "+(Parcelable) value);
            }else {
                throw new IllegalArgumentException("不支持的数据类型");
            }
        }
        intent.putExtras(bundle);
        Log.e(TAG, "performJump: "+intent);
        PackageManager packageManager = context.getPackageManager();
        List activities = packageManager.queryIntentActivities(intent, 0);
        if (!activities.isEmpty()) {
            context.startActivity(intent);
        }

    }
}

4、在业务模块的Library内定义接口;需要注意的是,由App模块跳转到业务模块的页面定义的路由接口放到业务模块内,这样便于管理业务模块的路由页面,这也是为什么不使用显示跳转或者隐式跳转的原因,便于管理路由页面

public interface RouterService {

    @FullUrl("router://com.xiaoma.mylibrary.libraryactivity")
    void startLibraryActivity(@IntentExtrasParam("stringParam") String stringParam,
                              @IntentExtrasParam("user") User user);
}

其中User类如下,其中get/set相关方法和Parcelabel未写出来

public class User implements Parcelable {

    private String name;
    private int age;
}

5、业务模块路由页面的清单文件定义如下

 
            
                
                
                
            
        

6、在主App内,跳转到模块化页面代码

 RouterService routerService = new Router(this).create(RouterService.class);
        User user = new User("张三", 30);
        routerService.startLibraryActivity("xiaoma", user);

7、从主App跳转到模块化页面获取数据的代码如下:

 String stringParam = getIntent().getStringExtra("stringParam");
        User user = getIntent().getParcelableExtra("user");

关于路由和注解的项目地址

文章参考
Android 模块化探索与实践

你可能感兴趣的:(Android模块化路由组件)