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

当前回答

到处寻找,终于找到了一个适合我们的解决方案。启用并行构建(在OSX:首选项->编译器-> gradle ->“并行编译独立模块”)和启用“自动制作项目”将它从~1分钟降低到~20秒。感谢/u/Covalence。

http://www.reddit.com/r/androiddev/comments/1k3nb3/gradle_and_android_studio_way_slower_to_build/

其他回答

Android Studio 2.3后更新

所有的答案都很棒,我鼓励使用这些方法来提高构建速度。

在2016年9月android 2.2发布后,android发布了实验性的构建缓存功能,以加速gradle构建性能,现在正式从android Studio 2.3 Canary发布。(正式发布说明)

它引入了一个新的构建缓存功能,默认情况下是启用的,可以通过存储和重用在相同或不同Android项目的以前构建中创建的文件/目录来加快构建时间(包括完整构建,增量构建和即时运行)。

使用方法:

在gradle中添加以下线条。属性文件

android.enableBuildCache = true
# Set to true or false to enable or disable the build cache. If this parameter is not set, the build cache is enable by default.

清理缓存:

有一个新的Gradle任务叫做cleanBuildCache,你可以更容易地清理构建缓存。您可以在终端中输入以下命令来使用它: 。/ gradlew cleanBuildCache 或者你可以通过删除所有存储在该位置的文件来清除Android studio 2.2的缓存 C:\Users\ <用户名> \ .android \构建缓存

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

步骤1:

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

步骤2:

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

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

劣势:

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

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

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

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

您可以尝试打开studio右侧的gradle菜单,只组装已更改的模块,然后运行install命令。当你按下运行键时,不管你对它正在组装的代码做了什么改变,它都会组装所有东西

请按照以下步骤操作。

开启脱机模式:请检查打印屏幕下方。 https://i.stack.imgur.com/RF9uG.png 启用立即运行:请检查下面的打印屏幕。 https://i.stack.imgur.com/mvHKJ.png 如果你想了解更多关于即时运行,请访问android开发者网站。