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

其他回答

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

在我们的具体案例中,问题是由于使用了retrolambda插件,这迫使所有项目和子项目在每次尝试启动应用程序时都要重新编译,即使核心模块中没有任何代码被修改。

切除retrolamba为我们修复了它。希望它能帮助到别人。

公认的答案是旧版本的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

在低配置机器上运行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.

我最近买了一个新的固态硬盘,从Windows换成了Linux。我的构建时间现在快了一个数量级,不再烦人了。

虽然它没有直接回答为什么它比eclipse慢的问题,但它表明该进程受磁盘限制,升级到SSD可能是一个(有点昂贵的)解决方案。我猜会有人在谷歌上搜索这个问题,最后来到这里,他们可能会欣赏我的经验。