因为我们只需要修改 leakcanary-android/src/main/java/com/squareup/leakcanary/LeakCanary.java 中的一个函数,因此很容易想到继承+重写的方法。但是,LeakCanary 类是一个 final 类,无法被继承。既然这样,我们可以重新写一个类,内容和 LeakCanary 类保持一致,然后再修改上面提到的部分。之所以这样做,是因为 LeakCanary 类正是我们在接入时调用的类(LeakCanary.install(this))。
此方法接入 LeakCanary 的方法与之前文章里提到的一样。但需要在项目的代码目录,新建 com/squareup/leakcanary 包,然后在这个包内新建一个类,名字随意,这里取 LeakCanaryWithoutDisplay.java。直接将 LeakCanary.java 中的代码复制到这里,修改 public final class LeakCanary 为 public final class LeakCanaryWithoutDisplay、private LeakCanary() 为 private LeakCanaryWithoutDisplay(),即:
/* * Copyright (C) 2015 Square, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.squareup.leakcanary;
/** * Creates a {@link RefWatcher} that works out of the box, and starts watching activity * references (on ICS+). */ publicstatic RefWatcher install(Application application) { return install(application, DisplayLeakService.class, AndroidExcludedRefs.createAppDefaults().build()); }
/** * Creates a {@link RefWatcher} that reports results to the provided service, and starts watching * activity references (on ICS+). */ publicstatic RefWatcher install(Application application, Class<? extends AbstractAnalysisResultService> listenerServiceClass, ExcludedRefs excludedRefs) { if (isInAnalyzerProcess(application)) { return RefWatcher.DISABLED; } enableDisplayLeakActivity(application); HeapDump.ListenerheapDumpListener= newServiceHeapDumpListener(application, listenerServiceClass); RefWatcherrefWatcher= androidWatcher(application, heapDumpListener, excludedRefs); ActivityRefWatcher.installOnIcsPlus(application, refWatcher); return refWatcher; }
/** * Creates a {@link RefWatcher} with a default configuration suitable for Android. */ publicstatic RefWatcher androidWatcher(Context context, HeapDump.Listener heapDumpListener, ExcludedRefs excludedRefs) { LeakDirectoryProviderleakDirectoryProvider=newDefaultLeakDirectoryProvider(context); DebuggerControldebuggerControl=newAndroidDebuggerControl(); AndroidHeapDumperheapDumper=newAndroidHeapDumper(context, leakDirectoryProvider); heapDumper.cleanup(); Resourcesresources= context.getResources(); intwatchDelayMillis= resources.getInteger(R.integer.leak_canary_watch_delay_millis); AndroidWatchExecutorexecutor=newAndroidWatchExecutor(watchDelayMillis); returnnewRefWatcher(executor, debuggerControl, GcTrigger.DEFAULT, heapDumper, heapDumpListener, excludedRefs); }
/** * Whether the current process is the process running the {@link HeapAnalyzerService}, which is * a different process than the normal app process. */ publicstaticbooleanisInAnalyzerProcess(Context context) { return isInServiceProcess(context, HeapAnalyzerService.class); }