2

Building Apps with over 65K methods requires the following in the manifest:

  <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
  </application>

Using Google Analytics needs the following in the manifest:

  <application
        ...
        android:name="AnalyticsApplication">
        ...
  </application>

How can these two be reconciled?

Hong
  • 17,643
  • 21
  • 81
  • 142

2 Answers2

4

Have you tried MultiDex.install in your application class?

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

It works fine for me without changing the app name in the manifest. See How to enable multidexing with the new Android Multidex support library for more info.

Edit:

You can create a new class that extends Application

public class AnalyticsApplication extends Application {
  private Tracker mTracker;

  @Override
  protected void attachBaseContext(Context base) {
      super.attachBaseContext(base);
      MultiDex.install(this);
  }

  /**
   * Gets the default {@link Tracker} for this {@link Application}.
   * @return tracker
   */
  synchronized public Tracker getDefaultTracker() {
    if (mTracker == null) {
      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
      mTracker = analytics.newTracker(R.xml.global_tracker);
    }
    return mTracker;
  }
}

Then in your manifest:

<application
    ...
    android:name="AnalyticsApplication">
    ...
</application>
Community
  • 1
  • 1
pt2121
  • 11,720
  • 8
  • 52
  • 69
  • Thank you. Sorry for my ignorance. Where do I add this code? I assume this does not mean that I have to add this code to every class. – Hong Dec 04 '15 at 19:30
  • 1
    No, you can add it only in your Application class. I updated my answer. Let me know if it helps. Thanks! – pt2121 Dec 04 '15 at 20:03
  • Thank you for the editing. Is this class used anywhere else in the app? In other words, is this just a sort of dummy class not used by any activities of the app? – Hong Dec 04 '15 at 21:20
  • 1
    It's the main application class. it has callbacks for application level lifecycle. e.g. onCreate() is called when an app is created. Roughly speaking, Activity represents a screen but Application represents the whole app. Anyway, you can treat it as a dummy class if you don't have any global variables/methods. more info at http://developer.android.com/reference/android/app/Application.html – pt2121 Dec 04 '15 at 21:25
  • 2
    Thank you so much for your patience. Everything makes sense now. – Hong Dec 04 '15 at 21:38
0

1) Enable multidex in build.gradle

android {
    defaultConfig {
        // your stuff
        multiDexEnabled true
    }
}

2) Need to subclass Application and add this

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}
mbmc
  • 5,024
  • 5
  • 25
  • 53