Android 在首次启动期间需要更多时间来启动应用程序

标签 android android-studio android-5.0-lollipop android-multidex

在我的项目中,我使用了需要 multidex 支持的库。根据我所做的研究,我知道它会导致应用程序启动延迟。

我在 gradle 中启用了 multidex 功能。 defaultConfig { multiDexEnabled true }

我曾经为 kitkat 设备获得 NoClassDefFound Exception,所以我添加了以下内容:

我在 list 中添加了应用程序名称 android:name="android.support.multidex.MultiDexApplication"

并使用 MultiDexApplication 扩展了我的应用程序类。

我知道上面的任何一个都足够了,但我只是写了两个。

我尝试了不同的方法来优化启动持续时间,如下所示:

1.添加产品 flavor { 开发{ minSdkVersion 21 } 产品{ minSdkVersion 14 } }

  1. dexOptions { preDexLibraries 错误 javaMaxHeapSize "4g" }

  2. 我只导入了所需的 google play 服务库,而不是导入完整的库。

您能否建议我一种即兴创作持续时间的方法,因为它实际上需要超过 10 秒,有时甚至超过某些设备的时间?

或者除了multidex之外还有其他原因可能导致延迟吗?

提前致谢。

我已经添加了我的 build.gradle 文件内容:

 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'
 }

最佳答案

新发布的 Android Studio 2.0 或 2.1 存在一个奇怪的问题。首次启动应用程序需要比平时更长的时间。此问题仅在 Debug模式下发生,不会影响您发布的 APK。我也遇到了这个问题并找到了这个解决方案。

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

enter image description here

这将解决您的问题,如果是,请标记为已接受的答案。

关于Android 在首次启动期间需要更多时间来启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37296544/

相关文章:

android - 设置LightTouchEnabled(假);已弃用 - 替代方案?

android - : ./gradlew lint 和分析 -> 检查代码之间的区别

android - ListView 设置自定义波纹选择器

android - Lollipop - ProgressDialog 颜色变化

Android BackupManager 全量数据备份没有同意

php - 如何从android发送多个数组作为json并在php中读取?

android - 出现 SWT 错误

android - 折线不遵循两个位置之间的实际路径

java - Android Studio 更新 Activity 位于 'Run'

android - 在 Spinner 上选择/滚动项目时应用程序崩溃。 (LG 移动) (java.lang.IllegalStateException : Iteration already started)