热修复 tinker接入及源码分析( 四 )


ext {tinkerEnabled = true//old apk file to build patch apktinkerOldApkPath = "${bakPath}/old.apk"//proguard mapping file to build patch apktinkerApplyMappingPath = "${bakPath}/old-mapping.txt"}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    提供的例子,基本上展示了的自定义扩展的方式,具体还可以参考:
    所以,如果你使用命令行方式接入,也不要忘了学习下其支持哪些扩展 。
    三、是如何编译时生成的
    从注释和命名上看:
    //可选,用于生成application类provided('com.tencent.tinker:tinker-android-anno:1.7.7')
    • 1
    • 2
      • 1
      • 2

      明显是该库,其结构如下:
      热修复 tinker接入及源码分析

      文章插图
      典型的编译时注解的项目,源码见--anno 。
      入口为com...anno.,可以在该/javax...文件中找到处理类全路径 。
      再次建议,如果你不了解,简单阅读下 如何编写基于编译时注解的项目该文 。
      直接看的方法:
      @Overridepublic boolean process(Set annotations, RoundEnvironment roundEnv) {processDefaultLifeCycle(roundEnv.getElementsAnnotatedWith(DefaultLifeCycle.class));return true;}
      • 1
      • 2
      • 3
      • 4
      • 5
        • 1
        • 2
        • 3
        • 4
        • 5

        直接调用了cle:
        private void processDefaultLifeCycle(Set elements) {// 被注解DefaultLifeCycle标识的对象for (Element e : elements) {// 拿到DefaultLifeCycle注解对象DefaultLifeCycle ca = e.getAnnotation(DefaultLifeCycle.class);String lifeCycleClassName = ((TypeElement) e).getQualifiedName().toString();String lifeCyclePackageName = lifeCycleClassName.substring(0, lifeCycleClassName.lastIndexOf('.'));lifeCycleClassName = lifeCycleClassName.substring(lifeCycleClassName.lastIndexOf('.') + 1);String applicationClassName = ca.application();if (applicationClassName.startsWith(".")) {applicationClassName = lifeCyclePackageName + applicationClassName;}String applicationPackageName = applicationClassName.substring(0, applicationClassName.lastIndexOf('.'));applicationClassName = applicationClassName.substring(applicationClassName.lastIndexOf('.') + 1);String loaderClassName = ca.loaderClass();if (loaderClassName.startsWith(".")) {loaderClassName = lifeCyclePackageName + loaderClassName;}// /TinkerAnnoApplication.tmplfinal InputStream is = AnnotationProcessor.class.getResourceAsStream(APPLICATION_TEMPLATE_PATH);final Scanner scanner = new Scanner(is);final String template = scanner.useDelimiter("\\A").next();final String fileContent = template.replaceAll("%PACKAGE%", applicationPackageName).replaceAll("%APPLICATION%", applicationClassName).replaceAll("%APPLICATION_LIFE_CYCLE%", lifeCyclePackageName + "." + lifeCycleClassName).replaceAll("%TINKER_FLAGS%", "" + ca.flags()).replaceAll("%TINKER_LOADER_CLASS%", "" + loaderClassName).replaceAll("%TINKER_LOAD_VERIFY_FLAG%", "" + ca.loadVerifyFlag());JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(applicationPackageName + "." + applicationClassName);processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Creating " + fileObject.toUri());Writer writer = fileObject.openWriter();PrintWriter pw = new PrintWriter(writer);pw.print(fileContent);pw.flush();writer.close();}}