-Added LeakCanary to debug build for memory detection on activities and fragments.

-Added LeakCanary no-op lib to release and beta builds.
This commit is contained in:
John Zhen Mo 2018-02-08 12:46:54 -08:00
parent e88312659b
commit 622d698ff8
4 changed files with 41 additions and 0 deletions

View File

@ -89,4 +89,8 @@ dependencies {
implementation 'frankiesardo:icepick:3.2.0'
annotationProcessor 'frankiesardo:icepick-processor:3.2.0'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
betaImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
}

View File

@ -4,6 +4,10 @@ import android.content.Context;
import android.support.multidex.MultiDex;
import com.facebook.stetho.Stetho;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import java.util.concurrent.TimeUnit;
public class DebugApp extends App {
private static final String TAG = DebugApp.class.toString();
@ -41,4 +45,11 @@ public class DebugApp extends App {
// Initialize Stetho with the Initializer
Stetho.initialize(initializer);
}
@Override
protected RefWatcher installLeakCanary() {
return LeakCanary.refWatcher(this)
.watchDelay(5, TimeUnit.SECONDS)
.buildAndInstall();
}
}

View File

@ -10,6 +10,8 @@ import android.util.Log;
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import org.acra.ACRA;
import org.acra.config.ACRAConfiguration;
@ -54,6 +56,7 @@ import io.reactivex.plugins.RxJavaPlugins;
public class App extends Application {
protected static final String TAG = App.class.toString();
private RefWatcher refWatcher;
@SuppressWarnings("unchecked")
private static final Class<? extends ReportSenderFactory>[] reportSenderFactoryClasses = new Class[]{AcraReportSenderFactory.class};
@ -69,6 +72,13 @@ public class App extends Application {
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
refWatcher = installLeakCanary();
// Initialize settings first because others inits can use its values
SettingsActivity.initSettings(this);
@ -157,4 +167,12 @@ public class App extends Application {
mNotificationManager.createNotificationChannel(mChannel);
}
public static RefWatcher getRefWatcher(Context context) {
final App application = (App) context.getApplicationContext();
return application.refWatcher;
}
protected RefWatcher installLeakCanary() {
return RefWatcher.DISABLED;
}
}

View File

@ -13,6 +13,7 @@ import android.view.View;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.squareup.leakcanary.RefWatcher;
import icepick.Icepick;
@ -67,6 +68,13 @@ public abstract class BaseFragment extends Fragment {
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
}
@Override
public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = App.getRefWatcher(getActivity());
refWatcher.watch(this);
}
/*//////////////////////////////////////////////////////////////////////////
// Init
//////////////////////////////////////////////////////////////////////////*/