Router路由文档生成插件开发

路由文档生成插件

背景

  • 手动维护scheme文档,太费劲
  • 业务繁忙,经常忘记添加一些新的scheme
  • 减少PM、后端、FE同学沟通scheme成本

为什么用gradle插件

  • Router2会自动会给每个modlue生成路由的txt文件
  • 路由表信息是在打包的时候生成的, hock打包task就可以轻松实现

插件开发

1、简介

  • 支持路由动态维护,减少人工维护成本
  • 打正式包支持路由上报
  • 打测试包支持路由文档本地生成
  • 支持路由名称、路由介绍、参数名称、参数介绍输出
  • 支持选择路由是否导出,避免暴露一些不对外的路由

2、实现

Router路由文档生成插件开发_第1张图片
插件流程图.png

1、在gradle插件中如何获取配置信息

  • 在gradle中app下build.gradle文件中定义extention变量
routerDocumentInfo {
  modlues = [
      "m_house",
      "m_tenant"
  ]
}
  • 在自定义plugin中获取变量值
 void apply(Project project) {
    project.extensions.create("routerDocumentInfo", RouterDocumentExtention.class)

    project.afterEvaluate {
         RouterDocumentExtention extention = project.routerDocumentInfo
      for(String str : extention.modlues){
        System.out.println(str)
      }
     }
}     

2、通过默认ext变量获取配置信息
在跟项目下build.gradle文件下

ext {
  isPlugin = true
  sourceCompatibility = JavaVersion.VERSION_1_7
  targetCompatibility = JavaVersion.VERSION_1_7

  compileSdkVersion = 15
  buildToolsVersion = "26.0.3"
  minSdkVersion = 15
  targetSdkVersion = 19

  supportVersion = "25.3.1"
  annotationVersion = "1.0.0-SNAPSHOT"
  compilerVersion = "1.0.0-SNAPSHOT"
}

在配置加载完成后,通过下面代码获取

 project.afterEvaluate{
      ExtraPropertiesExtension ext = project.rootProject.ext
      if (ext.has("routerVersion")) {
        routerVersion = ext.get("routerVersion")
      }
      if (ext.has("compilerVersion")) {
        compilerVersion = ext.get("compilerVersion")
      }
      if (ext.has("annotationVersion")) {
        annotationVersion = ext.get("annotationVersion")
      }
      if (ext.has("isPlugin")) {
        isPlugin = ext.get("isPlugin")
      }
 }

2、hock打包流程

AppExtension extension = project.extensions.getByName("android")
      extension.applicationVariants.all { variant ->
        println(variant.name) //debug、release
        ArrayList routerUrls = getRouterUrls(project, variant)
        if (variant.name.toLowerCase().contains("release")) {
          variant.assemble.doLast {
            //release环境上报路由信息
            readRouterInfo(getRouterUrls(project, variant))
          }
        } else if (variant.name.toLowerCase().contains("debug")) {
          variant.assemble.doLast {
            //debug环境上报路由信息
            generateRouterDocument(routerUrls)
          }
    
        }

其它:

  • gradle调试:https://www.jianshu.com/p/b2000f80b818

  • gradle基础:

    https://www.jianshu.com/p/a724dda759de

    https://www.jianshu.com/p/98e991ac8d3a

    https://www.jianshu.com/p/f428388bd511

    https://www.jianshu.com/p/2f2d765b4322

使用

1、 activity、fragment

@Route(value = "lianjiaalliance://house/detail",desc = "房源详情页",beExport = 1)
public class HouseDetailActivity extends Activity {
  @InjectParam(key = "house_id",desc = "房源id")
  public String houseId;

  @InjectParam(key = "del_type",desc = "委托类型")
  public String delType;
  @Override
  protected void onCreate( @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }
}

2、方法路由

public class HouseRouterFunction {

  @Route(value = "lianjiaalliance://house/share",beExport = 1)
  public static void toShareHouse(
      @Param(value = { "houseId", "house_id" }, desc = "房源Id") String houseId) {

  }
}

你可能感兴趣的:(Router路由文档生成插件开发)