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

是否有可能在Android Studio中自动执行构建过程?或者你有什么建议可以让这个过程更快些吗?

在Eclipse中,多亏了自动构建,在仿真器上运行相同的项目大约需要3-5秒.

这是我的身材.gradle文件(应用程序模块):

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

推荐答案

硬件

我很抱歉,但是将开发站升级到SSD和数以吨计的RAM的影响可能比下面的分数加起来还要大.

工具版本

提高构建性能是开发团队的首要任务,所以确保使用最新的GradleAndroid Gradle Plugin.

配置文件

在任何适用的目录中创建一个名为gradle.properties的文件:

  • /home/<username>/.gradle/(Linux)
  • /Users/<username>/.gradle/(Mac)
  • C:\Users\<username>\.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.properties,则Gradle属性在本地有效;如果您将它们放置在user_home\.gradle\gradle.properties,则Gradle属性全局有效.从Console或直接从IDEA运行Gradle任务时应用的属性:

IDE设置

可以从IDE设置GUI调整Gradle-IntelliJ集成.启用"离线工作"(判断下面yava的答案)将禁用每个"sync Gradle文件"上的真实网络请求.

IDE设置

本机多索引

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

依赖项

与库子项目相比,更喜欢@aar个依赖项.

mavenCentraljCenter上搜索aar包,或者使用jitpack.io从github构建任何库.如果您没有编辑依赖项库的源,那么不应该每次都使用项目源来构建依赖项库.

杀毒软件

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

分析生成

Gradle内置了对profiling projects的支持.不同的项目使用不同的插件和定制脚本组合.使用--profile将有助于找到瓶颈.

Android相关问答推荐

我遇到了一个HashMaps对象没有存储在Firebase数据库中的问题.HashMap的一个对象put方法未被存储

Android添加设置图标齿轮到应用程序信息

在Android应用程序上使用Room数据库时,是否可以预先填充数据

约束布局:垂直链中的视图应将内容包裹到空间的1/3

如何在Android Jetpack Compose中找到我的手机屏幕一行有多少个单词

MAP函数返回单位列表而不是字符列表

SmsMessage如何在Kotlin库中工作?

如何在 Jetpack Compose LazyColumn 中将项目分组在一起,例如卡片

SDK 33 的问题 - 面向 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

retrofit2.HttpException: HTTP 401

Jetpack Compose 中的用户在线指示器

如何在ExecutorService中设置progressBar的进度?不想使用 AsyncTask,因为它已被弃用

需要在按钮 onclick 上从 string.xml 获取值. @Composable 调用只能在@Composable 函数的上下文中发生

setContentView() 方法的签名

在 jetpack compose 中交替使用 View.INVISIBLE

在 Jetpack Compose 中更改列中子项的对齐方式

如何在 Android Studio 中创建新的可组合函数?

对话框中的内容不可见

Jetpack 使用 Canvas 组成半圆

如何在 flow.stateIn() 之后从流中的另一个函数发出emits ?