SimpleRouter

    Android组件化路由库,反对模块间通信,实用于中小型我的项目.

最新版本

模块srouter-annotationsrouter-compilersrouter-api
版本

Demo展现

Demo apk下载


接入形式

  1. 在build.gradle文件中增加依赖和配置

    参数示意:

    > SIMPLE_ROUTER_KEY   ->   配置生成的路由文件加密秘钥,长度为16个字> OPEN_AES            ->   是否开启加密> SROUTER_ANNOTATION  ->   annotation版本号(查看下面最新版本)> SROUTER_API         ->   api版本号(查看下面最新版本)> SROUTER_COMPILER    ->   compiler版本号(查看下面最新版本)

    代码配置:

        plugins {       id 'kotlin-kapt'    }        android {        defaultConfig {            javaCompileOptions {                annotationProcessorOptions {                    arguments = [                            SIMPLE_ROUTER_KEY: project.ext.simpleRouterKey,                            OPEN_AES         : project.ext.openAes                    ]                }            }        }    }    dependencies {        implementation "com.github.jeff-liu14:srouter-annotation:$SROUTER_ANNOTATION"        implementation "com.github.jeff-liu14:srouter-api:$SROUTER_API"        kapt "com.github.jeff-liu14:srouter-compiler:$SROUTER_COMPILER"    }
  2. 在须要的Activity/Fragment中增加注解

    #activity@Route(path = "/app/home")class MainActivity : AppCompatActivity() {}      # fragment@Route(path = "/app/demo/product/fragment")class ProductFragment : Fragment() {}
  3. 初始化SDK

       SimpleRouter.init(this)   SimpleRouter.scanRoute(BuildConfig.SIMPLE_ROUTER_KEY, BuildConfig.OPEN_AES)
  4. 路由跳转操作

    //无参跳转SimpleRouter.getInstance()            .build("/app/demo/profile")            .navigate(this)//带参跳转SimpleRouter.getInstance()            .build("/app/demo/profile")            .withString("name", "app-profile:透传参数")            .navigate(this)               
  5. 混同(Proguard)规定

    #FastJson-dontwarn com.alibaba.fastjson.**-keep class com.alibaba.fastjson.**{*; }#SimpleRouter-keep class * extends com.laydown.srouter.api.provider.IProvider

API详解

具体应用办法可参考示例

  1. 手动加载路由文件

    SimpleRouter.getInstance()            .scanRoute(BuildConfig.SIMPLE_ROUTER_KEY, BuildConfig.OPEN_AES)
  2. 无参跳转办法

    SimpleRouter.getInstance()            .build("/app/demo/profile")            .navigate(this)
  3. 有参构造方法

    // 根底数据类型 String boolean int long floatSimpleRouter.getInstance()            .build("/app/demo/profile")            .withString("name", "app-profile:透传参数")            .navigate(this) // 应用Bundle SimpleRouter.getInstance()            .build("/app/demo/profile")            .withBundle(Bundle().apply {                putString("name", "app-profile:透传参数")            })            .navigate(this)
  4. startActivityForResult

    // 旧版跳转SimpleRouter.getInstance()             .build("/app/demo/product")             .withString("name", "app-product:透传参数")             .navigateForResult(this, 10001)             // 应用ActivityResultLauncherval launcher: ActivityResultLauncher<Intent> =         Helper.startActivityForResult(this) { result ->             when (result?.resultCode) {                 RESULT_OK -> {                     Toast.makeText(                         this, result.data?.extras?.getString("uName"), Toast.LENGTH_SHORT                     ).show()                 }             }         }          // 应用SimpleRouter获取结构的intent SimpleRouter.getInstance()             .build("/app/demo/product")             .withString("name", "app-product:透传参数")             .navigateForResultX(this, launcher)    
  5. 获取Fragment

     private fun setFragment() {     val transaction = supportFragmentManager.beginTransaction()     val fragment =         SimpleRouter.getInstance()             .build("/app/demo/product/fragment")             .withString("name", intent.extras?.getString("name"))             .navigate() as Fragment     fragment.arguments = intent.extras     transaction.add(R.id.fl_content, fragment, fragment.javaClass.simpleName)     transaction.commitNowAllowingStateLoss() }
  6. 应用provider实现module对外公布ability

    //在lib-provider中定义biz-tax模块对外公布的接口public interface ITaxProvider extends IProvider {       void sayHello(String message);}/** * 在biz-tax模块中实现ITaxProvider接口 * 并且应用@Route标签注册服务 */@Route(path = "/tax/provider")public class TaxProviderImpl implements ITaxProvider {    private Context mContext;    @Override    public void sayHello(String message) {        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();    }    @Override    public void init(Context context) {        this.mContext = context;    }}//在biz-shop模块中获取服务并调用 val provider: ITaxProvider = SimpleRouter.getInstance()                 .build("/tax/provider")                 .navigate(this@ShopActivity) as ITaxProvider provider.sayHello("ShopActivity call Tax Provider")
  7. 全局降级策略

    //实现全局降级策略服务接口DegradeServiceclass CommonDegradeImpl : DegradeService {       override fun onLost(context: Context, targetMeta: TargetMeta) {           Toast.makeText(context, "Path: " + targetMeta.path + " Lost.", Toast.LENGTH_SHORT).show()       }}//在SimpleRouter中进行注册SimpleRouter.setDegradeService(CommonDegradeImpl())
  8. 全局拦挡策略

    //实现全局拦挡策略服务接口InterceptorCallBack//return false-页面跳转被拦挡 true-持续进行路由操作class CommonInterceptorImpl : InterceptorCallBack {    override fun onContinue(context: Context, targetMeta: TargetMeta): Boolean {        when (targetMeta.path) {            "/app/demo/product1" -> {                Toast.makeText(context, "Product页面被拦挡", Toast.LENGTH_SHORT).show()                return false            }            "/app/demo/profile" -> {                Toast.makeText(context, "Profile页面被拦挡", Toast.LENGTH_SHORT).show()                return false            }        }        return true    } } //在SimpleRouter中注册  SimpleRouter.setInterceptorCallBack(CommonInterceptorImpl())

    源码地址:https://github.com/jeff-liu14/SimpleRouter

欢送大家多多star,也能够参加独特保护