eleme / lancet

A lightweight and fast AOP framework for Android App and SDK developers
2.12k stars 333 forks source link

能介绍一下原理么?跟AspectJ和Dexposed的区别 #8

Closed matrixxun closed 6 years ago

matrixxun commented 6 years ago

如题, 另外请问支持module或者Library 工程中使用么?我记得AspectJ是不支持在Module或者Library中使用的。

dieyidezui commented 6 years ago

Lancet本质上是 Gradle Plugin,通过依赖 Android 的打包插件提供的 Transform API,在打包过程中获取到所有的代码。 依赖 ASM 提供的字节码注入能力,通过我们解析自定义的注解,在目标点注入相应的代码。 通过注解进行 AOP 这点和 AspectJ 很相似,但是更加轻量和简洁,使用方式也有所不同。

这里要区分一个概念,编译期注入和运行期注入。

编译期:即在编译时对字节码做插桩修改,达到 AOP 的目的。优点是运行时无额外的性能损耗,但因为编译时的限制,只能修改最终打包到APK中的代码,即 Android Framework 的代码是固化在 ROM 中的,无法修改。 运行期:是只在运行时动态的修改代码的执行,因而可以修改 Framework 中代码的执行流程,在hook点上执行性能上有所损耗。

Lancet,编译期注入 AspectJ,既支持编译期也支持运行期的注入,运行期的注入一般是依赖JVM提供的AttachAPI,因为 Android 没有 JVM 的环境,实际上 class 还会继续转换成 dex,因此 AspectJ 在 Android 平台是只能做到编译期注入。 Dexposed,运行期注入

dieyidezui commented 6 years ago

可以在 module 或者 library 中使用,在 module 中定义的注解,在 APK 打包时才会被解析,因此在写 module 时不要 apply plugin: me.ele.lancet

matrixxun commented 6 years ago

ok, that sounds great! 我现在有一个需求是统计子module工程中的所有业务方法的耗时, 能否提供这种批量方法的编织功能?@dieyidezui

dieyidezui commented 6 years ago

具体使用场景是什么样的呢,比如是某个类的所有子类?还是指定的一些类?

matrixxun commented 6 years ago

比如com.tecent.droid.xxx.game包下的所有类和所有方法需要统计方法耗时,如果按照readme中的教程一个类一个类去指定targetclass,会比较累。

dieyidezui commented 6 years ago

没有类似的功能,只能一个个指定,而且方法也要一个个指定。 目前不支持这种所有方法的 AOP, 现在的 AOP 是要匹配方法签名的。