最新版本:1.0.1
这是一个使用ASM字节码插桩技术的Android Gradle插件,用于统计主线程中的方法耗时,适配AGP 8.0以上版本。
- ✅ 使用ASM字节码插桩,无侵入性
- ✅ 支持主线程方法耗时统计
- ✅ 可配置最小记录时长
- ✅ 实时统计和日志输出
- ✅ 适配AGP 8.0+版本
- ✅ 支持方法调用次数、平均耗时、最大/最小耗时统计
- ✅ 自动打印功能,无需手动调用
- ✅ 可配置插桩范围(仅项目代码 或 项目+所有依赖库)
在项目根目录的settings.gradle.kts中添加:
pluginManagement {
repositories {
google()
mavenCentral() // 添加mavenCentral仓库
}
}
dependencyResolutionManagement {
repositories {
google()
mavenCentral() // 添加mavenCentral仓库
}
}buildscript {
dependencies {
classpath("io.github.hequanli:method-timer-plugin:1.0.1")
}
}在app模块的build.gradle.kts中:
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.methodtimer.plugin")
}
dependencies {
implementation("io.github.hequanli:method-timer-runtime:1.0.1")
// 其他依赖...
}
// 配置方法耗时统计插件
methodTimer {
enabled.set(true) // 启用插件
mainThreadOnly.set(true) // 只监控主线程
minDuration.set(10L) // 只记录超过10ms的方法
instrumentationScopeAll.set(false) // 只插桩项目自身代码(推荐)
}建议参考 Demo 集成方式
插件现在支持自动打印和手动打印两种方式:
自动打印模式:
D/MethodTimer: Auto print configured: enabled=true, threshold=3
D/MethodTimer: [com.example.myapplication.MainActivity.heavyComputation] 25ms (avg: 25.0ms, count: 1)
D/MethodTimer: [com.example.myapplication.MainActivity.testMethodTiming] 28ms (avg: 28.0ms, count: 1)
D/MethodTimer: [com.example.myapplication.MainActivity.simulateLongRunningTask] 2003ms (avg: 2003.0ms, count: 1)
D/MethodTimer: === 自动打印统计信息 (方法数量达到3) ===
D/MethodTimer: === Method Timer Statistics ===
D/MethodTimer: com.example.myapplication.MainActivity.simulateLongRunningTask: avg=2003.0ms, max=2003ms, min=2003ms, total=2003ms, count=1
D/MethodTimer: com.example.myapplication.MainActivity.testMethodTiming: avg=28.0ms, max=28ms, min=28ms, total=28ms, count=1
D/MethodTimer: com.example.myapplication.MainActivity.heavyComputation: avg=25.0ms, max=25ms, min=25ms, total=25ms, count=1
D/MethodTimer: === End of Statistics ===
页面显示时打印:
D/MethodTimer: === Method Timer Statistics ===
D/MethodTimer: com.example.myapplication.MainActivity.simulateLongRunningTask: avg=2003.0ms, max=2003ms, min=2003ms, total=2003ms, count=1
D/MethodTimer: com.example.myapplication.MainActivity.testMethodTiming: avg=28.0ms, max=28ms, min=28ms, total=28ms, count=1
D/MethodTimer: com.example.myapplication.MainActivity.heavyComputation: avg=25.0ms, max=25ms, min=25ms, total=25ms, count=1
D/MethodTimer: === End of Statistics ===
// 配置自动打印功能
public static void configAutoPrint(boolean enabled, int threshold)
// 检查当前是否在主线程
public static boolean isMainThread()
// 获取方法统计信息
public static MethodStats getMethodStats(String methodName)
// 获取所有方法统计信息
public static ConcurrentHashMap<String, MethodStats> getAllMethodStats()
// 清空所有统计信息
public static void clearStats()
// 手动打印所有方法统计信息到Logcat
public static void printAllStats()// 获取总耗时
public long getTotalDuration()
// 获取调用次数
public long getCallCount()
// 获取平均耗时
public double getAverageDuration()
// 获取最大耗时
public long getMaxDuration()
// 获取最小耗时
public long getMinDuration()methodTimer {
enabled.set(true) // 是否启用插件,默认 true
mainThreadOnly.set(true) // 是否只监控主线程,默认 true
minDuration.set(10L) // 最小记录时长(毫秒),默认 10ms
instrumentationScopeAll.set(false) // 插桩范围,默认 false
// false = 仅插桩项目自身代码(推荐)
// true = 同时插桩所有依赖库(原始行为)
}| 值 | 对应 AGP 枚举 | 插桩范围 | 适用场景 |
|---|---|---|---|
false(默认) |
InstrumentationScope.PROJECT |
仅项目自身的 class | 日常性能分析,推荐 |
true |
InstrumentationScope.ALL |
项目 + 所有 AAR/JAR 依赖 | 需要分析三方库耗时时使用 |
说明:
false是 1.0.1 起的新默认值。相比原来的ALL,它可以显著减少编译时的插桩工作量,并避免对 OkHttp、Retrofit、Kotlin 协程等三方库引入不必要的运行时计时开销。
- 字节码插桩: 使用ASM在编译时对方法进行插桩
- AGP 8.0+适配: 使用新的
AsmClassVisitorFactory和AndroidComponentsExtension - 主线程检测: 通过
Looper.myLooper() == Looper.getMainLooper()检查 - 性能统计: 使用
System.nanoTime()进行高精度计时 - 线程安全: 使用
ConcurrentHashMap和AtomicLong保证线程安全
插件会自动过滤以下类型的方法,避免影响系统性能:
- Android 系统类(
android.*) - AndroidX 类(
androidx.*) - Java 标准库类(
java.*) - Kotlin 标准库类(
kotlin.*) - 插件自身 Runtime 类(
com.hql.methodtimer.*) - R 资源类
- 构造函数和静态初始化块
- 抽象方法
提示:当
instrumentationScopeAll为false(默认)时,AGP 在流水线入口就已将所有依赖库 class 排除在外,上述过滤规则仅作用于项目自身代码,编译效率更高。
- 插件会为每个被插桩的方法引入两次
System.nanoTime()调用(约 1-2μs),对绝大多数方法性能影响可忽略 - 建议只在 debug 版本中启用,release 版本关闭:
enabled.set(BuildConfig.DEBUG) - 通过
minDuration过滤短耗时方法,可显著减少日志输出量 - 统计数据存储在内存中,应用重启后会清空
- 推荐将
instrumentationScopeAll保持默认的false,避免对三方库造成不必要的插桩开销
- Android Gradle Plugin: 8.0+
- Gradle: 7.0+
- JDK: 17+
- Kotlin: 2.0+