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

当前回答

先试试这个。这是我的个人经历。

我也有同样的问题。我所做的只是永久禁用反病毒(我的是Avast Security 2015)。就在禁用防病毒后,事情进展顺利。gradle成功完成了。 从现在开始,gradle在几秒钟内完成(只需要5-10秒)。

其他回答

我受够了本地机器上缓慢的android构建。我解决这个问题的方法是在AWS上启动一台高端机器,将我本地的代码重新同步到机器上,然后在那里编译。

我看到了性能的立即提升,并且我的本地系统不再占用CPU。看看我创建的这个工具,帮助开发人员加快他们的终端https://stormyapp.com

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

先试试这个。这是我的个人经历。

我也有同样的问题。我所做的只是永久禁用反病毒(我的是Avast Security 2015)。就在禁用防病毒后,事情进展顺利。gradle成功完成了。 从现在开始,gradle在几秒钟内完成(只需要5-10秒)。

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

关闭浏览器中多余的网页标签 对于防病毒用户,请排除自动生成的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.