Android studio plugin to clear cache

Hari Sudhan
Quality Content
Published in
4 min readApr 8, 2019

--

If the user needs to delete any library cache of a project, they have to traverse through the .gradle/cache/modules-2/files-2.1 and .gradle/cache/modules-2/metadata-x.xx paths to delete the cache. It really consumes much time of the user. This kind of situations will arise when the project consumes internal libraries with more frequent library release on the same version.

To avoid this manual deletion process, multiple ideas raised in my mind to automate the deletion of the cached library.

Idea 1 — Writing a Gradle script to clear cache:

I have written a Gradle script to automate the removal of caches (specific cache folders of our project that we manually remove). Below are the steps to add the script in your project.

  1. Create a new folder inside your app( In my case I named it as scripts )
  2. Create a .gradle file inside this folder ( In my case I named it as clearcache.gradle )
  3. Add the following script inside the newly created .gradle file

4. Add the below line to your app’s build.gradle

apply from: 'script/clearcache.gradle'

Now the script is ready. So whenever you want to delete the cache folders execute the following command in your terminal.

For Windows users:

gradlew clearCacheModules

For Mac users:

./gradlew clearCacheModules

This idea exhibited few disadvantages

  1. The user has to do these steps at least one time.
  2. Whenever the user wants to delete the library cache, they have to specify the package prefix in the Gradle script before running the script.
  3. No way for the user to select the library cache which is to be deleted.
  4. And this has to be done for every project.

To overcome these disadvantages, there comes another idea of creating a plugin.

Idea 2 — Creation of a plugin:

Created a plugin which takes care of the deleting library cache. This plugin provides a User Interface where the user has the option to select and delete the library cache they want. Used IntelliJ IDEA Community Edition 2018.1.6 to build this plugin which is compatible with Android Studio Version 3.2+.

I am not going to explain the steps, have a look at Marcos Holgado’s Write an Android Studio Plugin Part 1: Creating a basic plugin. This will give you an idea to create a plugin and also have a look at my project at GitHub to get an idea about the classes that I have written.

After writing the code using IntelliJ, you might be facing issues in generating the plugin jar. So, I will provide you the steps to generate the plugin jar.

Step 1: Select ProjectStructure from File menu.

Step 2: In the ProjectStructure window select Artifacts.

Step 3: Click the + icon at the top and select From modules with dependencies option under the JAR option menu.

Step 4: In the Create JAR from Modules popup window select the main module(In our case it is Clear_Cache_Plugin_main) in Module dropdown leaving other fields empty.

Step 5: Right click the jar file in the output layout tab and create a directory in the name of META-INF.

Step 6: Right click the META-INF directory and add your plugin.xml file in it and finally click the Apply button.

Step 7: Now you are ready to build a plugin. Select BuildArtifacts… from Build menu and build the artifact. Once building process is done you could see the plugin jar inside the out/artifacts folder path like below.

The generated plugin can be installed locally to your android studio or other compatible IDEs. But to make the plugin public, it should be published in any available repositories.

Currently, this plugin is published in JetBrains and available for public use.

You can install this plugin in your Android Studio and can start using it. Installation screenshots are available in my github repository.

Also Compatible with: IntelliJ IDEA, PhpStorm, WebStorm, PyCharm, RubyMine, AppCode, CLion, GoLand, DataGrip, Rider, MPS

If you found this post useful, it would help others to find it if you could click on the 👏 icon below and also do follow for more articles like this — thanks!

--

--

Hari Sudhan
Quality Content

Learn, extract, and teach others something old but rarely realised in every step of your life.