在Android Studio 使用Gradle构建项目比较慢

1问题

Q:我有许多个项目(大约10个modules),每个项目都需要大约20s-30s的时间去构建。当我点击Run按钮的时候 ,每一次我都需要等待去重新构建app,十分慢。

在Android Studio中有没有自动构建的步骤?或者你有没有一些建议能让这个过程更快?

在Eclipse中,由于自动构建,所以说在模拟器上运行同样的项目只需要3-5秒。

下面是我的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
    }
}

2 回答

1 硬件

很抱歉,但如果你升级了SSD会大大提高速度

2 TOOLS的版本
提交构建的性能是开发团队最主要要考虑的,所以说rue报你在使用最新的 Gradle and Android Gradle Plugin.

3 配置你的文件

创建一个gradle.properties 的文件,在合适的地方

  • /home//.gradle/ (Linux)
  • /Users//.gradle/ (Mac)
  • C:\Users\.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

如果说你把上面的文件放在projectRoot\gradle.properties,那么Gradle 的特性就会在本地工作,如果说把他们放在user_home.gradle\gradle.properties 就是全局的属性。

上面的属性会在你使用控制台或者idea的时候应用

IDE设置

勾选上 “offline work” 会保证你取消真成的网络请求,在同步gradle文件的时候
这里写图片描述

Native multi-dex

一个慢的原因就是把java自结巴转换为单个的dex文件。使用native multidex (minSdk 21 for debug builds only) 会钱少大量的工作。

Dependencies

在依赖库的子项目上使用dependencies使用@arr前缀

搜寻arr的package在mavenCentral, jCenter or use jitpack.io。如果你不打算编辑依赖库的源码,你就应该这样做。

Profiling a build

Gradle为profiling projects支持built-support.不同的项目会使用不同插件和客户脚本,–profile会帮助你找到瓶颈

没有更多推荐了,返回首页