public class MainActivity extends BaseActivity {

/**

  • 接收参数
    */
    @Autowired(name = “name”)
    public String name;
    @Autowired(name = “age”)
    public int age;

    }
拦截器

// A more classic application is to handle login events during a jump so that there is no need to repeat the login check on the target page.
// Interceptors will be executed between jumps, multiple interceptors will be executed in order of priority
@Interceptor(priority = 8, name = “test interceptor”)
public class TestInterceptor implements IInterceptor {
@Override
public void process(Postcard postcard, InterceptorCallback callback) {

// No problem! hand over control to the framework
callback.onContinue(postcard);

// Interrupt routing process
// callback.onInterrupt(new RuntimeException(“Something exception”));

// The above two types need to call at least one of them, otherwise it will not continue routing
}

@Override
public void init(Context context) {
// Interceptor initialization, this method will be called when sdk is initialized, it will only be called once
}
}

降级策略

ARouter提供的降级策略主要有两种方式,一种是通过回调的方式;一种是提供服务接口的方式。
一、回调方式
这种方式在跳转失败的时候会回调NavCallback接口的onLost方法。ARouter提供的回调方式的函数如下:

ARouter.getInstance().build(MainRoutePath.MAIN_ACTIVITY).navigation(this, new NavCallback() {
@Override
public void onFound(Postcard postcard) {
Log.d(“ARouter”, “找到了”);
}

@Override
public void onLost(Postcard postcard) {
Log.d(“ARouter”, “找不到了”);
}

@Override
public void onArrival(Postcard postcard) {
Log.d(“ARouter”, “跳转完了”);
}

@Override
public void onInterrupt(Postcard postcard) {
Log.d(“ARouter”, “被拦截了”);
}
});

二、服务接口
全局降级-服务接口的方式主要处理逻辑在内部,暴露的接口很友好。

ARouter.getInstance().build("/test/test").navigation();

此种降级策略需要实现服务接口DegradeService,返回的就一个方法就是onLost。例如:

@Route(path = RoutePath.DEGRADE)
public class DegradeServiceImpl implements DegradeService {
@Override
public void onLost(Context context, Postcard postcard) {
ARouter.getInstance().build(RoutePath.DEGRADE_TIP).greenChannel().navigation();
}

@Override
public void init(Context context) {

}
}

混淆

为了避免打包时
出现错误,需要将下面的内容使用keep标示。

-keep public class com.alibaba.android.arouter.routes.{*;}
-keep public class com.alibaba.android.arouter.facade.
{;}
-keep class * implements com.alibaba.android.arouter.facade.template.ISyringe{
;}

如果使用了 byType 的方式获取 Service,需添加下面规则,保护接口

-keep interface * implements com.alibaba.android.arouter.facade.template.IProvider

如果使用了 单类注入,即不定义接口实现 IProvider,需添加下面规则,保护实现

-keep class * implements com.alibaba.android.arouter.facade.template.IProvider

ARouter Helper,

在 Android Studio 插件市场中搜索 ARouter Helper, 或者直接下载文档上方 最新版本 中列出的 arouter-idea-plugin zip 安装包手动安装,安装后 插件无任何设置,可以在跳转代码的行首找到一个图标 (navigation) 点击该图标,即可跳转到标识了代码中路径的目标类。

ARouter模块化示例

接下来,将会用一个demo介绍如何用ARouter进行模块化开发,demo模块化的整体架构如下图所示。

每个模块的作用如下:

  • app:项目的宿主模块,仅仅是一个空壳,依赖于其他模块,成为项目架构的入口;
  • baselibrary:项目的基类库,每个子模块都依赖共享公用的类和资源,防止公用的功能在不同的模块中有多个实现方式;
  • module_route:集中管理所有模块的route的库;
  • module_main:闪屏页,登录页,主页等;
  • module_home:首页模块;
  • module_video:视频模块;
  • module_mine:我的模块;
依赖模式与独立模式的切换

使用模块化开发的一个好处是,各个独立的模块可以同时开发,独立运行而不必依赖于宿主app,也就是每个module是一个独立的App,项目发布的时候依赖到宿主App中即可。各业务模块之间不允许存在相互依赖关系,但是需要依赖基类库。
并且,单一模块生成的apk体积也小,编译时间也快,开发效率会高很多,同时也可以独立测试,要实现这样的效果需要对项目做一些配置。

gradle.properties配置

在主项目的gradle.properties中需要设置一个开关,用来控制module的编译模式,例如:

isModule=false

当isModule为false时作为依赖库,只能以宿主app启动项目,选择运行模块时其他module前都是红色的X,表示无法运行。

当isModule为true时,作为单独的模块进行运行,选择其中一个具体的module就可以直接运行。

module清单文件

为了完成依赖模式与独立模式的切换,module清单文件需要配置两个,一个作为独立项目的清单文件,一个作为库的清单文件,以module_main模块为例。

buildApp作为依赖库的清单文件,和独立项目的清单文件buildModule区别是依赖库的清单文件Application中没有配置入口的Activity,其他都一样。

gradle配置

为了完成切换,还需要对module的build.gradle文件进行配置,如下图:

宿主app配置

接下来,在宿主app的build.gradle中添加模块依赖,如下所示:

dependencies {
if (!isModule.toBoolean()) {
implementation project(’:module_home’)
implementation project(’:module_video’)
implementation project(’:module_main’)
implementation project(’:module_mine’)
implementation project(’:module_router’)
}
}

ARouter Helper

常见问题

问题1

错误:ARouter::Compiler >>> No module name, for more information, look at gradle log.
检查项目依赖的全部module,包括module依赖的module,为了能够进行单独的编译,所以需要为每一个module添加名称,即在每个module的 build.gradle中加上下面的代码:

defaultConfig {

javaCompileOptions {
annotationProcessorOptions {
arguments = [ AROUTER_MODULE_NAME : project.getName() ]
}
}
}

问题2

ARouter.getInstance().inject(this);
问题描述:有一个singletask启动模式的activity,在onNewIntent方法中调用ARouter.getInstance().inject(this);得不到参数,查看ARouter在build过程中生成的代码可以知道它是调用了activity的getIntent来获取参数的,但是onNewIntent中的intent和在onCreate方法中的intent并不相同,所以需要在onNewIntent方法中调用setIntent方法,然后就能得到参数了。

问题3

ARouter there’s no route matched

不同module的一级路径必须不同,否则会导致一个moudle中的一级路径失效。

附:示例下载地址

##文末
ewIntent中的intent和在onCreate方法中的intent并不相同,所以需要在onNewIntent方法中调用setIntent方法,然后就能得到参数了。

问题3

ARouter there’s no route matched

不同module的一级路径必须不同,否则会导致一个moudle中的一级路径失效。

附:示例下载地址

##文末

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐