通过 Gradle 和 Android Studio 构建和运行应用程序比通过 Eclipse 慢 [英] Building and running app via Gradle and Android Studio is slower than via Eclipse

查看:34
本文介绍了通过 Gradle 和 Android Studio 构建和运行应用程序比通过 Eclipse 慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

是否可以在 Android Studio 中自动化构建过程?或者您对如何加快此过程有什么建议?

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

这是我的 build.gradle 文件(应用模块):

buildscript {存储库{行家 { url 'http://repo1.maven.org/maven2' }}依赖{类路径 'com.android.tools.build:gradle:0.4'}}应用插件:'android'依赖{编译文件树(目录:'libs',包括:'*.jar')编译项目(':libraries:SharedLibs')编译项目(':libraries:actionbarsherlock')编译项目(':libraries:FacebookSDK')编译项目(':libraries:GooglePlayServices')编译项目(':libraries:Horizo​​ntalGridView')编译项目(':libraries:ImageViewTouch')编译项目(':libraries:SlidingMenu')}安卓 {compileSdkVersion 17构建工具版本17.0.0"默认配置{minSdk 版本 8目标SDK版本16}}

解决方案

硬件

很抱歉,但将开发站升级到 SSD 和大量内存可能比以下几点加起来的影响更大.

工具版本

提高构建性能是开发团队的首要任务,因此请确保您使用的是最新的 下面).

依赖项

优先于 @aar 依赖项而不是库子项目.

mavenCentral 上搜索 aar 包,jCenter 或使用 jitpack.io 从 github 构建任何库.如果您不是在编辑依赖库的源代码,则不应每次都使用您的项目源代码构建它.

杀毒软件

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

分析构建

Gradle 内置支持分析项目.不同的项目使用不同的插件和自定义脚本组合.使用 --profile 将有助于找到瓶颈.

I have a multi-project (~10 modules) of which building takes about 20-30 seconds each time. When I press Run in Android Studio, I have to wait every time to rebuild the app, which is extremely slow.

Is it possible to automate building process in Android Studio? Or do you have any advice on how to make this process faster?

In Eclipse, thanks to automatic building, running the same project on an emulator takes about 3-5 seconds.

This is my build.gradle file (app module):

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

解决方案

Hardware

I'm sorry, but upgrading development station to SSD and tons of ram has probably a bigger influence than points below combined.

Tools versions

Increasing build performance has major priority for the development teams, so make sure you are using latest Gradle and Android Gradle Plugin.

Configuration File

Create a file named gradle.properties in whatever directory applies:

  • /home/<username>/.gradle/ (Linux)
  • /Users/<username>/.gradle/ (Mac)
  • C:\Users\<username>\.gradle (Windows)

Append:

# 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 properties works local if you place them at projectRoot\gradle.properties and globally if you place them at user_home\.gradle\gradle.properties. Properties applied if you run gradle tasks from console or directly from idea:

IDE Settings

It is possible to tweak Gradle-IntelliJ integration from the IDE settings GUI. Enabling "offline work" (check answer from yava below) will disable real network requests on every "sync gradle file".

Native multi-dex

One of the slowest steps of the apk build is converting java bytecode into single dex file. Enabling native multidex (minSdk 21 for debug builds only) will help the tooling to reduce an amount of work (check answer from Aksel Willgert below).

Dependencies

Prefer @aar dependencies over library sub-projects.

Search aar package on mavenCentral, jCenter or use jitpack.io to build any library from github. If you are not editing sources of the dependency library you should not build it every time with your project sources.

Antivirus

Consider to exclude project and cache files from antivirus scanning. This is obviously a trade off with security (don't try this at home!). But if you switch between branches a lot, then antivirus will rescan files before allowing gradle process to use it, which slows build time (in particular AndroidStudio sync project with gradle files and indexing tasks). Measure build time and process CPU with and without antivirus enabled to see if it is related.

Profiling a build

Gradle has built-in support for profiling projects. Different projects are using a different combination of plugins and custom scripts. Using --profile will help to find bottlenecks.

这篇关于通过 Gradle 和 Android Studio 构建和运行应用程序比通过 Eclipse 慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆