マルチプロジェクト(~10個のモジュール)があり、そのうちビルドには毎回20~30秒程度かかります。Android Studioで実行を押すと、アプリの再構築のために毎回待たなければならず、非常に時間がかかります。
Android Studioでビルドプロセスを自動化することは可能でしょうか?あるいは、このプロセスを高速化するためのアドバイスがあれば教えてください。
Eclipseでは、自動ビルドのおかげで、同じプロジェクトをエミュレータ上で実行しても3~5秒程度で済みます。
これは私のbuild.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
}
}
申し訳ありませんが、開発ステーションをSSDにアップグレードしたり、大量のRAMを搭載したりすることは、以下の点を合わせたよりも大きな影響を与えるでしょう。
ビルドのパフォーマンスを上げることは開発チームにとって最優先事項なので、最新のGradleとAndroid Gradle Pluginを使っていることを確認してください。
任意のディレクトリに gradle.properties
という名前のファイルを作成します。
/Users/<username>/.gradle/
(Mac)Append:
# 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.
# https://medium.com/google-developers/faster-android-studio-builds-with-dex-in-process-5988ed8aa37e#.krd1mm27v
org.gradle.jvmargs=-Xmx5120m -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
# Set to true or false to enable or disable the build cache.
# If this parameter is not set, the build cache is disabled by default.
# http://tools.android.com/tech-docs/build-cache
android.enableBuildCache=true
Gradleのプロパティは、projectRoot\gradle.properties
に置くとローカルに、user_home\gradle.properties
に置くとグローバルに動作します。また、コンソールからgradleタスクを実行した場合や、アイデアから直接実行した場合にもプロパティが適用されます。
IDE設定のGUIから、GradleとIntelliJの連携を調整することができます。オフライン作業を有効にすると(下記yavaの回答を参照してください)、すべての"sync gradle file"で実際のネットワーク要求を無効にすることができます。
.
apkのビルドで最も時間のかかるステップの1つは、javaバイトコードを単一のdexファイルに変換することです。ネイティブマルチデックス(minSdk 21 for debug builds only)を有効にすると、ツールの作業量を減らすことができます(下記のAksel Willgertの回答を参照)。
ライブラリのサブプロジェクトよりも @aar
の依存関係を優先してください。
aar パッケージを mavenCentral, jCenter で検索するか、jitpack.io を使って github からライブラリをビルドしてください。依存関係にあるライブラリのソースを編集していない場合は、プロジェクトのソースで毎回ビルドするべきではありません。
プロジェクトファイルやキャッシュファイルをアンチウイルスのスキャン対象から外すことを検討してください。これは明らかにセキュリティとのトレードオフです(自宅では試さないでください!)。しかし、ブランチを頻繁に切り替える場合、gradleプロセスに使用させる前にアンチウイルスがファイルを再スキャンするため、ビルド時間が遅くなります(特にAndroidStudioのsync project with gradle files and indexing tasks)。アンチウイルスを有効にした場合としない場合のビルド時間とプロセスのCPUを測定して、関係があるかどうかを確認してください。
Gradleには、プロジェクトのプロファイリングのサポートが組み込まれています。プロジェクトによって、使用しているプラグインやカスタムスクリプトの組み合わせは異なります。--profile`を使うと、ボトルネックを見つけるのに役立ちます。
あらゆるところを調べて、ようやく私たちに合った解決策を見つけました。パラレルビルドを有効にして(OSXではpreferences -> compiler -> gradle -> "Compile independent modules in parallel"
)、 'make project automatically'を有効にすると、1分程度だったのが20秒程度に短縮されました。Thanks to /u/Covalence.
http://www.reddit.com/r/androiddev/comments/1k3nb3/gradle_and_android_studio_way_slower_to_build/