我有一个多项目(大约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 Studio 3.0包含新的DEX编译器D8。
“dex编译器主要工作在日常应用开发的底层,但它直接影响应用的构建时间、.dex文件大小和运行时性能。”
“当将新的D8编译器与当前的DX编译器进行比较时,D8编译速度更快,输出的。dex文件更小,同时具有相同或更好的应用程序运行时性能。”
D8是可选的-使用它,我们必须把它放在项目的gradle.properties
android.enableD8=true
更多信息:https://android-developers.googleblog.com/2017/08/next-generation-dex-compiler-now-in.html
PS.它使我的构建时间提高了大约30%。
这个设置对我来说太快了
(大约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
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 \构建缓存
公认的答案是旧版本的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