Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Optimizing Gradle Daemon’s memory and cache usage

 

 
Rafa Araujo
Android Developer @ Paris, France
Published: July 21, 2020
Tweet
Share
 

Photo by Felix Mittermeier from Pexels

 

Versão em português aqui

When we build our project with Gradle, either by command line or by Android Studio, a long-running background process that runs the builds called Daemon is used.

The Daemon is used not only to avoid the cost of starting the JVM for each build but also to cache information about the project structure, files, tasks, and more in memory. This makes the next compilations much faster.

Android Studio contains its own JVM, which is installed inside it and is used for its builds within the IDE, therefore, its own Daemon instance.
So far so good … Right? But if you also use Gradle by command line, it will not be using the instance started by the IDE, but a new one as the terminal will be using your machine’s JVM.

What does that mean? That you will have two instances of the Daemon with cache and everything else running on your machine and consuming more memory.
Also, if you modify the file system present in one of the builds, when using the second one it will build again because it does not know about the changes in the first.

How to solve this? Use the same java in both places.

 

Django Unchained, great movie!


đź’» Setting the environment variable

 

In the terminal, type echo $JAVA_HOME to find out if the variable has been previously configured. If so, skip this step.

 

In this step, you should identify where your Java is installed and add your installation path as an environment variable under the name of JAVA_HOME.

macOS

Probably, Java is located in /usr/libexec/java_home.

On file ~/.bashrc if you use bash, or ~/.zshrc if you use zsh, add the line with the path between inverted quotes:

 

export JAVA_HOME=`/usr/libexec/java_home`

 

Restart the terminal, or run the command source ~/.bashrc if you use bash, or source ~/.zshrc if you use zsh, to reload the file without having to restart it.


Windows

Probably, Java is located in C:\Program Files\Java\jdk1.8.0_XX/.

In your terminal, entersetx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0_XX".

Restart your terminal.


Linux

Probably, Java is located at /usr/lib/jvm/java-1.x.x-openjdk or usr/bin/javac. You can also run the update-alternatives — config javacommand that lists the installed jdk options and their respective directories.

On file/etc/profile, add the line with the found path:

 

export JAVA_HOME=usr/bin/javac

 

Restart your terminal.

 

If you need help, you can count on this link directly from the Java website

 


🤖 Setup on Android Studio

For safety protection, for Android Studio to recognize JAVA_HOME, restart your IDE.

With your project opened, go to File ➡️ Project Structure, as shown below.

 

Image for post

Setup on Android Studio

 

In JDK Location, select the JAVA_HOME option, which Android Studio has probably already detected that you have this environment variable configured.

 

Image for post

Chose JAVA_HOME option

 

Restart your IDE et voilá! 👌

 

You did it!


🛑 Ending all Gradle Daemon if you need to

If after all steps you detect that there are still Gradle Daemons running, you can close them all on your terminal with the command:

 

pkill -f '.*GradleDaemon.*'

đź’°Bonus: Make your terminal “rich”

When executing a command per command line, the entire printed log will be in plain text, but if you use the --console=rich tag after the command to be executed, e.G.: ./gradlew clean --console=rich, we can leave the more “informative” terminal. What changes:

  • Log grouping.
  • Progress bar and stopwatch that describe the overall status.
  • The parallel lines of tasks in progress.
  • Colors and fonts will be used to highlight errors and important results.

 

Image for post

Terminal rich

 

To avoid the hassle of always having to add the --console=rich tag at the end of each command, you can configure it by default using a file called gradle.properties directly in the Gradle installed on your machine.

  • Create a file called gradle.properties inside your ~/.gradle/ folder (on Gradle settings in your “home” folder).
  • Within the file gradle.properties, add the line org.gradle.console=rich.

Each command will be executed automatically with --console=richbecause the new gradle.properties will be merged with your project’s gradle.properties.

If your project’s gradle.properties contains the same tag as the local file, your project’s file tag will be used, overwriting the local file’s tag.

 

If you want plain text, use --console=plain instead of --console=rich

 


Final thoughts

Not all paths and versions probably will match with your setup, but if you set up it correctly you’ll be able to run a single instance of Daemon/JAVA on Android Studio and terminal. Good code!

 

Contacts

You can find me on Twitter @orafaaraujo.
Feedback is welcome!

 

Android Community on Brazil

Join the largest forum on Android in Brazil slack Android Dev BR.
Site: http://www.androiddevbr.org | Invite: http://slack.androiddevbr.org

 

Thank you!

 

 

Tags: Android, Gradle, Android App Development, Gradle Plugin, Daemon

 

View original article at: 


 

Originally published: June 26, 2020

Android News
Compose CameraX on Android
Compose CameraX on Android

By Peng Jiang

Android new UI toolkit Jetpack compose is in beta now, which has all the features you need to build production-ready apps. CameraX is another Jetpack support library, which let you control the camera easier. As compose is still under development, lots of the views are still not available the compose way.

By ProAndroidDev -
Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code With Kotlin Scopes
Noisy Code With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One