LoginSignup
257
242

More than 5 years have passed since last update.

Android Studioのgradleビルドを高速化する

Last updated at Posted at 2014-12-03

EclipseからAndroid Studioに移行したんですが、ビルドが超遅くなりました。

見てみると、なんか毎回gradle syncしてたりJVMのメモリ足りない感じだったりするみたいなので、こちらのStackOverFlowの記事を参考に高速化してみました。

1. gradle.propertiesファイルを変更

プロジェクトルートにあるgradle.propertiesを編集します。なかったら作ってください。
ここには、gradleビルドするときの引数を設定できます。以下のように設定します。

gradle.properties
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Settings specified in this file will override any Gradle settings
# configured through the IDE.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# The Gradle daemon aims to improve the startup and execution time of Gradle.
# When set to true the Gradle daemon is to run the build.
# TODO: disable daemon on CI, since builds should be clean and reliable on servers
org.gradle.daemon=true

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true

# Enables new incubating mode that makes Gradle selective when configuring projects.
# Only relevant projects are configured which results in faster builds for large multi-projects.
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
org.gradle.configureondemand=true

以下簡単な説明。

  • org.gradle.daemonは、デーモンプロセスを使うかどうかのパラメータです。
  • org.gradle.jvmargsは、実行時のJVM引数です。メモリ足らなくなって遅くなるのを防ぎます。
  • org.gradle.parallelは、gradleの並列ビルドモードを有効にするパラメータです。複数プロジェクトのビルド時に効果を発揮するらしいです。
  • org.gradle.configureondemandに関しては詳しく理解できていないのですが、関連するプロジェクトがある場合に必要な部分だけビルドする設定みたいです。

これらのパラメータの情報は、こちらで説明されています。

この設定は、体感的にちょっと速くなったかな、くらいの変化でした。まぁ効果はありそうです。

2. gradleをオフラインモードに変更

これは超効果ありました。毎回gradleで最新バージョンをチェックしにいくのを省略してくれます。大規模なプロジェクトほど効果高いのではないでしょうか。

Android Studio > Preferences > Gradle の、Offline workにチェックをいれます。

gradle_faster1.png

OKを押してビルドすると、gradle syncせずにビルドを始めます。Android Studio0.4.0からサポートされた機能みたいです。

新しいdependencyを追加した時はネットワーク接続が必要になるので、オフラインモードのままだとエラーが出るので注意です。まぁ日頃開発している中で何度もdependencyを追加することはないでしょうから、基本はオフラインモードにしておくのがいいんじゃないかなと思います。

257
242
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
257
242