【问题标题】:Android App is taking too long to startAndroid 应用程序启动时间过长
【发布时间】:2016-07-21 07:47:39
【问题描述】:

我为这个问题搜索了很多,但我能找到的唯一答案是重构 onCreate 中的代码以实现快速启动。但在我的情况下,问题看起来不同。 我和我的团队成员正在处理通过 GITHUB 同步的相同代码,他制作的构建会立即打开,而我制作的构建需要 10 多秒才能启动。 另一个问题,他的构建是 90MB 左右,而我的构建只有 30MB。无论如何,应用程序功能齐全,但启动时间和构建大小的差异太大。

我不确定他的系统配置,但我使用 JAVA8 进行编译,使用 SDK 24 和 Gradle 2.10 构建应用程序。

这是我的 gradle 文件

buildscript {
    repositories {
        jcenter()
        /*flatDir {
            dirs 'libs'
        }*/
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        //compile(name:'library-release', ext:'aar')
        classpath 'io.realm:realm-gradle-plugin:0.88.0'
        classpath 'io.fabric.tools:gradle:1.21.2'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}


android {
    compileSdkVersion 24
    buildToolsVersion '24.0.0'

    useLibrary 'org.apache.http.legacy'
    dexOptions {
        maxProcessCount 4 // this is the default value
        javaMaxHeapSize "4g"
        dexInProcess = false
    }

    defaultConfig {
        applicationId "com.sample.app"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 31
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        /*debug {
            minifyEnabled true
            useProguard false
        }*/
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            java.srcDirs = ['src/main/java']
        }
        robolectric {
            java.srcDir file('src/test/java/')
        }
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile('com.twitter.sdk.android:twitter:1.9.0@aar') {
        transitive = true;
    }
    testCompile 'org.robolectric:shadows-play-services:3.0'
    testCompile 'org.robolectric:shadows-support-v4:3.0'
    testCompile 'org.powermock:powermock-module-junit4:1.6.2'
    testCompile 'org.powermock:powermock-module-junit4-rule:1.6.2'
    testCompile 'org.powermock:powermock-api-mockito:1.6.2'
    testCompile 'org.powermock:powermock-classloading-xstream:1.6.2'
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'org.assertj:assertj-core:1.7.0'
    androidTestCompile 'com.android.support:support-annotations:24.0.0'
    androidTestCompile 'com.android.support.test:runner:0.4'
    androidTestCompile 'com.android.support.test:rules:0.4'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.7@aar') {
        transitive = true;
    }

    //    compile(group: 'com.google.gms', name: 'google-services', version: '2.0.0-beta4', ext: 'pom')
    //apply plugin: 'com.android.application'
    compile 'com.google.android.gms:play-services:9.2.1'
    compile 'com.google.android.gms:play-services-location:9.2.1'
    compile 'com.google.android.gms:play-services-gcm:9.2.1'
    compile 'com.mcxiaoke.volley:library:1.0.15'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.coinbase.android:coinbase-android-sdk:1.0.1'
    compile 'org.roboguice:roboguice:2.0'
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.14'
    compile 'com.sprylab.android.texturevideoview:texturevideoview:1.1.1'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.google.android.gms:play-services-ads:9.2.1'
    compile 'com.google.android.gms:play-services-auth:9.2.1'
}
//put 'com.google.gms.google-services' plugin at the bottom
apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • APK 的大小应该没有那么不同!了解他使用的 IDE/版本以及你们是否使用相同的资源等可能很有用。30 与 90MB 的大小看起来很奇怪。
  • 是的,我知道@Sufian,我们已经匹配了资源,它们几乎相同。我们都在使用 Android Studio 2.1.2
  • 也可以看到这个帖子stackoverflow.com/questions/22064591/…

标签: android android-studio android-gradle-plugin


【解决方案1】:

如果仅在调试模式下发生,您可以尝试禁用即时运行

首选项>构建、执行、部署>即时运行

然后取消选中“启用即时运行...”

【讨论】:

  • 我不排除即时运行会导致这样的怪异。真是一团糟。
  • 成功了,应用程序运行速度更快,大小从 30MB 变为 120MB。效果很好,但如果您能提供任何有关此行为的链接或解释,将会更有帮助。
  • @MohammedAtif 这可能会回答您的问题 - stackoverflow.com/questions/34925708/…
  • @Sufian,有点用处。但是更结构化的细节,特别是来自任何谷歌官方文档的细节会更有帮助。也许我可以使用一些即时运行的自定义来防止此类错误。
  • @MohammedAtif Google 的文档很糟糕。就我个人而言,我相信(我希望很多人与我分享这一点)Instant Run 仍然存在问题,甚至不应该被认为是测试版。
猜你喜欢
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 2016-05-01
  • 1970-01-01
  • 2022-06-14
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多