|
1 | 1 | ## modifier-aspect
|
2 | 2 |
|
3 |
| -用于实现 AOP 的字节码修改器 |
| 3 | +基于 [BCU](https://github.com/Ysj001/BytecodeUtil) 开发的,用于在 Android 中实现 AOP 的字节码修改器。 |
| 4 | + |
| 5 | +本库相比 AspectJ 在 Android 上有更好的编译兼容性,可避免编译时产生的各种问题。 |
| 6 | + |
| 7 | +- 如果本项目对你有所帮助,欢迎 start。 |
| 8 | +- 如果有问题或 bug 欢迎提 issues 给我。 |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +### Compile |
| 13 | + |
| 14 | +工程结构如下 |
| 15 | + |
| 16 | +- `app` 用于演示 |
| 17 | +- `buildSrc` 管理 maven 发布和版本控制项目统一配置 |
| 18 | +- `repos` 本地 maven 仓库,便于开发时调试 |
| 19 | +- `lib_modifier_aspect` 本项目核心 Modifier 实现 |
| 20 | + - `aspect-api` 上层需要使用的 api |
| 21 | + |
| 22 | +在编译本项目前请先在根目录下执行 `gradlew publishAllPublicationsToLocalRepository` 然后重新 sync。 |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +### Quick Start |
| 27 | + |
| 28 | +1. 推荐使用下面所示的最新版本: |
| 29 | + |
| 30 | + - BCU:[](https://jitpack.io/#Ysj001/BytecodeUtil) |
| 31 | + - modifier-aspect:[](https://jitpack.io/#Ysj001/bcu-modifier-aspect) |
| 32 | + |
| 33 | +2. 在项目的根 `build.gradle.kts` 中配置如下 |
| 34 | + |
| 35 | + ```kotlin |
| 36 | + buildscript { |
| 37 | + repositories { |
| 38 | + maven { setUrl("https://jitpack.io") } |
| 39 | + } |
| 40 | + |
| 41 | + dependencies { |
| 42 | + // BCU 插件依赖 |
| 43 | + classpath("com.github.Ysj001.BytecodeUtil:plugin:<lastest-version>") |
| 44 | + // modifier-aspect 依赖 |
| 45 | + classpath("com.github.Ysj001:bcu-modifier-aspect:<lastest-version>") |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + subprojects { |
| 50 | + repositories { |
| 51 | + maven { url 'https://jitpack.io' } |
| 52 | + } |
| 53 | + } |
| 54 | + ``` |
| 55 | + |
| 56 | +3. 在 `app` 模块的 `build.gradle.kts` 中的配置如下 |
| 57 | + |
| 58 | + ```kotlin |
| 59 | + plugins { |
| 60 | + id("com.android.application") |
| 61 | + id("org.jetbrains.kotlin.android") |
| 62 | + // 添加 bcu 插件 |
| 63 | + id("bcu-plugin") |
| 64 | + } |
| 65 | + |
| 66 | + // 配置 bcu 插件 |
| 67 | + bytecodeUtil { |
| 68 | + loggerLevel = 1 |
| 69 | + modifiers = arrayOf( |
| 70 | + // 使用 modifier-aspect 的 Modifier 实现 |
| 71 | + Class.forName("com.ysj.lib.bcu.modifier.aspect.AspectModifier"), |
| 72 | + ) |
| 73 | + notNeed = { entryName -> |
| 74 | + // 请按需配置过滤,可大幅提升编译输速度 |
| 75 | + false |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + dependencies { |
| 80 | + // 依赖 modifier-aspect-api |
| 81 | + implementation("com.github.Ysj001:bcu-modifier-aspect-api:<lastest-version>") |
| 82 | + } |
| 83 | + ``` |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +### API |
| 88 | + |
| 89 | +1. 通过 `Aspect` 注解标记一个类用于处理切面 |
| 90 | + |
| 91 | + ```kotlin |
| 92 | + @Aspect |
| 93 | + object AspectDemo { |
| 94 | + } |
| 95 | + ``` |
| 96 | + |
| 97 | +2. 通过 `Pointcut` 注解标记一个切面类中的方法,使其变成切入点 |
| 98 | + |
| 99 | + ```kotlin |
| 100 | + @Aspect |
| 101 | + object AspectDemo { |
| 102 | + @Pointcut( |
| 103 | + target = "class:com/ysj/demo/aspect/MainActivity", // 切入的目标方法所在的类 |
| 104 | + funName = "test1", // 切入的目标方法名 |
| 105 | + funDesc = ".*." // 切入的目标方法描述,这里的正则表示任意描述 |
| 106 | + position = POSITION_START, // 定义切入位置,这里表示在目标方法开始处 |
| 107 | + ) |
| 108 | + fun test1onStart() { |
| 109 | + Log.i(TAG, "test1onStart.") |
| 110 | + } |
| 111 | + } |
| 112 | + ``` |
| 113 | + |
| 114 | +3. 编译运行并查看 log |
| 115 | + |
| 116 | +更详细功能演示可以进入 `app` 的 [AspectDemo](app/src/main/java/com/ysj/demo/aspect/AspectDemo.kt) 中查看,效果如下图所示。 |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +### Projects Using modifier-aspect |
| 123 | + |
| 124 | +- [Android Permission Monitor](https://github.com/Ysj001/PermissionMonitor):Android 隐私政策敏感权限监控。 |
0 commit comments