Android 从调试应用程序中暂时禁用泄漏金丝雀
我正在使用泄漏金丝雀来检测Android应用程序中的潜在泄漏。但当我开发这个特性时,它非常令人不安,因为它开始时不时地进行堆转储。我正在调试实现中使用它Android 从调试应用程序中暂时禁用泄漏金丝雀,android,memory-leaks,leakcanary,Android,Memory Leaks,Leakcanary,我正在使用泄漏金丝雀来检测Android应用程序中的潜在泄漏。但当我开发这个特性时,它非常令人不安,因为它开始时不时地进行堆转储。我正在调试实现中使用它 dependencies { debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4' } 现在,我想暂时禁用它。我该怎么做?。我找到的一个答案是 LeakCanary.Config config = LeakCanary.getConfig().n
dependencies {
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
}
现在,我想暂时禁用它。我该怎么做?。我找到的一个答案是
LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
.dumpHeap(false)
.build();
LeakCanary.setConfig(config)
它可以工作,但在发布模式下,此库不可用,因此无法编译。如果我使用实现而不是调试实现,我将增加apk大小而不增加任何值。有什么我能做的吗?- 第1步-继续保持泄漏金丝雀依赖项作为调试实现
- 步骤2-在src/debug/java中创建一个Util方法/
- 步骤3-在src/release/java中创建相同的Util函数以抑制编译器错误
- 步骤4-在应用程序类onCreate()中
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();
}