问题
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 } }
dexOptions { preDexLibraries false javaMaxHeapSize "4g" }
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'
}
回答1:
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
This will solve your problem and if it is please mark as accepted answer.
回答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
来源:https://stackoverflow.com/questions/37296544/android-takes-more-time-on-application-start-up-during-first-time-launch