25

When I add to my project the multidex:true, and make an Application class that extends from the MultiDexApplication, my project build time passed from 20 sec to around 90 sec.How to do some faster?

  • AFAIK, there is no way to make it faster. The only solution is to reduce the numbers of methods in your app and get rid of `MultiDex`. Even the official documentation says *This means that routine builds performed as part of the development process with multidex typically take longer and can potentially slow your development process.* – Vladimir Mironov May 11 '15 at 21:01
  • Are you sure all the libraries imported are really necessary for your app? If no you can remove something and avoid multidex – aorlando May 11 '15 at 21:03
  • I need to do this, when add to dependencies all libraries for google calendar api. In these theme I describe its problem: http://stackoverflow.com/questions/30104925/multidex-issue-execution-failed-for-task-appdexdebug . Maybe you now how to around the multidex? I think this can be avoided, but I dont now another methods. Thanks) – Александр Шевчук May 11 '15 at 21:26

4 Answers4

27

If you are like me who already tried Vic Vu's solution but still can't avoid enabling multiDex then you can try this (as long as your are using a device that has Android 5.0 and above).

Note This will only speed up your development build. Your production build will still be slow.

Basically you need to introduce 2 product flavors one for dev and one for prod.

Add multiDexEnabled true

android {
    productFlavors {
        // Define separate dev and prod product flavors.
        dev {
            // dev utilizes minSDKVersion = 21 to allow 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
        }
        prod {
            // The actual minSdkVersion for the application.
            minSdkVersion 14
        }
    }
          ...
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                 'proguard-rules.pro'
        }
        defaultConfig {
            applicationId "com.something.something"
            targetSdkVersion 23
            versionCode 1
            versionName "1.0.0"

            multiDexEnabled true
        }
    }
dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

And I have a class which extends Application so I had to override attachBaseContext()

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

If you are not extending Application simply use MultiDexApplication in your AndroidManifest.xml application tag.

Ensure that in your Android Studio Build Variants you are pointing to devDebug.

Read the complete instructions here https://developer.android.com/studio/build/multidex.html#dev-build

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
  • where you put yourClass extends Application ? because I don't know where put –  Apr 26 '16 at 07:48
  • Create a class preferably in your root package (ex. `com.something.something.MyApplication`). Let `MyApplication` class extend `Application` (ex. `public class MyApplication extends Application { ... }` . Declare This class in your AndroidManifest in the `application` tag. (ex. ` ..... ` – Mark Pazon Apr 26 '16 at 08:10
  • I can't see, sorry, can you post any tutorial ? now I run without this class that extends from Aplication (and seems works), and I don't know if put into root - /java/here or java/package/here. is best any guide thanks. –  Apr 26 '16 at 08:25
  • @delive A quick `Google` bring up a lot of articles about this. I randomly picked one: http://www.intertech.com/Blog/androids-application-class/ – Mark Pazon Apr 26 '16 at 11:44
25

Supplying as an answer because this is better fit with the formatting.

To simply answer your question: No, there is no way. Multidex is a process meant to help lift the burden of the 65k method limit. This process is complicated and will simply make your build times longer.

The best you can can do is lower your method count.

In your build.gradle (supplied here) you're using:

`compile 'com.google.android.gms:play-services:8.3.0'`

But if you look at the most recent play services api you can pick and choose what services you actually need.

Look at Table 1 on this page.

Only use the ones you need. Google play services as a whole is somewhere around 30k methods.

That should help.

Community
  • 1
  • 1
Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
  • 3
    Thanks, you save me from dying of boring. Only adding wallet, and login services keeps it way under 30 k methods. – vktr Aug 22 '15 at 12:17
  • As mentioned by Mark Pazon in another answer, the solution is explained here http://developer.android.com/tools/building/multidex.html under "Optimizing Multidex Development Builds" – agirardello Nov 22 '15 at 23:04
2

Multidexing uses more memory. As you get closer to your max heap size in Java you'll find Java spends more time doing GC than it does doing any real work, this can slow things down a lot.

I'd strongly recommend increasing the max heap size when using multidex. Add the following to the android closure in your build.gradle file to make the max heap size 4GB (Make it larger/smaller if you wish):

dexOptions {
    javaMaxHeapSize "4g"
}
Will Calderwood
  • 4,393
  • 3
  • 39
  • 64
1

It depends.

You haven't specified it in your question, but if you just want to speed-up your development builds - then you can avoid the extra work. Official documentation includes a whole section about that.

Alex Lipov
  • 13,503
  • 5
  • 64
  • 87