我有一个多项目(大约10个模块),每次建造大约需要20-30秒。当我在Android Studio中按下Run键时,我每次都要等待重新构建应用程序,这非常缓慢。

是否有可能在Android Studio中自动化构建过程?或者你有什么建议可以加快这个过程吗?

在Eclipse中,由于自动构建,在模拟器上运行相同的项目大约需要3-5秒。

这是我的身材。Gradle文件(app模块):

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':libraries:SharedLibs')
    compile project(':libraries:actionbarsherlock')
    compile project(':libraries:FacebookSDK')
    compile project(':libraries:GooglePlayServices')
    compile project(':libraries:HorizontalGridView')
    compile project(':libraries:ImageViewTouch')
    compile project(':libraries:SlidingMenu')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 16
    }
}

当前回答

我最近买了一个新的固态硬盘,从Windows换成了Linux。我的构建时间现在快了一个数量级,不再烦人了。

虽然它没有直接回答为什么它比eclipse慢的问题,但它表明该进程受磁盘限制,升级到SSD可能是一个(有点昂贵的)解决方案。我猜会有人在谷歌上搜索这个问题,最后来到这里,他们可能会欣赏我的经验。

其他回答

硬件

我很抱歉,但是升级开发站到SSD和大量内存可能比下面的点加起来影响更大。

工具版本

提高构建性能是开发团队的首要任务,所以请确保您使用的是最新的Gradle和Android Gradle Plugin。

配置文件

创建一个名为gradle的文件。应用于任何目录的属性:

-用户home / < >。gradle - (Linux) - \用户/用户< >。gradle - (Mac) C: \ \用户用户< > \。gradle (Windows)

附加:

# IDE (e.g. Android Studio) users:
# Settings specified in this file will override any Gradle settings
# configured through the IDE.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# The Gradle daemon aims to improve the startup and execution time of Gradle.
# When set to true the Gradle daemon is to run the build.
# TODO: disable daemon on CI, since builds should be clean and reliable on servers
org.gradle.daemon=true

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# https://medium.com/google-developers/faster-android-studio-builds-with-dex-in-process-5988ed8aa37e#.krd1mm27v
org.gradle.jvmargs=-Xmx5120m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true

# Enables new incubating mode that makes Gradle selective when configuring projects. 
# Only relevant projects are configured which results in faster builds for large multi-projects.
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
org.gradle.configureondemand=true

# Set to true or false to enable or disable the build cache. 
# If this parameter is not set, the build cache is disabled by default.
# http://tools.android.com/tech-docs/build-cache
android.enableBuildCache=true

如果你把Gradle属性放在projectRoot\ Gradle,它们就可以在本地工作。属性和全局,如果你把它们放在user_home\.gradle\gradle.properties。属性适用于从控制台或直接从想法运行gradle任务:

IDE设置

可以从IDE设置GUI中调整Gradle-IntelliJ集成。启用“离线工作”(从yava下面检查答案)将禁用每个“同步gradle文件”上的真实网络请求。

本机multi-dex

apk构建中最慢的步骤之一是将java字节码转换为单个dex文件。启用本地multidex(仅用于调试版本的minSdk 21)将有助于减少工具的工作量(请看下面Aksel Willgert的答案)。

依赖关系

优先选择@aar依赖而不是库子项目。

在mavenCentral, jCenter或使用jitpack搜索aar包。IO从github构建任何库。如果你没有编辑依赖库的源代码,你不应该每次都用你的项目源代码来构建它。

杀毒

考虑从防病毒扫描中排除项目和缓存文件。这显然是一个与安全的权衡(不要在家里尝试!)。但是如果你在分支之间切换很多,那么反病毒会在允许gradle进程使用它之前重新扫描文件,这会减慢构建时间(特别是AndroidStudio同步项目与gradle文件和索引任务)。测量构建时间和进程CPU是否启用防病毒,以查看两者是否相关。

分析构建

Gradle内置了对项目分析的支持。不同的项目使用不同的插件和自定义脚本组合。使用——profile将有助于发现瓶颈。

我最近买了一个新的固态硬盘,从Windows换成了Linux。我的构建时间现在快了一个数量级,不再烦人了。

虽然它没有直接回答为什么它比eclipse慢的问题,但它表明该进程受磁盘限制,升级到SSD可能是一个(有点昂贵的)解决方案。我猜会有人在谷歌上搜索这个问题,最后来到这里,他们可能会欣赏我的经验。

如果使用谷歌播放服务,只依赖于你需要的库而不是整个blob可以使事情更快。

如果你只需要地图,使用:

compile 'com.google.android.gms:play-services-maps:6.5.+'

而不是:

compile 'com.google.android.gms:play-services:6.5.+'

后者将20k个方法(参见博客)引入类路径,这可能使总方法计数超过64k。

这将强制使用proguard或multidex,即使是调试版本。对于我的一个项目,我有以下的构建时间

Multidex构建(带支持库)~40秒 保护建造~20秒 当方法限制< 64k ~5sec时构建

如果在sdk 21+上开发,就有可能优化multidex构建,如android文档中所述

android {
    productFlavors {
        // Define separate dev and prod product flavors.
        dev {
            // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
            // to pre-dex each module and produce an APK that can be tested on
            // Android Lollipop without time consuming dex merging processes.
            minSdkVersion 21
        }
        prod {
            // The actual minSdkVersion for the application.
            minSdkVersion 14
        }
    }
    ...
}

更改此设置后,我的编译持续时间10分钟更改为~10秒。

步骤1:

设置(ctrl + Alt + S) - > 构建、执行部署- > 编译器- > 在命令行选项框中输入“——offline”。

步骤2:

选中“并行编译独立模块”复选框。 &点击应用->确定

参考资料- https://www.sundoginteractive.com/blog/speed-up-gradle-in-android-studio

劣势:

您将无法下拉构建中确定的依赖项的最新版本。gradle文件。它运行得更快,因为它使用了这些导入库的缓存快照。

重要提示:部署应用程序时,请删除此设置并使用最新版本的依赖项构建。

在低配置机器上运行Android环境。

关闭浏览器中多余的网页标签 对于防病毒用户,请排除自动生成的build文件夹 Android工作室有1.2 Gb的默认堆可以减少到512 MB 帮助>编辑自定义虚拟机选项 studio.vmoptions -Xmx512m 布局性能将加快 对于Gradle的核心组件之一,在Android工作室Mkae肯定 就像现在3.0beta是最新的版本

以下提示可能会影响代码质量,因此请谨慎使用:

Studio contain Power safe Mode when turned on it will close background operations that lint , code complelitions and so on. You can run manually lintcheck when needed ./gradlew lint Most of are using Android emulators on average it consume 2 GB RAM so if possible use actual Android device these will reduce your resource load on your computer. Alternatively you can reduce the RAM of the emulator and it will automatically reduce the virtual memory consumption on your computer. you can find this in virtual device configuration and advance setting. Gradle offline mode is a feature for bandwidth limited users to disable the downloading of build dependencies. It will reduce the background operation that will help to increase the performance of Android studio. Android studio offers an optimization to compile multiple modules in parallel. On low RAM machines this feature will likely have a negative impact on the performance. You can disable it in the compiler settings dialog.