我正在使用泄漏金丝雀来检测我的 Android 应用程序中的潜在泄漏。但是当我开发 feature 时,它开始不时地进行堆转储,这很令人不安。我在 中使用它调试实现 .
dependencies {
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
}
现在,我想暂时禁用它。我怎样才能做到这一点 ?。我发现的一个答案是 LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
.dumpHeap(false)
.build();
LeakCanary.setConfig(config)
它可以工作,但在 Release模式下,此库不可用,因此无法编译。如果我使用 实现 而不是 调试实现 , 我会增加 apk 大小而不增加任何值(value)。有什么我可以做的吗?
最佳答案
import leakcanary.AppWatcher
import leakcanary.LeakCanary
fun configureLeakCanary(isEnable: Boolean = false) {
LeakCanary.config = LeakCanary.config.copy(dumpHeap = isEnable)
LeakCanary.showLeakDisplayActivityLauncherIcon(isEnable)
}
/**
* This method is added just to ensure we can build the demo application in release mode.
*/
fun configureLeakCanary(isEnable: Boolean = false) {
// This log is added just to supress kotlin unused variable lint warning and this will never be logger.
android.util.Log.i("Demo Application", "Leak canary is disabled - State isEnable - ${isEnable}")
// do nothing
}
if (BuildConfig.DEBUG) {
configureLeakCanary();
}
引用 - https://square.github.io/leakcanary/recipes/#disabling-leakcanary
关于android - 从调试应用程序中暂时禁用泄漏金丝雀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62570358/