1、添加依赖
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
2、Application中初始化leakcanary
//在自己的Application中添加如下代码
public static RefWatcher getRefWatcher(Context context) {
App application = (App) context.getApplicationContext();
return application.refWatcher;
}
//在自己的Application中添加如下代码
private RefWatcher refWatcher;
@Override
public void onCreate() {
super.onCreate();
//在自己的Application中添加如下代码
refWatcher = LeakCanary.install(this);
}
3、Activity中使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在自己的应用初始Activity中加入如下两行代码
RefWatcher refWatcher = MyApp.getRefWatcher(this);
refWatcher.watch(this);
}