Synchronise your Gradle files and get some insight about the Android Studio cache

How to manually synchronize your code with Gradle and understand the Android Studio cache
Synchronise your Gradle files and get some insight about the Android Studio cache

This tip explains how to synchronize your project with Gradle when updating build.gradle files and provides insight on handling the cache when using Android Studio.

There are multiple levels of cache. We will explain the difference between the cache behind the "Invalidate cache and restart" menu, the "Clean project" one and other gradle caches involved in building an Android project.

This article is not specific to Pepper QiSDK and introduces good practices for using the IDE.

Synchronize a project with Gradle Files

When you make changes to the build configuration files in your project, Android Studio requires that you synchronize (i.e. sync) your project files so that the IDE can import the new build configuration and run some tests to make sure there are no build errors.

In order to build projects, Android Studio needs the information contained in Gradle files. These files contain instructions on what dependencies to download and package within your application, information about versions, application id, and other information used to build the packages.

When you create a project you generally have two build.gradle files: one to configure project-wide properties and another one at module level. When your project structure view is set to Android the two files look as follow:

build.gradle (Project: YourFooAppName)

build.gradle (Module: app)

Gradle files and your application code need to be synced if any Gradle file changes, for example when updating dependencies or adding new dependencies manually.

This is the case when the project is robotified (see ‘Result' of section Creating a robot application). The build.gradle of your module will then get two lines added or updated to the API level you have chosen (the exact version number may vary):

implementation 'com.aldebaran:qisdk:1.7.5'
implementation 'com.aldebaran:qisdk-design:1.7.5'

Android Studio should automatically sync the project with Gradle when you robotify your app (ie make it compatible with Pepper's tablet).

If it does not, then choose menu File > Sync project with Gradle Files or use the elephant icon Sync project from the menu bar to sync your project files manually.

Gradle Sync project icon
Gradle Sync project icon

If Android Studio notices any errors with your configuration, the Messages window appears with more details about the issue.

Once Gradle files are synced, your project will have access to the necessary libraries, such as the QiSQK ones. You can then use code autocompletion to import libraries.

That's it!

Understanding Android Studio caches

Different types of caches

Android Studio caches a lot of files to speed up its use and build time. This cache is necessary to improve, for example, the performance of the IDE or some time-consuming tasks repeated at every build. This could lead to the system cache becoming overloaded or inconsistent.

There are different types of caches in Android Studio or used by Android Studio plugins. Let's check in details:

  • Android Studio System cache
  • Gradle build caching
  • AGP Android Gradle Plugin cache

Android Studio System cache

You can clear the Android Studio System cache using "Invalidate cache and restart". It mainly deals with the indexes and improves Android system performance.

Gradle build caching

This cache contains the built packages and the intermediate files involved at packaging your application. It can be found in two different locations:

  • In the build/ directory in your project
  • In the .gradle/caches/ directory of your Java SDK home directory

AGP Android Gradle Plugin cache

This cache contains other building caches and third party dependencies caches.

Now let's consider each of these caches and explain how to clear them.

How to clear all system caches

Clearing the Android Studio System cache

The Android Studio cache contains information about the project's structure, deals with the AS indexes and improves Android system performance.

You may need to clear the cache in several situations. For instance, moving some Java or Kotlin files or directories could lead to errors such as "Cannot resolve symbol". Using a copy of a project from another operating system may also require to clear this cache and clean the project.

If you are using Android Studio 3.6, the cache files are located in the folder $HOME/.AndroidStudio3.6/

To clear this cache, from the main menu in Android Studio , select File | Invalidate Caches / Restart. In the Invalidate Caches dialog, select an action. You can invalidate the cache and restart the IDE, invalidate the cache without restarting the IDE, or just restart the IDE.

When you invalidate the cache, Android Studio rebuilds all projects you had opened in the current version of the IDE.

Clearing the Gradle build cache

The Gradle build cache has two distinct parts located in two different directories:

  • In the build/ directory in your project
  • In the .gradle/caches/ directory of your Java SDK home directory

Build directory "cache" aka incremental builds

This cache handles Gradle generated code and files. Not all parts of the code are rebuilt at every build, only the necessary ones. This can speed up the build. It is located in the build/ directory(ies) within your project. If you clean and "assemble" your application, you can explore those directories to see what is generated.

In order to clean this cache, from the main menu in Android Studio, select Build | Clean project

You can also manually clean the cache using the terminal as follow:

$ ./gradlew clean

And to rebuild your app, run:

$ ./gradlew app:assembleDebug

If you want to investigate on what is built and cached, you can use the --scan option of gradlew as follows (but this is out of the scope of this article):

$ ./gradlew --scan app:assembleDebug

Build cache

The previous "cache" applies only to your current project, on your machine. However, there is another lesser known cache, named "build cache". The build cache stores some outputs that the Android plugin for Gradle generates when building your project, e.g. unpackaged AARs and pre-dexed remote dependencies. The build cache is enabled by default in recent plugins (2.3.0+) but can be activated/deactivated using the gradle property org.gradle.caching=true

This cache is located in the directory $JAVA_SDK_HOME/.gradle/caches/. In most cases $JAVA_SDK_HOME is your $HOME folder.

In order to clear the build cache run:

$ rm -rf $GRADLE_HOME/caches/build-cache-*

"Invalidate cache" has no impact on the "Build Cache", and "Clean Project" has no relation to the "Build Cache".

To clear gradle build caches:

  • Build directory cache (in your project)

# delete project-specific cache
$ ./gradlew clean

  • Build cache (in $JAVA_SDK_HOME/.gradle)

# delete build cache
$ rm -rf $GRADLE_HOME/caches/build-cache-*

Clearing the Android build cache

To complete the cache "tour", let's present one more layer of cache: the Android Gradle Plugin (AGP) adds some complementary caching over the Gradle build one. This cache stores outputs that the Android plugin for Gradle generates when building your project (such as unpackaged AARs and pre-dexed remote dependencies).

This cache is located in the $HOME/.android/build-cache/ directory.

In order to clear the AGP cache run:

$ ./gradlew cleanBuildCache

Cache cleaning cheat sheet

From time to time you may need to really clean your project, including all related caching. You could do that to measure how long a clean build takes, or if you have experienced a bug potentially related to one of the caches.

Let's go through all the necessary steps to build a project, having first cleared all of the caches and then rebuilt again using them all. To do so, run the following commands:

# clear the incremental build cache
$ ./gradlew clean

# clear the Gradle build cache
$ rm -rf $GRADLE_HOME/caches/build-cache-*

# clear the AGP cache
$ ./gradlew cleanBuildCache

# clear the 3rd party dependencies cache
$ rm -rf $GRADLE_HOME/caches/modules-2
$ rm -rf $GRADLE_HOME/caches/transform-2

# stop the gradle daemon
$ ./gradlew --stop

# run a build
$ ./gradlew --scan app:assembleDebug

BUILD SUCCESSFUL in 11m 49s
279 actionable tasks: 192 executed, 82 from cache, 5 up-to-date

# re-run a build without changing anything
$ ./gradlew --scan app:assembleDebug

BUILD SUCCESSFUL in 9s
279 actionable tasks: 279 up-to-date

The time gain can be that important. Cache is certainly useful, isn't it?

Conclusion

This article introduces Gradle sync and caching with Android Studio. In some situations you have to manually sync with Gradle files, run a "Clean project" or "Invalidate cache and restart", which are the main actions you should usually face when developing your apps. If they do not solve all of the issues when building your project, you also have a more complete view on how the system caching works in Android Studio.

You can explore more on the topic using Jason Atwood's very well documented presentation of the Android Studio cache (link in Related Content section below). It is really detailed and will help you understand more of the Android Studio intricacies.

We hope you learned some new things on how to use cache on Android Studio, and that it will be helpful for your future projects. May you code well, clean well and build well!