AndroidJUnitRunner无法找到com.squareup.leakcanary.FailTestOnLeakRunListener类
解决LeakCanary Instrumentation测试集成启动失败问题
核心问题定位
报错Could not find extra class com.squareup.leakcanary.FailTestOnLeakRunListener的直接原因是监听器全类名配置错误,同时需排查依赖与测试环境兼容性问题。
具体解决步骤
1. 修正测试监听器全类名
LeakCanary 2.x版本中,FailTestOnLeakRunListener的正确全类名包含instrumentation子包,你遗漏了该路径。修改build.gradle中defaultConfig的配置:
testInstrumentationRunnerArgument "listener", "com.squareup.leakcanary.instrumentation.FailTestOnLeakRunListener"
2. 确认依赖配置有效性
你已添加正确的Instrumentation测试依赖,确保同步Gradle后无版本冲突:
androidTestImplementation 'com.squareup.leakcanary:leakcanary-android-instrumentation:2.9.1'
3. 排查测试Orchestrator兼容性
若使用androidx.test:orchestrator,可能存在类加载隔离问题。可先临时移除Orchestrator配置验证:
// 注释或移除该行,测试能否正常启动 // androidTestUtil 'androidx.test:orchestrator:1.1.1'
若移除后恢复正常,再重新配置Orchestrator并确保监听器类在测试进程中可见。
4. 清理缓存并重建项目
执行以下操作消除缓存影响:
- 点击Android Studio菜单:
File -> Invalidate Caches... -> Invalidate and Restart - 终端执行命令:
./gradlew clean assembleDebugAndroidTest
完成上述修改后,重新运行Instrumentation测试,验证应用启动与LeakCanary监听器是否正常工作。
内容的提问来源于stack exchange,提问作者Sourabh
相关产品推荐
相关产品推荐

