From 7abdfa72e372d927bf2dec3185e05588cb1a94b7 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Fri, 19 Jan 2018 13:14:38 +0100 Subject: #8822 adds leakCanary library to Bitmask for analyzing memory leaks --- app/build.gradle | 13 +++++++++++ .../java/se/leap/bitmaskclient/BitmaskApp.java | 26 +++++++++++++++++++++- .../drawer/NavigationDrawerFragment.java | 7 ++++++ 3 files changed, 45 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index cb865ecd..a71cff57 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -77,6 +77,8 @@ dependencies { androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.6.3' testCompile 'junit:junit:4.12' testCompile 'org.json:json:20170516' + debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4' + releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' compile 'com.jakewharton:butterknife:6.1.0' provided 'com.squareup.dagger:dagger-compiler:1.2.2' compile 'com.github.pedrovgs:renderers:1.5' @@ -92,6 +94,17 @@ dependencies { compile 'com.android.support:support-fragment:26.1.0' } +// Ensure the no-op dependency is always used in JVM tests. +configurations.all { config -> + if (config.name.contains('UnitTest')) { + config.resolutionStrategy.eachDependency { details -> + if (details.requested.group == 'com.squareup.leakcanary' && details.requested.name == 'leakcanary-android') { + details.useTarget(group: details.requested.group, name: 'leakcanary-android-no-op', version: details.requested.version) + } + } + } +} + def processFileInplace(file, Closure processText) { def text = file.text file.write(processText(text)) diff --git a/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java b/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java index 88a01b62..f9e45b79 100644 --- a/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java +++ b/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java @@ -1,6 +1,10 @@ package se.leap.bitmaskclient; import android.app.Application; +import android.content.Context; + +import com.squareup.leakcanary.LeakCanary; +import com.squareup.leakcanary.RefWatcher; /** * Created by cyberta on 24.10.17. @@ -8,11 +12,31 @@ import android.app.Application; public class BitmaskApp extends Application { + private RefWatcher refWatcher; + @Override 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 = LeakCanary.install(this); + // Normal app init code...*/ PRNGFixes.apply(); - //TODO: add LeakCanary! } + /** + * Use this method to get a RefWatcher object that checks for memory leaks in the given context. + * Call refWatcher.watch(this) to check if all references get garbage collected. + * @param context + * @return the RefWatcher object + */ + public static RefWatcher getRefWatcher(Context context) { + BitmaskApp application = (BitmaskApp) context.getApplicationContext(); + return application.refWatcher; + } + + } diff --git a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java index 566134dd..84c407d7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java @@ -36,6 +36,7 @@ import se.leap.bitmaskclient.fragments.LogFragment; import se.leap.bitmaskclient.userstatus.User; import se.leap.bitmaskclient.userstatus.UserStatusFragment; +import static se.leap.bitmaskclient.BitmaskApp.getRefWatcher; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; @@ -287,6 +288,12 @@ public class NavigationDrawerFragment extends Fragment { return super.onOptionsItemSelected(item); } + @Override + public void onDestroy() { + super.onDestroy(); + getRefWatcher(getActivity()).watch(this); + } + /** * Per the navigation drawer design guidelines, updates the action bar to show the global app * 'context', rather than just what's in the current screen. -- cgit v1.2.3