How to Clean the iOS and Android Folders of a Flutter Project with a Makefile

How to Clean the iOS and Android Folders of a Flutter Project with a Makefile

A step by step guide on you can use a Makefile to automate the process of deleting these temporary files and folders in your Flutter project

Make is a powerful tool for automating software builds, and it can be used to streamline common development tasks such as cleaning up build artifacts. In this article, we will explore how to use a Makefile to clean the iOS and Android folders of a Flutter project.

When working on a Flutter project, you may find that the iOS and Android folders can quickly become cluttered with build artifacts and temporary files. Cleaning up these folders manually can be time-consuming and error-prone, and it can be difficult to ensure that all unnecessary files have been removed.

By using a Makefile to automate the process of cleaning these folders, you can ensure that the process is fast, reliable, and consistent. A Makefile can be easily customized to match the specific needs of your project, and it can be integrated into your existing build process.

To get started, create a file named Makefile in the root directory of your Flutter project. Open the file in your favorite text editor and follow the next steps:

Cleaning the iOS folder

To clean the iOS folder of a Flutter project, we can use the rm command to remove the Podfile, Podfile.lock, and Pods directories. Additionally, we can remove the .symlinks, .flutter-plugins, and .flutter-plugins-dependencies files, which are generated by the Flutter tooling.

Here's an example Makefile target for cleaning the iOS folder:

# Clean iOS folder
clean_ios:
    @echo "Cleaning iOS folder..."
    @cd ios && rm -rf Podfile Podfile.lock Pods .symlinks .flutter-plugins .flutter-plugins-dependencies
    @echo "iOS folder cleaned."

This target can be invoked by running the following command in your terminal:

make clean_ios

Cleaning the Android folder

To clean the Android folder of a Flutter project, we can use the rm command to remove the .gradle, build, and app/build directories. These directories contain build artifacts and other temporary files that can be safely removed.

Here's an example Makefile target for cleaning the Android folder:

# Clean Android folder
clean_android:
    @echo "Cleaning Android folder..."
    @cd $(ANDROID_PATH) && rm -rf .gradle/ build/ app/build/
    @echo "Android folder cleaned."

This target can be invoked by running the following command in your terminal:

make clean_android

Combining these sections, we have the below code:

# Makefile to clean iOS and Android folders of a Flutter project

# Define variables
IOS_PATH = ios/
ANDROID_PATH = android/
FLUTTER := flutter
FLUTTER_COMMAND := pub get

# Define targets
.PHONY: clean_ios clean_android get_dependencies

# Clean iOS folder
clean_ios:
    @echo "Cleaning iOS folder..."
    @cd $(IOS_PATH) && rm -rf Podfile Podfile.lock Pods .symlinks .flutter-plugins .flutter-plugins-dependencies
    @echo "iOS folder cleaned."
    @echo "Remember to run flutter pub get"

# Clean Android folder
clean_android:
    @echo "Cleaning Android folder..."
    @cd $(ANDROID_PATH) && rm -rf .gradle/ build/ app/build/
    @echo "Android folder cleaned."
    @echo "Remember to run flutter pub get"

# Run flutter pub get
get_dependencies:
    @echo "Running '$(FLUTTER) $(FLUTTER_COMMAND)' command..."
    $(FLUTTER) $(FLUTTER_COMMAND)

# Default target - clean both iOS and Android folders
clean: clean_ios clean_android get_dependencies

Let's go through this code step by step:

  • The IOS_PATH and ANDROID_PATH variables define the paths to the iOS and Android folders, respectively. These paths are relative to the root directory of your project.

  • The .PHONY directive tells Make that the clean_ios and clean_android targets are not actually files, but rather phony targets that should always be considered out-of-date.

  • The clean_ios target deletes the Podfile, Podfile.lock, Pods, .symlinks, .flutter-plugins, and .flutter-plugins-dependencies files and directories from the iOS folder.

  • The clean_android target deletes the .gradle/, build/, and app/build/ directories from the Android folder.

  • The clean target is a phony target that depends on the clean_ios and clean_android targets. Running make clean will clean both the iOS and Android folders.

If you want to clean both the iOS and Android folders at once, run the following command:

make clean

This will clean both folders in sequence.

In conclusion, using a Makefile to clean the iOS and Android folders of your Flutter project can help you keep your project organized and efficient. By automating this process, you can save time and reduce the risk of accidentally deleting important files or directories.

Connect with me on Linkedin, Twitter and Discord