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

当前回答

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

步骤1:

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

步骤2:

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

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

劣势:

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

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

其他回答

如果使用谷歌播放服务,只依赖于你需要的库而不是整个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
        }
    }
    ...
}

根据android文档,将此添加到app模块的gradle文件中。

android {
    ...
    dexOptions {
    preDexLibraries true
    maxProcessCount 8
    }
}

只需创建一个名为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/

公认的答案是旧版本的android studio,其中大多数现在仍然有效。更新android studio让它更快了一点。不要费心指定堆大小,因为它会随着Xms和Xmx的增加而自动增加。下面是对VMoptions的一些修改

In bin folder there's a studio.vmoptions file to set the environment configuration. In my case this is studio64.vmoptions Add the following lines if they're not added already and save the file. In my case I've 8GB RAM. -Xms4096m -Xmx4096m -XX:MaxPermSize=2048m -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=utf-8` Start android studio. Go to File-> Settings-> Build, Execution, Deployment-> Compiler Check compile independent modules in parallel In command-line Options write: --offline Check Make project automatically Check configure on demand

在使用mac的情况下,起初我找不到vmoptions。不管怎样,这里有一篇关于如何在MAC OSX中更改vmoptions的好文章。引用这篇文章。

打开你的终端,输入下面的命令打开MAC OSX中的vmoptions:

open -e /Applications/Android\ Studio.app/Contents/bin/studio.vmoptions

这个设置对我来说太快了 (大约2秒构建)

build.gradle

android {

    dexOptions {
        incremental true
        preDexLibraries = false
        jumboMode = false
        maxProcessCount 4
        javaMaxHeapSize "6g"
    }
}

gradle.properties

org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx8192M

我的电脑:

CPU Intel(R) Pentium(R) CPU G2030 @ 3.00GHz, 3000 Mhz, 2个处理器 原理,2处理器lógicos x64 微软Windows 7专业版 (ram) 16,0 gb

项目文件 -都位于当地的HD