【问题标题】:Why does my build takes so long is Android Studio?为什么我的构建需要这么长时间是 Android Studio?
【发布时间】:2015-10-08 22:11:53
【问题描述】:

我在 Android Studio 中的构建需要很长时间,超过 2 分钟,有时需要 3 分钟。我尝试了 StackOverflow 中解释的一些方法来加快构建时间,但它并没有解决我的问题。

我的 app 模块的 build.gradle 代码如下:

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

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

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

// Adding The GIT SHA to Crashlytics crash reporting
def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.moymer"
        minSdkVersion 11
        targetSdkVersion 22
        multiDexEnabled true
        versionCode 5
        versionName "2.0.0"

        buildConfigField "String", "GIT_SHA", "\"${gitSha}\""
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.github.nirhart:parallaxscroll:1.0'
    compile 'com.android.support:multidex:1.0.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.4.0@aar') {
        transitive = true;
    }
    compile 'in.srain.cube:grid-view-with-header-footer:1.0.12'
    compile 'com.android.support:recyclerview-v7:23+'
    compile 'com.squareup:otto:1.3.8'
    compile 'com.android.support:percent:23.0.0'
    compile 'com.jakewharton.timber:timber:3.1.0'
    compile project(':androidffmpeglibrary')
}

我的 gradle.properties 文件如下:

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true

【问题讨论】:

    标签: android android-studio gradle build


    【解决方案1】:

    你有 11 多个依赖项和一个额外的插件。至少有一个 (androidffmpeglibrary) 似乎涉及 NDK。这不会很快,尤其是取决于您的构建硬件。

    如果您想要更快的构建,请摆脱一些垃圾。比如有finer-grained Play Services SDK dependencies一年多;仅使用您需要的部分,而不是“除了厨房水槽之外的所有东西”play-services 工件。反过来,这可能允许您转储 multidex,因为您可能会回落到 64K DEX 方法引用限制以下。

    Android 工具团队正在努力提高构建速度,这将出现在更新版本的 Android Plugin for Gradle 中。那必须在您的顶级build.gradle 文件中,因为我看不到您在此处包含该文件的位置。确保您使用的是最新版本,并随着新版本的发布不断更新。

    【讨论】:

      【解决方案2】:

      正如 CommonsWare 所描述的,拥有大量依赖项确实会减慢整体构建速度。到目前为止,您可以开始使用具有instant run 功能的 Android Studio 2.0。

      即时运行大大缩短了构建时间,因为它只推动了不重要的小变化。无论您在代码中做什么更改,首先都会分为三类——热插拔、热插拔和冷插拔。负载更小,交换更温暖,构建时间更快。

      更改注释、静态字段等重大结构更改进入冷交换。清单文件中的更改也来自冷交换。您可以查看完整列表here

      好消息是大多数小的更改都属于热插拔,并且只要您按下运行按钮,这些更改就会迅速推送到正在运行的应用程序中。相关活动重新启动,您可以在几秒钟内看到您的更改。

      Android Studio 2.0稳定版发布,大家可以from this link下载。它具有即时运行功能。希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        相关资源
        最近更新 更多