18

In my project, I have used libraries which require multidex support. Based on the research I did, I came to know that it causes delay during app start up.

I have enabled multidex feature in gradle. defaultConfig { multiDexEnabled true }

I used to get NoClassDefFound Exception for kitkat devices so I added the following:

I have added application name in manifest as android:name="android.support.multidex.MultiDexApplication"

and extended my application class with MultiDexApplication.

I know any one of the above is sufficient but I have just written both.

I tried different ways to optimize the duration of start up like the following :

1.added productFlavors { dev { minSdkVersion 21 } prod { minSdkVersion 14 } }

  1. dexOptions { preDexLibraries false javaMaxHeapSize "4g" }

  2. I imported only required google play service's libraries instead of importing full library.

Can you please suggest me an approach to improvise the duration because its actually taking more than 10 seconds and some times even more than that in some of the devices?

Or Is there any other reason that might be causing the delay apart from multidex?

Thanks in advance.

I have added my build.gradle file contents :

 apply plugin: 'com.android.application'
 apply plugin: 'com.google.gms.google-services'

 android {
  compileSdkVersion 23
  buildToolsVersion "23.0.3"
  useLibrary 'org.apache.http.legacy'

 defaultConfig {
    applicationId "com.example.app"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 3
    versionName "1.2"
    multiDexEnabled true
}

dexOptions {
    incremental true
    javaMaxHeapSize "10g"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

  dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/ormlite-android-4.28.jar')
    compile files('libs/ormlite-core-4.28.jar')
    compile project(':volley')
    compile 'com.android.support:cardview-v7:23.2.1'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
    compile 'com.viewpagerindicator:library:2.4.1@aar'
    compile 'com.android.support:design:23.2.1'
    compile 'com.facebook.android:facebook-android-sdk:4.1.2'
    compile 'org.jsoup:jsoup:1.8.2'
    compile 'org.twitter4j:twitter4j:4.0.3'
    compile 'com.github.castorflex.smoothprogressbar:library-circular:1.1.0'
    compile 'com.orhanobut:dialogplus:1.11@aar'
    compile 'com.google.android.gms:play-services-maps:8.3.0'
    compile 'co m.google.android.gms:play-services-gcm:8.3.0'
    compile 'com.google.android.gms:play-services-location:8.3.0'
    compile 'org.java-websocket:Java-WebSocket:1.3.0'
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
 }
shakunthalaMK
  • 386
  • 1
  • 4
  • 20
  • post your `build.gradle` – IntelliJ Amiya May 18 '16 at 10:13
  • 1
    Can I know the reason for the downvote? So that I will not do the mistake again. Thanks. – shakunthalaMK May 18 '16 at 10:28
  • 1
    @IntelliJAmiya When I try to set buildToolsVersion 23.2.1, it says its not available for download. – shakunthalaMK May 18 '16 at 10:38
  • Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. In here you have lots of `dependencies` thats why slow. nothing else – IntelliJ Amiya May 18 '16 at 10:47
  • 1
    @IntelliJ Amiya - I changed buildToolsVersion to 23.0.3. Still it is taking the same amount of time. Yes, I understood about that. But is there any other feasible way to optimize it because it is really odd that it takes so much time to start and user might not find it user friendly. – shakunthalaMK May 18 '16 at 11:01

2 Answers2

45

There is a strange issue with newly released Android Studio 2.0 or 2.1. First time of launching application take longer than usual. This issue happens only in debug mode and not effect to your released APK. Also I was suffering this issue and found this solution.

Settings/Preferences(Mac) → Build, Execution, Deployment → Instant Run and uncheck Instant Run

enter image description here

This will solve your problem and if it is please mark as accepted answer.

Community
  • 1
  • 1
Anant Shah
  • 3,744
  • 1
  • 35
  • 48
  • Great solution -- i was wondering why even with a tiny single activity APP at FIRST RUN took loads of seconds to Initialize when you run the APP through AS – Tasos Aug 21 '16 at 03:07
  • 1
    Wow, thanks for the heads up, I really didn't know why my app was taking so much time just to get to the application's onCreate method after install (over 10s!!)... But when I tried with a release version, it took only 148 ms – Raphael Royer-Rivard Oct 21 '16 at 00:21
  • Which is better, to wait 1 time for seconds, or every time wait for gradle build ? Disabling Instant Run is a shitty solution. – Arsen Sench Nov 30 '16 at 07:37
  • @ArsenSench that is depends on us what to choose and use there are options available for both kind of developers those who disabled it and want to enabled it... Also for some developers its "shitty solution" to enable instant run and wait for 10-15 seconds for first time... – Anant Shah Nov 30 '16 at 10:44
  • "Instant Run" is not available on the Windows version. (Not sure if still available at all) – 101is5 Dec 07 '21 at 11:26
  • @101is5 you can try searching in your android studio settings, Google team cannot be biased with mac and windows user, All features are there in windows too, the same as mac. – Anant Shah Dec 08 '21 at 18:01
  • The version I have is Win's, so this is the one I know of. But ok, assuming Win and Mac versions are exactly the same, the option "Instant Run" isn't available anymore. @AnantShah – 101is5 Dec 08 '21 at 18:07
2

There is an issue in version 2.0 and 2.1 of android studio, but the answer is simple. You must go to Preferences/Build, Execution, Deployment/Instant Run.

Then when you click on "Instant Run" in the right side will appear 4 options with a checkbox for echa one, you must uncheck the first and the last one. Finally click Apply,Ok. And that's all.

That will solve the problem

Leslie Morejon
  • 117
  • 1
  • 7
  • 3
    Instant run is the biggest feature of Android Studio 2. It takes more time the first time but then it takes a lot less time to deploy changes than with normal builds. – headsvk Aug 10 '16 at 17:11