androidmemory-leaksleakcanary

Disable Leak Canary temporarily from Debug apps


I am using leak canary to detect potential leaks in my Android application. But when I was developing feature , it is quite disturbing as it starts taking heap dumps time to time. I am using it in debugImplemetation.

dependencies {
  debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
} 

Now , I want to disable it temporarily. How can I do that ?. One anwer I found is

    LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
                        .dumpHeap(false)
                        .build();
                LeakCanary.setConfig(config)

It works but In release mode this library is not available so it will not compile. If I use implementation instead of debugImplemetation , I will increase apk size and not adding any value. Is there anything I can do ?


Solution

  •    
    
         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();
         }
    
    

    Reference - https://square.github.io/leakcanary/recipes/#disabling-leakcanary