Disable Leak Canary temporarily from Debug apps

Viewed 5745

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 ?

2 Answers
  • Step 1 - Continued to keep the Leak canary dependency as debugImplementation
  • Step 2 - Create a Util method in the src/debug/java/
   

     import leakcanary.AppWatcher
        import leakcanary.LeakCanary
            fun configureLeakCanary(isEnable: Boolean = false) {
                LeakCanary.config = LeakCanary.config.copy(dumpHeap = isEnable)
                LeakCanary.showLeakDisplayActivityLauncherIcon(isEnable)
            }

  • Step 3 - Create the same Util function in the src/release/java to suppress compiler errors

    /**
     * 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
    }

  • Step 4 - In the Application class onCreate()

     if (BuildConfig.DEBUG) {
       configureLeakCanary();
     }

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

I don't believe it is currently possible to completely disable Leaccanary because it activates some processes just by including it in dependencies.

As I see if I do suggested setup it writes: D/LeakCanary: LeakCanary is currently disabled: LeakCanary.Config.dumpHeap is set to false.

But on the other hand it still is doing something else like:

W/instruments.ap: Verification of void leakcanary.internal.HeapDumpTrigger.showNoMoreRetainedObjectNotification() took 356.993ms (394.97 bytecodes/s) (3992B approximate peak alloc)

And it changes some parameters to all started threads looks like as I have multitude of:

D/LeakCanary: Setting up flushing for Thread[LeakCanary-Heap-Dump,5,main]

And this is just what you actually see in log, it can be far more happening there behind even tho heap dump is disabled, it looks like its enabled by design just bu including as dependency and than you can just partially disable it by turning off heap dump and also I had to maunaly turn off calling of expectWeaklyReachable so I don't get additional log statements when it is "disabled".

Related