Bug 1285511 - Add multidex support to automation builds. r=sebastian draft
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 20 Jul 2016 13:06:40 -0700
changeset 390263 106d15f29db183387a77eb3cbc6968a7cac34286
parent 390262 d9631679e44acea4d31ab547633e0729768f987a
child 525968 8e3cacd90f334e31f3716d830cee408f0d9203ea
push id23636
push usermichael.l.comella@gmail.com
push dateWed, 20 Jul 2016 22:59:21 +0000
reviewerssebastian
bugs1285511
milestone50.0a1
Bug 1285511 - Add multidex support to automation builds. r=sebastian I initially tested my code with a branch around the multidex dependency: if (mozconfig.substs.MOZ_BUILD_MOBILE_ANDROID_WITH_GRADLE) { compile ...multidex... } I removed it because it seemed unnecessary - the code shouldn't be included if it's not referenced. I tested: * Locally with a build that did not exceed the method limit, pre-Lollipop * On try, with fx-team * On try, with beta MozReview-Commit-ID: APaOdlKd3QF
mobile/android/app/build.gradle
mobile/android/base/AppConstants.java.in
mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
--- a/mobile/android/app/build.gradle
+++ b/mobile/android/app/build.gradle
@@ -48,26 +48,40 @@ android {
         // For API 21+ - with multi dex, this will be faster for local development.
         local {
             // For multi dex, setting `minSdkVersion 21` allows the Android gradle plugin to
             // pre-DEX each module and produce an APK that can be tested on
             // Android Lollipop without time consuming DEX merging processes.
             minSdkVersion 21
             dexOptions {
                 preDexLibraries true
+                // We only call `MultiDex.install()` for the automation build flavor
+                // so this may not work. However, I don't think the multidex support
+                // library is necessary for 21+, so I expect that it will work.
                 multiDexEnabled true
             }
         }
         // For API < 21 - does not support multi dex because local development
         // is slow in that case. Most builds will not require multi dex so this
         // should not be an issue.
         localOld {
         }
         // Automation builds.
         automation {
+            dexOptions {
+                // As of FF48 on beta, the "test", "lint", etc. treeherder jobs fail because they
+                // exceed the method limit. Beta includes Adjust and its GPS dependencies, which
+                // increase the method count & explain the failures. Furthermore, this error only
+                // occurs on debug builds because we don't proguard.
+                //
+                // We enable multidex as an easy, quick-fix with minimal side effects but before we
+                // move to gradle for our production builds, we should re-evaluate this decision
+                // (bug 1286677).
+                multiDexEnabled true
+            }
         }
     }
 
     sourceSets {
         main {
             manifest.srcFile "${project.buildDir}/generated/source/preprocessed_manifest/AndroidManifest.xml"
 
             java {
@@ -166,16 +180,18 @@ android {
             // we have tests that start test servers and the bound ports
             // collide.  We'll fix this soon to have much faster test cycles.
             maxParallelForks 1
         }
     }
 }
 
 dependencies {
+    compile 'com.android.support:multidex:1.0.0'
+
     compile "com.android.support:support-v4:${mozconfig.substs.ANDROID_SUPPORT_LIBRARY_VERSION}"
     compile "com.android.support:appcompat-v7:${mozconfig.substs.ANDROID_SUPPORT_LIBRARY_VERSION}"
     compile "com.android.support:cardview-v7:${mozconfig.substs.ANDROID_SUPPORT_LIBRARY_VERSION}"
     compile "com.android.support:recyclerview-v7:${mozconfig.substs.ANDROID_SUPPORT_LIBRARY_VERSION}"
     compile "com.android.support:design:${mozconfig.substs.ANDROID_SUPPORT_LIBRARY_VERSION}"
     compile "com.android.support:customtabs:${mozconfig.substs.ANDROID_SUPPORT_LIBRARY_VERSION}"
 
     if (mozconfig.substs.MOZ_NATIVE_DEVICES) {
--- a/mobile/android/base/AppConstants.java.in
+++ b/mobile/android/base/AppConstants.java.in
@@ -1,17 +1,21 @@
 //#filter substitution
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; 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 org.mozilla.gecko;
 
+import android.content.Context;
 import android.os.Build;
+//#ifdef MOZ_BUILD_MOBILE_ANDROID_WITH_GRADLE
+import android.support.multidex.MultiDex;
+//#endif
 
 /**
  * A collection of constants that pertain to the build and runtime state of the
  * application. Typically these are sourced from build-time definitions (see
  * Makefile.in). This is a Java-side substitute for nsIXULAppInfo, amongst
  * other things.
  *
  * See also SysInfo.java, which includes some of the values available from
@@ -338,9 +342,26 @@ public class AppConstants {
 //#ifdef MOZ_ANDROID_CUSTOM_TABS
     true;
 //#else
     false;
 //#endif
 
     // (bug 1266820) Temporarily disabled since no one is working on it.
     public static final boolean SCREENSHOTS_IN_BOOKMARKS_ENABLED = false;
+
+    /**
+     * Enables multidex depending on build flags. For more information,
+     * see `multiDexEnabled true` in mobile/android/app/build.gradle.
+     *
+     * As a method, this shouldn't be in AppConstants, but it's
+     * the only semi-relevant Java file that we pre-process.
+     */
+    public static void maybeInstallMultiDex(final Context context) {
+//#ifdef MOZ_BUILD_MOBILE_ANDROID_WITH_GRADLE
+        if (BuildConfig.FLAVOR.equals("automation")) {
+            MultiDex.install(context);
+        }
+//#else
+        // Do nothing.
+//#endif
+    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
@@ -134,16 +134,22 @@ public class GeckoApplication extends Ap
         final Context applicationContext = getApplicationContext();
         GeckoBatteryManager.getInstance().start(applicationContext);
         GeckoNetworkManager.getInstance().start();
 
         mInBackground = false;
     }
 
     @Override
+    protected void attachBaseContext(Context base) {
+        super.attachBaseContext(base);
+        AppConstants.maybeInstallMultiDex(base);
+    }
+
+    @Override
     public void onCreate() {
         Log.i(LOG_TAG, "zerdatime " + SystemClock.uptimeMillis() + " - Fennec application start");
 
         mRefWatcher = LeakCanary.install(this);
 
         final Context context = getApplicationContext();
         HardwareUtils.init(context);
         Clipboard.init(context);