Bug 1238066 - Add LeakCanary to local (gradle) builds. r=nalexander
authorSebastian Kaspari <s.kaspari@gmail.com>
Thu, 21 Jan 2016 12:35:21 +0100
changeset 281487 2ff2a6523bc06d93a5d55d848c8638296f0f574f
parent 281486 0be45a5671ba5b5513c2e0da21185b5ba2531011
child 281488 48571bf11be832658eabe82402739b4f42428d98
push id17205
push users.kaspari@gmail.com
push dateMon, 25 Jan 2016 12:53:35 +0000
treeherderfx-team@2ff2a6523bc0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
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
@@ -117,16 +117,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 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:6.0.1'
     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
@@ -773,16 +773,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,7 @@
+# LeakCanary
+-keep class org.eclipse.mat.** { *; }
+-keep class com.squareup.leakcanary.** { *; }
+-keep class com.squareup.haha.** { *; }
+
+# 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,21 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+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,20 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+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) {}
+}