Increase your Gradle Build Speed

Increase your Gradle Build Speed

Long build times slow down your development process, so to overcome the long build process there are some important techniques to help you resolve build speed.

1. Always use the latest Gradle Plugin for Android

Android studio incrementally improves performance in newer gradle version so it is recommended to use latest one.Currently latest gradle plugin is 5.1.1.Newer version always resolved unwanted compilation errors or compatibility issues, follow the migration guide when upgrading your Gradle using android studio.

For more details about gradle latest version follow: https://gradle.org/whats-new/gradle-5/

No alt text provided for this image
No alt text provided for this image

2. Enable Offline mode, Gradle daemon and parallel build for the project

On slow network connections, while build the gradle it suffers problem to resolve the dependencies.You need to tell Gradle to avoid using network resources by using only the artifacts that it has cached locally.

To use Gradle offline when building with Android Studio, proceed as follows:

  • Open the Preferences window by clicking File > Settings (on Mac, Android Studio > Preferences).
  • In the left pane, click Build, Execution, Deployment > Gradle.
  • Check the Offline work checkbox.
  • Click Apply or OK.
  • If you're building from the command line, pass the --offline option.
No alt text provided for this image

3. Don' use the dynamics versions

This means you tell gradle that I want the latest version of that library so every 24 hours gradle checks for the newer version or updates which causing your dependencies resolution time to increase.

Always use specific dependency versions! 

No alt text provided for this image


4. Use Instant Run

Instant run is introduced in Android studio 2.0. The team said that Instant run in Android studio 3.0 has a lot of improvements and also better than the earlier version.

What is Instant Run?

In an ordinary build and run the whole APK file is updated and pushed to the device. But when using instant run only the updated part gets replaced in the device. Instant Run pushes updated code and resources to your connected device or emulator by performing a hot swap, warm swap, or cold swap. It automatically determines the type of swap to perform based on the type of change you made.

It may be faster than an ordinary build. But it requires the application to be running at the time of build below API 21.

No alt text provided for this image

5. Add below flags in gradle.properties

#1 org.gradle.jvmargs=-Xmx2048m

Increase the amount of memory allocated to the Gradle Daemon VM to 2 Gb.  

# 2 org.gradle.daemon=true

This dramatically improves the performance of subsequent builds. This is background process that allows data and code to store in memory which can used for next build .

# 3 org.gradle.configureondemand=true

This feature can save you a couple of seconds from the Configuration phase of Gradle, as only ‘projects’ related to the current tasks are configured. This setting is relevant for multi- modules projects. When this property is set, Gradle configures modules that are only relevant to the requested tasks instead of configuring all of them, which is a default behavior.

# 4 org.gradle.parallel=true

This allows the execution of tasks from different projects in parallel. All modules in your Android app count as separate ‘projects’ in Gradle terms, so this feature is especially useful for modularised apps.

# 5 android.enableBuildCache=true

Build cache feature that can speed up build times (including full builds, incremental builds, and instant run) by storing and reusing files/directories that were created in previous builds of the same or different Android project.

Currently, the build cache contains only pre-dexed libraries; in the future, we will use it for caching other types of files as well.

# 6 org.gradle.caching=true

Turn on Gradle caching

Gradle caching is introduced in Gradle version 3.5. It reuses the outputs from the previous build. It can work across the projects and gives a high performance when used with Android Studio 3.0.

 

6. Disable Libraries from your debug builds (Ex. Crashlytics,PNG crunching )

As we know that we don't need crashlytics report in debug build , so it is better to disabled it.

android {
...
buildTypes {
debug {
ext.enableCrashlytics = false
}
}

PNG crunching 

Disabled automatic conversion of PNG images to WebP . If you're using Android plugin 3.0.0 or higher, PNG crunching is disabled by default for only the "debug" build type.

 android {
...
release {
crunchPngs false
}
}

At I/O ’17, google introduced most important tips from which you can fast your android gradle build.

For more details you can watch the video - how to speed up your android gradle builds.

For more details about latest gradle - What's new in Gradle 5.0

Happy Coding....



要查看或添加评论,请登录

Rohan Lodhi的更多文章

  • Android Developer RoadMap

    Android Developer RoadMap

    While Going through some of the android articles I found below image . This image explains all things which Android…

    6 条评论
  • Android Studio?Plugins

    Android Studio?Plugins

    Studio Plugins which makes development simpler!!! 1.Material Design Icon Generater Android Studio & IntelliJ Plugin for…

  • Adding Google Admob Ads to Android App

    Adding Google Admob Ads to Android App

    Main Objective of this blog post is to explain how to integrate Admob Ads in Android Application using Android…

  • This article shows you how to use Kotlin language for Development of Android application.

    This article shows you how to use Kotlin language for Development of Android application.

    The Android team announced during Google I/O 2017 that Kotlin is now an official language to develop Android Apps. This…

    1 条评论
  • Security While Sending the Data to Server Using SSL(Secure Sockets Layer)

    Security While Sending the Data to Server Using SSL(Secure Sockets Layer)

    This article describes how to create a secure socket from an android device to a server using a certificate. It…

  • RecyclerView using CardView

    RecyclerView using CardView

    This article shows you how to create recyclerview using cardview and to hide toolbar on scroll down of view. Android…

  • TabLayout Using ViewPager

    TabLayout Using ViewPager

    This article shows you how to create tab layout using fragments and viewpager. Tablayout Tab Layout provides a…

    1 条评论
  • Twitter Integration

    Twitter Integration

    Main Objective of this blog post is to explain how to integrate twitter in an Android Studio . 1)First make a account…

  • LinkedIn Integration

    LinkedIn Integration

    Main Objective of this blog post is to explain how to LinkedIn in an Android Studio . 1)CREATE DEVELOPER ACCOUNT First…

  • FaceBook Integration

    FaceBook Integration

    Main Objective of this blog post is to explain how to Integrate Facebook in an Android Studio for SDK 4.0.