Package 

Object AppWatcher


  • 
    public class AppWatcher
    
                        

    The entry point API for using ObjectWatcher in an Android app. AppWatcher.objectWatcher is in charge of detecting retained objects, and AppWatcher is auto configured on app start to pass it activity and fragment instances. Call ObjectWatcher.watch on objectWatcher to watch any other object that you expect to be unreachable.

    • Method Detail

      • manualInstall

        @JvmOverloads() final Unit manualInstall(Application application, Long retainedDelayMillis, List<InstallableWatcher> watchersToInstall)

        Enables usage of AppWatcher.objectWatcher which will expect passed in objects to become weakly reachable within retainedDelayMillis ms and if not will trigger LeakCanary (if LeakCanary is in the classpath).

        In the main process, this method is automatically called with default parameter values on app startup. You can call this method directly to customize the installation, however you must first disable the automatic call by overriding the leak_canary_watcher_auto_install boolean resource:

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
          <bool name="leak_canary_watcher_auto_install">false</bool>
        </resources>

        watchersToInstall can be customized to a subset of the default app watchers:

        val watchersToInstall = AppWatcher.appDefaultWatchers(application)
          .filter { it !is RootViewWatcher }
        AppWatcher.manualInstall(
          application = application,
          watchersToInstall = watchersToInstall
        )

        watchersToInstall can also be customized to ignore specific instances (e.g. here ignoring leaks of BadSdkLeakingFragment):

        val watchersToInstall = AppWatcher.appDefaultWatchers(application, ReachabilityWatcher { watchedObject, description ->
          if (watchedObject !is BadSdkLeakingFragment) {
            AppWatcher.objectWatcher.expectWeaklyReachable(watchedObject, description)
          }
        })
        AppWatcher.manualInstall(
          application = application,
          watchersToInstall = watchersToInstall
        )
      • manualInstall

        @JvmOverloads() final Unit manualInstall(Application application, Long retainedDelayMillis)

        Enables usage of AppWatcher.objectWatcher which will expect passed in objects to become weakly reachable within retainedDelayMillis ms and if not will trigger LeakCanary (if LeakCanary is in the classpath).

        In the main process, this method is automatically called with default parameter values on app startup. You can call this method directly to customize the installation, however you must first disable the automatic call by overriding the leak_canary_watcher_auto_install boolean resource:

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
          <bool name="leak_canary_watcher_auto_install">false</bool>
        </resources>

        watchersToInstall can be customized to a subset of the default app watchers:

        val watchersToInstall = AppWatcher.appDefaultWatchers(application)
          .filter { it !is RootViewWatcher }
        AppWatcher.manualInstall(
          application = application,
          watchersToInstall = watchersToInstall
        )

        watchersToInstall can also be customized to ignore specific instances (e.g. here ignoring leaks of BadSdkLeakingFragment):

        val watchersToInstall = AppWatcher.appDefaultWatchers(application, ReachabilityWatcher { watchedObject, description ->
          if (watchedObject !is BadSdkLeakingFragment) {
            AppWatcher.objectWatcher.expectWeaklyReachable(watchedObject, description)
          }
        })
        AppWatcher.manualInstall(
          application = application,
          watchersToInstall = watchersToInstall
        )
      • manualInstall

        @JvmOverloads() final Unit manualInstall(Application application)

        Enables usage of AppWatcher.objectWatcher which will expect passed in objects to become weakly reachable within retainedDelayMillis ms and if not will trigger LeakCanary (if LeakCanary is in the classpath).

        In the main process, this method is automatically called with default parameter values on app startup. You can call this method directly to customize the installation, however you must first disable the automatic call by overriding the leak_canary_watcher_auto_install boolean resource:

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
          <bool name="leak_canary_watcher_auto_install">false</bool>
        </resources>

        watchersToInstall can be customized to a subset of the default app watchers:

        val watchersToInstall = AppWatcher.appDefaultWatchers(application)
          .filter { it !is RootViewWatcher }
        AppWatcher.manualInstall(
          application = application,
          watchersToInstall = watchersToInstall
        )

        watchersToInstall can also be customized to ignore specific instances (e.g. here ignoring leaks of BadSdkLeakingFragment):

        val watchersToInstall = AppWatcher.appDefaultWatchers(application, ReachabilityWatcher { watchedObject, description ->
          if (watchedObject !is BadSdkLeakingFragment) {
            AppWatcher.objectWatcher.expectWeaklyReachable(watchedObject, description)
          }
        })
        AppWatcher.manualInstall(
          application = application,
          watchersToInstall = watchersToInstall
        )
      • getObjectWatcher

         final ObjectWatcher getObjectWatcher()

        The ObjectWatcher used by AppWatcher to detect retained objects. Only set when isInstalled is true.