Is my leakcanary working? How to know?

Does my application class need to be explicitly called?

No.

How do I know if it's all working?

Leak something intentionally. For example, assign your launcher activity instance to a static field.


First, check if you are attached to a debugger? LeakCanary ignores leak detection when debugging to avoid false positives.

Second, Add the LeakCanary via Gradle and then do the following:

class App : Application() {

    companion object {
        @JvmStatic
        fun getRefWatcher(context: Context): RefWatcher {
            val applicationContext = context.applicationContext as App
            return applicationContext.refWatcher
        }
    }

    private lateinit var refWatcher: RefWatcher

    override fun onCreate() {
        super.onCreate()
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        refWatcher = LeakCanary.install(this)
    }
}

In your Fragment (best you use a BaseFragment)

override fun onDestroy() {

   if (BuildConfig.DEBUG) {
      activity?.let {
         App.getRefWatcher(it).watch(this)
      }
      super.onDestroy()
}

Then in one of your Sub-Activities with a Fragment do:

class MemLeakFragment : BaseFragment() {

    companion object {
        @JvmStatic
        lateinit var memleak: Activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        activity?.let {
            memleak = it
        }
    }
}

Open the Activity with the memleak Fragment and close it with a back press. Wait a bit and LeakCanary will report the memory leak, this can take a while...