Bug 1238066 - Add LeakCanary to local (gradle) builds. r?nalexander draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Thu, 21 Jan 2016 12:35:21 +0100
changeset 323773 33b4e9949810434c7056d20084c725b753a4bf0d
parent 323642 63eb0ddd1cdbe82b4fdfd1c7bf9be38a95bc5234
child 513283 9391ac89a9755b817dd0546f9eabf0adf37f9870
push id9795
push users.kaspari@gmail.com
push dateThu, 21 Jan 2016 11:36:10 +0000
reviewersnalexander
bugs1238066
milestone46.0a1
Bug 1238066 - Add LeakCanary to local (gradle) builds. r?nalexander This patch adds LeakCanary to gradle builds and the no-op library to mach-based builds.
mobile/android/app/base/build.gradle
mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
mobile/android/base/moz.build
mobile/android/config/proguard/leakcanary-keeps.cfg
mobile/android/config/proguard/proguard.cfg
mobile/android/thirdparty/build.gradle
mobile/android/thirdparty/com/squareup/leakcanary/LeakCanary.java
mobile/android/thirdparty/com/squareup/leakcanary/RefWatcher.java
--- a/mobile/android/app/base/build.gradle
+++ b/mobile/android/app/base/build.gradle
@@ -113,16 +113,20 @@ dependencies {
     }
 
     if (mozconfig.substs.MOZ_ANDROID_GCM) {
         compile 'com.google.android.gms:play-services-basement:8.1.0'
         compile 'com.google.android.gms:play-services-base:8.1.0'
         compile 'com.google.android.gms:play-services-gcm:8.1.0'
     }
 
+    // Gradle based debug builds include LeakCanary. Mach based builds only include the no-op
+    // version of this library.
+    compile 'com.squareup.leakcanary:leakcanary-android:1.4-beta1'
+
     compile project(':thirdparty')
 
     testCompile 'junit:junit:4.12'
     testCompile 'org.robolectric:robolectric:3.0'
     testCompile 'org.simpleframework:simple-http:4.1.13'
     testCompile 'org.mockito:mockito-core:1.10.19'
 }
 
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
@@ -19,16 +19,18 @@ import org.mozilla.gecko.util.ThreadUtil
 
 import android.app.Application;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.os.SystemClock;
 import android.util.Log;
 
+import com.squareup.leakcanary.LeakCanary;
+
 import java.io.File;
 
 public class GeckoApplication extends Application 
     implements ContextGetter {
     private static final String LOG_TAG = "GeckoApplication";
 
     private static volatile GeckoApplication instance;
 
@@ -122,16 +124,18 @@ public class GeckoApplication extends Ap
 
         mInBackground = false;
     }
 
     @Override
     public void onCreate() {
         Log.i(LOG_TAG, "zerdatime " + SystemClock.uptimeMillis() + " - Fennec application start");
 
+        LeakCanary.install(this);
+
         final Context context = getApplicationContext();
         HardwareUtils.init(context);
         Clipboard.init(context);
         FilePicker.init(context);
         DownloadsIntegration.init();
         HomePanelsManager.getInstance().init(context);
 
         // This getInstance call will force initialization of the NotificationHelper, but does nothing with the result
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -777,16 +777,18 @@ gtjar.sources += [ thirdparty_source_dir
     'com/nineoldandroids/util/Property.java',
     'com/nineoldandroids/util/ReflectiveProperty.java',
     'com/nineoldandroids/view/animation/AnimatorProxy.java',
     'com/nineoldandroids/view/ViewHelper.java',
     'com/nineoldandroids/view/ViewPropertyAnimator.java',
     'com/nineoldandroids/view/ViewPropertyAnimatorHC.java',
     'com/nineoldandroids/view/ViewPropertyAnimatorICS.java',
     'com/nineoldandroids/view/ViewPropertyAnimatorPreHC.java',
+    'com/squareup/leakcanary/LeakCanary.java',
+    'com/squareup/leakcanary/RefWatcher.java',
     'com/squareup/picasso/Action.java',
     'com/squareup/picasso/AssetBitmapHunter.java',
     'com/squareup/picasso/BitmapHunter.java',
     'com/squareup/picasso/Cache.java',
     'com/squareup/picasso/Callback.java',
     'com/squareup/picasso/ContactsPhotoBitmapHunter.java',
     'com/squareup/picasso/ContentStreamBitmapHunter.java',
     'com/squareup/picasso/DeferredRequestCreator.java',
new file mode 100644
--- /dev/null
+++ b/mobile/android/config/proguard/leakcanary-keeps.cfg
@@ -0,0 +1,10 @@
+# LeakCanary
+-keep class org.eclipse.mat.** { *; }
+-keep class com.squareup.leakcanary.** { *; }
+-keep class com.squareup.haha.** { *; }
+
+# For Android 6.0 compatibility
+-dontwarn android.app.Notification
+
+# With LeakCanary 1.4-beta1 this creates a pile of warnings
+-dontwarn com.squareup.haha.**
--- a/mobile/android/config/proguard/proguard.cfg
+++ b/mobile/android/config/proguard/proguard.cfg
@@ -242,8 +242,11 @@
 
 -include "play-services-keeps.cfg"
 
 # Don't print spurious warnings from the support library.
 # See: http://stackoverflow.com/questions/22441366/note-android-support-v4-text-icucompatics-cant-find-dynamically-referenced-cl
 -dontnote android.support.**
 
 -include "adjust-keeps.cfg"
+
+-include "leakcanary-keeps.cfg"
+
--- a/mobile/android/thirdparty/build.gradle
+++ b/mobile/android/thirdparty/build.gradle
@@ -23,16 +23,20 @@ android {
     sourceSets {
         main {
             manifest.srcFile 'AndroidManifest.xml'
             java {
                 srcDir '.'
                 if (!mozconfig.substs.MOZ_INSTALL_TRACKING) {
                     exclude 'com/adjust/**'
                 }
+
+                // Exclude LeakCanary: It will be added again via a gradle dependency. This version
+                // here is only the no-op library for mach-based builds.
+                exclude 'com/squareup/leakcanary/**'
             }
         }
     }
 }
 
 dependencies {
     compile 'com.android.support:support-v4:23.0.1'
 }
new file mode 100644
--- /dev/null
+++ b/mobile/android/thirdparty/com/squareup/leakcanary/LeakCanary.java
@@ -0,0 +1,17 @@
+package com.squareup.leakcanary;
+
+import android.app.Application;
+
+/**
+ * A no-op version of {@link LeakCanary} that can be used in release builds.
+ */
+public final class LeakCanary {
+
+    public static RefWatcher install(Application application) {
+        return RefWatcher.DISABLED;
+    }
+
+    private LeakCanary() {
+        throw new AssertionError();
+    }
+}
new file mode 100644
--- /dev/null
+++ b/mobile/android/thirdparty/com/squareup/leakcanary/RefWatcher.java
@@ -0,0 +1,19 @@
+package com.squareup.leakcanary;
+
+/**
+ * No-op implementation of {@link RefWatcher} for release builds. Please use {@link
+ * RefWatcher#DISABLED}.
+ */
+public final class RefWatcher {
+
+    public static final RefWatcher DISABLED = new RefWatcher();
+
+    private RefWatcher() {
+    }
+
+    public void watch(Object watchedReference) {
+    }
+
+    public void watch(Object watchedReference, String referenceName) {
+    }
+}