如何忽略LeakCanary中的某些类? [英] How to ignore certain classes from LeakCanary?

查看:381
本文介绍了如何忽略LeakCanary中的某些类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我一个有效的例子,说明如何忽略LeakCanary中的某些类吗?

Can someone give me a working example of how to ignoring certain classes from LeakCanary?

我在看这个示例时忽略了LeakCanary中第三方库中的某些类,但是我不知道该在我的应用程序中放置什么位置.我把它放在我的Application类中,但是这些变量和方法有错误:isInAnalyzerProcess,enableDisplayLeakActivity,应用程序,androidWatcher

I was looking at this example for ignoring certain classes from third party library in LeakCanary, but I couldn't figure out where to put this in my application. I put this in my Application class, but there are error from these variables and methods: isInAnalyzerProcess, enableDisplayLeakActivity, application, androidWatcher

public class DebugExampleApplication extends ExampleApplication {
  protected RefWatcher installLeakCanary() {
    if (isInAnalyzerProcess(this)) {
      return RefWatcher.DISABLED;
    } else {
      ExcludedRefs excludedRefs = AndroidExcludedRefs.createAppDefaults().build();
      enableDisplayLeakActivity(application);
      ServiceHeapDumpListener heapDumpListener = new ServiceHeapDumpListener(application, DisplayLeakService.class);
      final RefWatcher refWatcher = androidWatcher(application, heapDumpListener, excludedRefs);
      registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        public void onActivityDestroyed(Activity activity) {
          if (activity instanceof ThirdPartyActivity) {
              return;
          }
          refWatcher.watch(activity);
        }
        // ...
      });
      return refWatcher;
    }
  }
}

推荐答案

感谢CommonsWare,它调用LeakCanary工程上的方法和变量.这是一个忽略LeakCanary中某些参考或活动的完整示例.查看评论:IGNORE引用和IGNORE活动.

Thanks to CommonsWare, calling the methods and variables on LeakCanary works. Here is a complete example for ignoring certain References or Activities in LeakCanary. Look at the comments: IGNORE Rreferences and IGNORE Activities.

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import com.squareup.leakcanary.AndroidExcludedRefs;
import com.squareup.leakcanary.DisplayLeakService;
import com.squareup.leakcanary.ExcludedRefs;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.squareup.leakcanary.ServiceHeapDumpListener;

public class MyApplication extends Application {

    // LeakCanary for memory leak detection
    private RefWatcher refWatcher;
    public static RefWatcher getRefWatcher(Context context) {
        MyApplication application = (MyApplication) context.getApplicationContext();
        return application.refWatcher;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        refWatcher = installLeakCanary();
    }


    /**
     * Excluding known memory leaks from third party libraries
     * @return
     */
    protected RefWatcher installLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return RefWatcher.DISABLED;
        } else {

            // IGNORE References: Update or add reference class and context name in instanceField
            ExcludedRefs excludedRefs = AndroidExcludedRefs.createAppDefaults()
                    .instanceField("com.example.third.party.TheirClassOne", "context")
                    .instanceField("com.example.third.party.TheirClassTwo", "context")
                    .build();

            LeakCanary.enableDisplayLeakActivity(this);
            ServiceHeapDumpListener heapDumpListener = new ServiceHeapDumpListener(this, DisplayLeakService.class);
            final RefWatcher refWatcher = LeakCanary.androidWatcher(this, heapDumpListener, excludedRefs);
            registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

                }

                @Override
                public void onActivityStarted(Activity activity) {

                }

                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

                }

                @Override
                public void onActivityPaused(Activity activity) {

                }

                @Override
                public void onActivityStopped(Activity activity) {

                }

                @Override
                public void onActivityDestroyed(Activity activity) {
                    //IGNORE Activities: Update or add the class name here to ingore the memory leaks from those actvities
                    if (activity instanceof ThirdPartyOneActivity) return;
                    if (activity instanceof ThirdPartyTwoActivity) return;
                    if (activity instanceof ThirdPartyThreeActivity) return;
                    refWatcher.watch(activity);
                }

                @Override
                public void onActivityResumed(Activity activity) {

                }
            });
            return refWatcher;
        }
    }

}

这篇关于如何忽略LeakCanary中的某些类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆