androidkotlinleakcanary

Manual install in Leak Canary 2?


I've been one of user who uses Leak Canary 2. Thanks to this stuff, I could fix memory leak bug in my app. I recently noticed that (I may be late to know this) there was update Leak Canary 2.2, so I applied newer version to my app by adding line below in Manifest

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'

Before implementing leak canary, I wanted to make it start detecting memory leaks (also installing "Leaks" application) only when I turn on Developer mode thing in my app via setting. I'm sharing my app as apk file to make users able to test and find bug easily. If "Leaks" application is installed suddenly when users install my app even though they didn't turn on developer mode, they may not know what this app is and actually, it's only useful for me because other users won't understand what this app is saying. But according to this site, I don't need to put any codes to install leak canary; it will be auto-installed. I cannot prevent it from being installed to users' devices.

class MainActivity : AppCompatActivity() {

    companion object {
        var installed = false
        @JvmField
        var watcher: RefWatcher? = null
    }

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

        ....

        val shared = getSharedPreferences(StaticStore.CONFIG, Context.MODE_PRIVATE)

        if (!installed && shared.getBoolean("DEV_MODE", false)) {
            watcher = LeakCanary.install(application)
            installed = true
        }

        ....
    }
}

Code above is how I performed manual installing before Leak Canary 2.2. Referred from this answer. When app checks boolean (DEV_MODE) which is from shared preferences is true, then it allows installing, and make watcher checks other activities or threads. If I don't call LeakCanary.install(application), "Leaks" application won't be installed, so users can use my app without seeing this additional application. Of course, if they turn on developer mode, they can use memory leak detecting feature.

But there aren't method like LeakCanaray.install() (also RefWatcher too) now, so I cannot perform manual install like before...

Are there any ways to perform manual install in Leak Canary 2.2?

Do I have to give up making it not installed automatically and notice users "Leaks" application will be installed?


Solution

  • With the recent v2.6 update, the manual installation of LeakCanary changed.

    Now you are required to add the following boolean resource:

    <bool name="leak_canary_watcher_auto_install">false</bool>
    

    And run the following API whenever you are ready to do a manual install with the default Watchers (Activity, Fragment, RootView, Service):

    JAVA:

    AppWatcher.INSTANCE.manualInstall(app, WATCH_DURATION_MILLISECONDS);
    

    Kotlin:

    AppWatcher.manualInstall(app, WATCH_DURATION_MILLISECONDS);
    

    For more details read the official documentation here.