DEV Community

VRaptor Code
VRaptor Code

Posted on • Edited on

Understanding the Android Studio menus for cleaning and rebuilding a project

Do you know when you're testing an Android app and it crashes, or when you want to build it, but various errors show up in Logcat (Android's log terminal), and you decide to clean up through Android Studio, but have no idea what's really going on? Well, in this article, we will explain in a very straightforward and quick way each type of clean-up and which one is best to use.

Image description

File → Invalidate Caches / Restart

  • Purpose Clears the Android Studio cache and restarts the IDE. This resolves indexing issues, phantom errors, or broken configurations.

What is indexing in the context of Android Studio?
In Android Studio, indexing is the process by which the IDE scans, analyzes, and organizes all the project files (source code, libraries, dependencies, resources, etc.) into an internal index. This index makes various essential Android Studio functionalities faster and more efficient, such as:

  • Autocomplete
  • Refactoring (renaming variables, classes, methods)
  • Quick navigation between files and symbols
  • Real-time error validation
  • Efficient project-wide search
  • Going back to Invalidate Caches: Clears the temporary files and caches of Android Studio.
  • Forces the IDE to reindex the project.

Resolves issues related to autocomplete or configurations that aren't being applied correctly.

When to use:

  • Autocomplete is not working.
  • The project has errors that don't appear in the source code.
  • After updating plugins or libraries."

Build → Clean Project

  • Purpose: Removes all files generated during the build process, such as compiled classes, temporary libraries, and previous APKs.

What it does:

  • Deletes files in the project's build/ folder.
  • Ensures there are no remnants from old builds.
  • When to use:
  • After significant changes in the code.
  • When there are build issues that might be related to old remnants.

Maven equivalent: mvn clean

Build → Rebuild Project

  • Purpose: Performs a Clean followed by a full rebuild of the project.

What it does:

Removes all files in the build/ folder.
Recompiles the entire project from scratch.
When to use:

After significant changes in the code or dependencies.
When the project is not compiling correctly.
Maven equivalent: mvn clean install

Code → Code Cleanup in Android Studio.

  • What is it? The option Code → Code Cleanup is used to automatically format and optimize source code in Android Studio. It applies predefined coding style rules, removes unnecessary imports, and makes other improvements to the code, making it cleaner and more standardized.

What does Code Cleanup do?

Removal of Unused Imports:

Deletes imports that are not being used in the file.

Code Formatting:

Adjusts indentation, spacing, and line breaks according to the style set in Android Studio.

Code Optimization:

  • Replaces redundant instructions with more efficient alternatives.
  • Simplifies complex expressions.

Warning Fixes:

  • Automatically resolves small warnings identified by the Android Studio Lint.

Style Standardization:

  • Applies rules defined in the style configuration file (Code Style Settings).

How to use Code Cleanup?

  • Go to Code → Code Cleanup.
  • Select the files you want to clean (it can be a file, a directory, or the entire project).
  • Choose a cleanup profile (default or custom).
  • Click Run.

Before Code Cleanup:

fun greet() { println("Hello, World!") }
Enter fullscreen mode Exit fullscreen mode

The code is more organized, with proper indentation and spacing.

When to use Code Cleanup?

  • Before submitting code for review (code review).
  • After finishing a new feature.
  • To maintain consistency in coding style.
  • Now you know how to keep your code clean and beautiful in Android Studio!

Extras (Commands with Mvn)

mvn clean package

  • Purpose: Removes old compiled files (clean) and packages the project into a file (package, usually a JAR or APK in the case of Android).
  • When to use: After modifying the source code to ensure all changes are correctly applied to the final artifact.
  • mvn clean package Build → Clean Project: Removes old files and compiles.

mvn clean install

  • Purpose: Removes old files (clean), recompiles the project, and installs the resulting artifact (JAR or APK) into the local Maven repository. This ensures that dependencies and libraries are correctly available in the classpath. When to use: When adding or updating dependencies in the pom.xml file (or build.gradle in Android Studio). mvn clean install Build → Rebuild Project: Removes, compiles, and installs.

Summary:

  • File → Invalidate Caches: Clears cache and restarts Android Studio.
  • Build → Clean Project: Removes old compiled files.
  • Build → Rebuild Project: Cleans and recompiles the entire project.
  • Code → Code Cleanup: Formats, optimizes, and organizes the code.

Image description

That's all for today. I hope you enjoyed it. If so, I kindly ask you to comment on what you thought and don't forget to like.

Top comments (0)