前言

继上一篇Android 路由实践(一)之后,断更已经差不多一个月,毕竟是年前的最后一个月,各种事情扎堆,直到近几天才稍微闲下来,于是有了此文。简单回顾下,上一篇文章中简单介绍了三种实现路由的方式,分别是:隐式的Intent、通过初始化路由表的方式实现、通过注解。最后总结了下优缺点,建议使用第二种,今天我们讲下第四种,为啥单开一篇文章呢?因为第四种涉及到知识点有点多,并且参考ButterKbife以及部分阿里巴巴ARouter的实现。

大体思路

通过 annotationProcessor处理编译期注解,在编译的时候给路由表注入数据,这样在运行时通过annotationProcessor生成java代码并编译class文件。以下代码部分参考了Butterknife的实现:

/*** 自定义的编译期Processor,用于生成xxx$$Router.java文件*/
@AutoService(Processor.class)
public class RouterProcessor extends AbstractProcessor {/*** 文件相关的辅助类*/private Filer mFiler;/*** 元素相关的辅助类*/private Elements mElementUtils;/*** 日志相关的辅助类*/private Messager mMessager;/*** 解析的目标注解集合*/@Overridepublic synchronized void init(ProcessingEnvironment processingEnv) {super.init(processingEnv);mElementUtils = processingEnv.getElementUtils();mMessager = processingEnv.getMessager();mFiler = processingEnv.getFiler();}@Overridepublic Set<String> getSupportedAnnotationTypes() {Set<String> types = new LinkedHashSet<>();types.add(RouterTarget.class.getCanonicalName());return types;}@Overridepublic SourceVersion getSupportedSourceVersion() {return SourceVersion.latestSupported();}@Overridepublic boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {mMessager.printMessage(Diagnostic.Kind.WARNING, "processprocessprocessprocess");Set<? extends Element> routeElements = roundEnv.getElementsAnnotatedWith(RouterTarget.class);for (Element element : routeElements) {String packageName = element.getEnclosingElement().toString();String fullClassName = element.toString();String className = fullClassName.substring(fullClassName.indexOf(packageName) + packageName.length() + 1, fullClassName.length());/**//             * 构建类//             */try {RouterTarget annotation = element.getAnnotation(RouterTarget.class);RouterByAnnotationManager.getInstance().addRouter(annotation.value(), element.toString());mMessager.printMessage(Diagnostic.Kind.WARNING, RouterByAnnotationManager.getInstance().getRouter(annotation.value()) + RouterByAnnotationManager.getInstance());FieldSpec routerKey = FieldSpec.builder(String.class, "routerKey", Modifier.FINAL, Modifier.PRIVATE).initializer("$S", annotation.value()).build();FieldSpec clazz = FieldSpec.builder(String.class, "fullClassName", Modifier.FINAL, Modifier.PRIVATE).initializer("$S", fullClassName).build();/*** 构建方法*/MethodSpec methodSpec = MethodSpec.methodBuilder("injectRouter").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addCode("com.example.commonlib.RouterByAnnotationManager.getInstance().addRouter($L,$L);", "routerKey", "fullClassName").build();TypeSpec finderClass = TypeSpec.classBuilder(className + "$$Router").addModifiers(Modifier.PUBLIC).addMethod(methodSpec).addField(routerKey).addField(clazz).addSuperinterface(RouterInjector.class).build();JavaFile.builder(packageName, finderClass).build().writeTo(mFiler);} catch (Exception e) {error("processBindView", e.getMessage());}}return true;}public String getPackageName(TypeElement type) {return mElementUtils.getPackageOf(type).getQualifiedName().toString();}private void error(String msg, Object... args) {mMessager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args));}private void info(String msg, Object... args) {mMessager.printMessage(Diagnostic.Kind.NOTE, String.format(msg, args));}
}复制代码

注意getSupportedAnnotationTypes(),如果你要对那些类进行处理,就要把Class的类名加入到Set中并且返回。然后看下process()方法,里面利用javaPoet生成java文件,文件形如UserInfoActivity$$Router,内容如下:

import java.lang.Override;
import java.lang.String;public class UserInfoActivity$$Router implements RouterInjector {private final String routerKey = "android.intent.action.USERINFO";private final String fullClassName = "com.example.userlib.UserInfoActivity";@Overridepublic void injectRouter() {com.example.commonlib.RouterByAnnotationManager.getInstance().addRouter(routerKey,fullClassName);}
}
复制代码

那么相信重点来了,怎么去调用injectRouter()方法,将数据注入到路由表中,到这里的时候

差点因为这个问题前功尽弃,最后祭出了阿里大法,参考了ARouter的实现。具体如下:

通过Application对Router进行初始化:

public class RouterApplication extends Application {@Overridepublic void onCreate() {super.onCreate();//初始化路由Router.init(this);}
}复制代码

Router初始化的时候通过反射将数据注入到路由表

public static void init(Application application) {try {Set<String> classNames = ClassUtils.getFileNameByPackageName(application, ActionConstant.SUFFIX);for (String className : classNames) {RouterFinder.bind(className);}} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}复制代码

来看下阿里ARouter的反射帮助类:

**这个类是从alibaba的ARouter复制过来的用来扫描所有的类等*/
public class ClassUtils {private static final String EXTRACTED_NAME_EXT = ".classes";private static final String EXTRACTED_SUFFIX = ".zip";private static final String SECONDARY_FOLDER_NAME = "code_cache" + File.separator + "secondary-dexes";private static final String PREFS_FILE = "multidex.version";private static final String KEY_DEX_NUMBER = "dex.number";private static final int VM_WITH_MULTIDEX_VERSION_MAJOR = 2;private static final int VM_WITH_MULTIDEX_VERSION_MINOR = 1;private static SharedPreferences getMultiDexPreferences(Context context) {return context.getSharedPreferences(PREFS_FILE, Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? Context.MODE_PRIVATE : Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);}/*** 通过指定包名,扫描包下面包含的所有的ClassName** @param context     U know* @param suffix 包名* @return 所有class的集合*/public static Set<String> getFileNameByPackageName(Context context, final String suffix) throws PackageManager.NameNotFoundException, IOException, InterruptedException {final Set<String> classNames = new HashSet<>();List<String> paths = getSourcePaths(context);final CountDownLatch parserCtl = new CountDownLatch(paths.size());for (final String path : paths) {RouterPoolExecutor.getInstance().execute(new Runnable() {@Overridepublic void run() {DexFile dexfile = null;try {if (path.endsWith(EXTRACTED_SUFFIX)) {//NOT use new DexFile(path), because it will throw "permission error in /data/dalvik-cache"dexfile = DexFile.loadDex(path, path + ".tmp", 0);} else {dexfile = new DexFile(path);}Enumeration<String> dexEntries = dexfile.entries();while (dexEntries.hasMoreElements()) {String className = dexEntries.nextElement();if (className.endsWith(suffix)) {classNames.add(className);}}} catch (Throwable ignore) {Log.e("ARouter", "Scan map file in dex files made error.", ignore);} finally {if (null != dexfile) {try {dexfile.close();} catch (Throwable ignore) {}}parserCtl.countDown();}}});}parserCtl.await();Log.e("getFileNameByPackage", "Filter " + classNames.size() + " classes by packageName <" + suffix + ">");return classNames;}/*** get all the dex path** @param context the application context* @return all the dex path* @throws PackageManager.NameNotFoundException* @throws IOException*/public static List<String> getSourcePaths(Context context) throws PackageManager.NameNotFoundException, IOException {ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);File sourceApk = new File(applicationInfo.sourceDir);List<String> sourcePaths = new ArrayList<>();sourcePaths.add(applicationInfo.sourceDir); //add the default apk path//the prefix of extracted file, ie: test.classesString extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;//        如果VM已经支持了MultiDex,就不要去Secondary Folder加载 Classesx.zip了,那里已经么有了
//        通过是否存在sp中的multidex.version是不准确的,因为从低版本升级上来的用户,是包含这个sp配置的if (!isVMMultidexCapable()) {//the total dex numbersint totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1);File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);for (int secondaryNumber = 2; secondaryNumber <= totalDexNumber; secondaryNumber++) {//for each dex file, ie: test.classes2.zip, test.classes3.zip...String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX;File extractedFile = new File(dexDir, fileName);if (extractedFile.isFile()) {sourcePaths.add(extractedFile.getAbsolutePath());//we ignore the verify zip part} else {throw new IOException("Missing extracted secondary dex file '" + extractedFile.getPath() + "'");}}}sourcePaths.addAll(tryLoadInstantRunDexFile(applicationInfo));return sourcePaths;}/*** Get instant run dex path, used to catch the branch usingApkSplits=false.*/private static List<String> tryLoadInstantRunDexFile(ApplicationInfo applicationInfo) {List<String> instantRunSourcePaths = new ArrayList<>();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && null != applicationInfo.splitSourceDirs) {// add the split apk, normally for InstantRun, and newest version.instantRunSourcePaths.addAll(Arrays.asList(applicationInfo.splitSourceDirs));Log.d("tryLoadInstantRunDex", "Found InstantRun support");} else {try {// This man is reflection from Google instant run sdk, he will tell me where the dex files go.Class pathsByInstantRun = Class.forName("com.android.tools.fd.runtime.Paths");Method getDexFileDirectory = pathsByInstantRun.getMethod("getDexFileDirectory", String.class);String instantRunDexPath = (String) getDexFileDirectory.invoke(null, applicationInfo.packageName);File instantRunFilePath = new File(instantRunDexPath);if (instantRunFilePath.exists() && instantRunFilePath.isDirectory()) {File[] dexFile = instantRunFilePath.listFiles();for (File file : dexFile) {if (null != file && file.exists() && file.isFile() && file.getName().endsWith(".dex")) {instantRunSourcePaths.add(file.getAbsolutePath());}}Log.d("tryLoadInstantRunDex", "Found InstantRun support");}} catch (Exception e) {Log.d("tryLoadInstantRunDex", "InstantRun support error, " + e.getMessage());}}return instantRunSourcePaths;}/*** Identifies if the current VM has a native support for multidex, meaning there is no need for* additional installation by this library.** @return true if the VM handles multidex*/private static boolean isVMMultidexCapable() {boolean isMultidexCapable = false;String vmName = null;try {if (isYunOS()) {    // YunOS需要特殊判断vmName = "'YunOS'";isMultidexCapable = Integer.valueOf(System.getProperty("ro.build.version.sdk")) >= 21;} else {    // 非YunOS原生AndroidvmName = "'Android'";String versionString = System.getProperty("java.vm.version");if (versionString != null) {Matcher matcher = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)?").matcher(versionString);if (matcher.matches()) {try {int major = Integer.parseInt(matcher.group(1));int minor = Integer.parseInt(matcher.group(2));isMultidexCapable = (major > VM_WITH_MULTIDEX_VERSION_MAJOR)|| ((major == VM_WITH_MULTIDEX_VERSION_MAJOR)&& (minor >= VM_WITH_MULTIDEX_VERSION_MINOR));} catch (NumberFormatException ignore) {// let isMultidexCapable be false}}}}} catch (Exception ignore) {}Log.i("isVMMultidexCapable", "VM with name " + vmName + (isMultidexCapable ? " has multidex support" : " does not have multidex support"));return isMultidexCapable;}/*** 判断系统是否为YunOS系统*/private static boolean isYunOS() {try {String version = System.getProperty("ro.yunos.version");String vmName = System.getProperty("java.vm.name");return (vmName != null && vmName.toLowerCase().contains("lemur"))|| (version != null && version.trim().length() > 0);} catch (Exception ignore) {return false;}}
}复制代码

注意看tryLoadInstantRunDexFile()这个方法,记得在上一篇文章中说到资源路径获得DexFile,注意5.0以上版本要求关掉instant run 方法否则会自动拆包遍历不到所有activity类,导致有些加了RouterTarget注解的Activity扫描不到,Arouter在tryLoadInstantRunDexFile()解决了这个问题,如果不调用这个方法的话,,只有如下图的apk:

base.apk一般是不包括我们自己写的代码,这个方法调用之后结果如下:

可以扫描到所有的apk,之后接下来我们就可以解压出项目里面所有的类,通过找出类名后缀为$$Router的类进行发射,代码如下:

/*** 通过指定包名,扫描包下面包含的所有的ClassName** @param context     U know* @param suffix 包名* @return 所有class的集合*/
public static Set<String> getFileNameByPackageName(Context context, final String suffix) throws PackageManager.NameNotFoundException, IOException, InterruptedException {final Set<String> classNames = new HashSet<>();List<String> paths = getSourcePaths(context);final CountDownLatch parserCtl = new CountDownLatch(paths.size());for (final String path : paths) {RouterPoolExecutor.getInstance().execute(new Runnable() {@Overridepublic void run() {DexFile dexfile = null;try {if (path.endsWith(EXTRACTED_SUFFIX)) {//NOT use new DexFile(path), because it will throw "permission error in /data/dalvik-cache"dexfile = DexFile.loadDex(path, path + ".tmp", 0);} else {dexfile = new DexFile(path);}Enumeration<String> dexEntries = dexfile.entries();while (dexEntries.hasMoreElements()) {String className = dexEntries.nextElement();if (className.endsWith(suffix)) {classNames.add(className);}}} catch (Throwable ignore) {Log.e("ARouter", "Scan map file in dex files made error.", ignore);} finally {if (null != dexfile) {try {dexfile.close();} catch (Throwable ignore) {}}parserCtl.countDown();}}});}parserCtl.await();Log.e("getFileNameByPackage", "Filter " + classNames.size() + " classes by packageName <" + suffix + ">");return classNames;
}复制代码

注意这里:

   dexfile = new DexFile(path);复制代码

我们上一篇文章中建议不要使用,因为安卓8.0已经打上了废弃标志

但是既然阿里爸爸这么用了,相信以后也会有相应的解决办法,我们及时跟进,如果读者有好的方法,欢迎提出,大家一起研究研究,接下来就是反射调用injectRouter() ,

public class RouterFinder {public RouterFinder() {throw new AssertionError("No .instances");}private static Map<String, RouterInjector> FINDER_MAP = new HashMap<>();/*** 获取目标类** @param className*/public static void inject(String  className) {try {Log.e("inject",className);RouterInjector injector = FINDER_MAP.get(className);if (injector == null) {Class<?> finderClass = Class.forName(className);injector = (RouterInjector) finderClass.newInstance();FINDER_MAP.put(className, injector);}injector.injectRouter();} catch (Exception e) {}}}复制代码

到此完成了路由表的数据填充,具体使用如下:

new Router.Builder(this, RouterByAnnotationManager.getInstance().getRouter(ActionConstant.ACTION_USER_INFO)).addParams(ActionConstant.KEY_USER_NAME, etUserName.getText().toString()).addParams(ActionConstant.KEY_PASS_WORD, etPassWord.getText().toString()).go();复制代码

到此完成了编译期路由的实现,牵扯的东西还是挺多,历经千辛万苦。代码github,喜欢的给个星吧!

Android 路由实践(二)相关推荐

  1. Android最佳性能实践(二)——分析内存的使用情况

    由于Android是为移动设备开发的操作系统,我们在开发应用程序的时候应当始终把内存问题充分考虑在内.虽然Android系统拥有垃圾自动回收机制,但这并不意味着我们就可以完全忽略何时去分配或释放内存. ...

  2. Android最佳性能实践(一):合理管理内存

    原文出处::http://blog.csdn.net/guolin_blog/article/details/42238627 系列阅读 Android最佳性能实践(一):合理管理内存 Android ...

  3. Android最佳性能实践(一)——合理管理内存

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/42238627 有不少朋友都问过我,怎样才能写出高性能的应用程序,如何避免程序出现O ...

  4. Xamarin.Android开发实践(十七)

    Xamarin.Android开发实践(十七) 原文:Xamarin.Android开发实践(十七) Xamarin.Android之定位 一.前言 打开我们手中的应用,可以发现越来越多的应用使用了定 ...

  5. React Native在Android当中实践(五)——常见问题

    React Native在Android当中实践(一)--背景介绍 React Native在Android当中实践(二)--搭建开发环境 React Native在Android当中实践(三)--集 ...

  6. Xamarin.Android开发实践(十四)

    原文:Xamarin.Android开发实践(十四) Xamarin.Android之ListView和Adapter 一.前言 如今不管任何应用都能够看到列表的存在,而本章我们将学习如何使用Xama ...

  7. Xamarin.Android开发实践(一)

    原文:Xamarin.Android开发实践(一) 一.准备工作 1.创建一个空的解决方案,并命名为Phoneword 2.右击解决方案 新建->新建项目 并命名为Phoneword_Droid ...

  8. Android开发实践:Java层与Jni层的数组传递

    Android开发中,经常会在Java代码与Jni层之间传递数组(byte[]),一个典型的应用是Java层把需要发送给客户端的数据流传递到Jni层,由Jni层的Socket代码发送出去,当然,Jni ...

  9. React Native在Android当中实践(一)——背景介绍

    React Native在Android当中实践(一)--背景介绍 React Native在Android当中实践(二)--搭建开发环境 React Native在Android当中实践(三)--集 ...

最新文章

  1. 【jsp】jsp的内置对象(部分)
  2. 【干货分享】云服务平台的架构及优势(上)
  3. 支持python开发的环境有哪些变化_Python开发实践:打造完美的项目工程环境
  4. Linux中的一个命令行计算器bc简介
  5. 计算机文化教程实验基础知识,《计算机文化基础上机实验教程》孙家启、黄晓梅、刘奎 著_孔网...
  6. Linux经典问题—五哲学家就餐问题
  7. axios框架里面如何使用get,post,通用ajax方法请求。
  8. 具体数学-第3课(递归式转化为求和求解)
  9. XShell收费?5款免费且超赞的SSH工具,一个比一个香
  10. 怎么把java程序打包?java源代码打包方法
  11. Scrum敏捷开发实战分享(上篇):方法介绍、敏捷团队和敏捷流程
  12. 【临床研究】---多元回归分析中的变量筛选问题
  13. 【品牌DTC增长力】私域,你做的可能是“假的”
  14. 快递单号查询,物流查询
  15. 飞机机身结构主要分三种,现在大部分飞机都用第三种
  16. 优麒麟 配置java_优麒麟中安装虚拟机(ARM64)
  17. 看来不止一次的电影(电影经典给你好看准备下载下来,免得以后收费了)
  18. http的一个在线工具(推荐)
  19. 【英语】人生千变万化,不要因挫折而苦恼
  20. EJB_开发EJB容器模型的WEB服务

热门文章

  1. idea springboot一直卡在启动 没有日志输出
  2. 数据结构课上笔记15
  3. C++:43---派生类向基类转换、静态/动态的类变量
  4. linux时间轮 Timing-Wheel的实现
  5. 硬件密码组件的硬件结构、作用及实现应用设计
  6. 服务器密码机部分文件的介绍学习
  7. C语言 项目练习-家庭收支软件
  8. 一个DEMO让你彻底理解线程池
  9. 机器学习入门阶段程序员易犯的5个错误
  10. 关于H264通过RTP传输的打包方式