我有一个多项目(大约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程序员(几年前曾是专业程序员)加速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
此外,在物理设备上而不是模拟器上进行测试对我来说效果很好;可以站立的小平板很方便。
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 \构建缓存