我有一个多项目(大约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
    }
}

当前回答

在低配置机器上运行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.

其他回答

一个微不足道的更改(到一个资源xml)仍然需要10分钟。 正如@rivare在他的回答中所说,命令行构建要快得多(缩短到15秒)。 下面是一些步骤,至少可以从Windows命令行快速完成一个简单的构建。

进入你的项目根目录(gradlew.bat所在的地方): android cd c: \ \ MaskActivity 执行构建: gradlew assembleDebug 直接从手机上卸载apk(拖拽即可卸载)。 构建完成后,使用Windows杀死BIG java进程 任务管理器。

或者如果你的Windows机器上有unix工具:

ps

“pid”的值如下所示:

kill -9 <pid>

现在安装apk: adb -d install C:\Android\MaskActivity\app\build\outputs\apk\app-debug.apk

硬件

我很抱歉,但是升级开发站到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将有助于发现瓶颈。

加速Gradle构建在Android Studio 3.2.1

你是否曾感觉在Android Studio中需要等待几分钟才能完成构建?我也是。这很烦人。 幸运的是,您可以使用一些方法来改善这一点。Android使用Gradle进行构建。最新版本4.6比以前的版本有了巨大的性能提升(详情请参阅发布说明)。

步骤1:更新Gradle版本 一个更简单的方法是:打开模块设置(你的项目)>项目结构

更新

更改到Gradle版本:4.6 而且 Android插件版本:3.2.1

从https://services.gradle.org/distributions/gradle-4.6-all.zip下载Gradle Release分发版 然后复制到Gradle文件夹:

最后一步是在设置> Gradle中添加您的发行版

不要忘记单击Apply以保存更改。

第二步:为项目启用离线模式、Gradle守护进程和并行构建 离线模式告诉Gradle忽略update-to-date检查。Gradle每次都要求依赖项,有了这个选项,它就只使用机器上已经存在的依赖项。 从android studio settings转到Gradle,点击离线工作框。

从android studio Setting进入Compiler,在命令行框中添加“- offline”,然后单击“并行编译独立模块”。

下一步是为您的项目启用Gradle守护进程和并行构建。并行构建将使您的项目具有多个模块(Gradle中的多项目构建)并行构建,这将使大型或模块化项目更快地构建。

这些设置可以通过修改一个名为gradle的文件来启用。属性在Gradle脚本目录(即、~ / .gradle / gradle.properties)。其中一些选项(例如并行编译模块)可以从Android Studio中获得,默认情况下也可以在那里启用,但将它们放在gradle中。属性文件将在从终端构建时启用它们,并确保您的同事将使用相同的设置。但如果你在一个团队中工作,有时你不能承诺这些事情。

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit org.gradle.parallel=true
# When set to true the Gradle daemon is used to run the build. For local developer builds this is our favorite property.
# The developer environment is optimized for speed and feedback so we nearly always run Gradle jobs with the daemon.
 org.gradle.daemon=true

Using the daemon will make your builds startup faster as it won’t have to start up the entire Gradle application every time. The Gradle Daemon is not enabled by default, but it’s recommend always enabling it for developers’ machines (but leaving it disabled for continuous integration servers). FAQ about this mode could be found here https://docs.gradle.org/current/userguide/gradle_daemon.html. The parallel builds setting could be unsafe for some projects. The requirement is that all your modules must be decoupled or your build could fail (see http://gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects for details).

步骤3:启用增量设计和调整内存设置 您可以通过启用增量索引来加快构建的速度。在模块的构建文件中:

添加这个选项到你的android块:

dexOptions {
    incremental true
}

在dexOptions块中,你还可以为dex进程指定堆大小,例如:

dexOptions {
    incremental true
    javaMaxHeapSize "12g"
}

这里的“12g”是12GB的内存。更多相关信息可以在这里找到google.github.io/android-gradle-dsl/current/ 你也可以在设置文件中配置Gradle参数,例如,如果你有一个大项目,增加最大堆大小:

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

查看所有参数列表:https://docs.gradle.org/current/userguide/userguide_single.html#sec:gradle_configuration_properties详细信息。

Step 4: Disable Antivirus Consider to exclude project and cache files from antivirus scanning. This is obviously a trade off with security. 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 Android Studio 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. I hope this helps. Leave a comment if you have any question or some other tips for improving the build performance.

有用的链接

以下是帮助这个刚开始的Android程序员(几年前曾是专业程序员)加速Android Studio 2.2的方法。我知道这是在重复,但是,只是在一个地方总结一下。

初始构建仍然非常缓慢,但现在重新启动运行中的应用程序通常是可以忍受的。我使用的是一台次优PC: AMD四核A8-7410 CPU, 8MB RAM,非ssd HD, Win 10。(这是我第一次在....上发布Stack Overflow;)

在设置-> gradle:

“离线工作”是(这可能是最重要的设置)。

在设置->编译器:

是“并行编译独立模块”(不确定这是否真的有助于利用多核cpu)。

在GRADLE脚本中,“构建。gradle(模块:app)”:

defaultConfig {
    ...
   // keep min high so that restarted apps can be hotswapped...obviously, this is hugely faster.
   minSdkVersion 14
   ...
    // enabling multidex support...does make big difference for me.
    multiDexEnabled true

也在GRADLE脚本,“GRADLE。物业(项目物业)”:

org . gradle。jvmargs=-Xmx3048m -XX: maxpermze =512m -XX:+ heapdumpfdfile -Dfile.encoding=UTF-8

org.gradle.parallel = true org.gradle.daemon = true

此外,在物理设备上而不是模拟器上进行测试对我来说效果很好;可以站立的小平板很方便。

只需创建一个名为gradle的文件。属性:

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

将这一行添加到文件中:

org.gradle.daemon=true

对我来说,现在的速度等于Eclipse。

来源:https://www.timroes.de/2013/09/12/speed-up-gradle/