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

Android takes more time on application start up during first time launch

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

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

我曾经为 kitkat 设备获取 NoClassDefFound 异常,所以我添加了以下内容:

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

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

我知道上面的任何一个就足够了,但我刚刚写了两个。

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

1.added 产品口味 { 开发{ minSdkVersion 21 } 产品{ minSdkVersion 14 } }

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

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

您能否建议我一种临时调整持续时间的方法,因为它实际上需要超过 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 存在一个奇怪的问题。首次启动应用程序需要比平时更长的时间。此问题仅在调试模式下发生,不会影响您发布的 APK。我也遇到了这个问题并找到了这个解决方案。

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

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

androidstudio 2.0 和 2.1 版本存在问题,但答案很简单。你必须去 Preferences/Build, 执行, Deployment/Instant 运行.

然后当你点击右边的"Instant Run"时会出现4个选项和echa one的复选框,你必须取消选中第一个和最后一个。 最后点击Apply,Ok。 仅此而已。

那就解决问题了