Creating launcher icons for Android applications can be a repetitive task, especially if you’re handling multiple resolutions. Instead of manually resizing images every time, you can use a bash script to automate this process. This guide will walk you through creating a bash script that generates Android application launcher icons in various sizes, simplifying your workflow.
Why Use a Bash Script for Icon Generation?
If you’re wondering why you should bother with a bash script, here are a few reasons:
- Time-Saving: Automating the icon generation process saves you time, especially when dealing with multiple projects.
- Consistency: A script ensures that all your icons are generated with the exact dimensions and format required by Android.
- Simplicity: Once the script is set up, generating icons is as simple as running a single command.
Step-by-Step Guide to Creating the Bash Script
Here’s how you can create a bash script to generate Android application launcher icons:
Step 1: Set Up Your Environment
Before you begin, make sure you have ImageMagick
installed, as it’s the tool we’ll use for resizing images. You can install it on Ubuntu with the following command:
sudo apt-get install imagemagick
Step 2: Write the Bash Script
Create a new bash script file named generate-icons.sh
:
touch generate-icons.sh
Open the file in your favorite text editor and start by adding the following code:
#!/bin/bash
# Check if an image file was provided
if [ -z "$1" ]; then
echo "Please provide the path to the image file."
exit 1
fi
# Define the icon sizes required for Android launcher icons
sizes=(48 72 96 144 192 512)
# Loop through each size and generate the corresponding icon
for size in "${sizes[@]}"; do
convert "$1" -resize ${size}x${size} "ic_launcher_${size}x${size}.png"
echo "Generated ic_launcher_${size}x${size}.png"
done
This script takes an input image file and generates icons in all the necessary sizes for Android.
Step 3: Make the Script Executable
To run the script, you need to make it executable:
chmod +x generate-icons.sh
Step 4: Run the Script
Now, you can generate icons by running the script with an image file as an argument:
./generate-icons.sh path/to/your/image.png
This command will produce launcher icons named ic_launcher_48x48.png
, ic_launcher_72x72.png
, and so on.
Example Use Case
Imagine you’ve just designed a new logo for your Android app, and it’s in a file called logo.png
. By running the script:
./generate-icons.sh logo.png
You’ll instantly get all the launcher icons you need, without having to resize each one manually.
Troubleshooting Tips
- ImageMagick Not Found: If you encounter an error saying
convert
command not found, ensure that ImageMagick is installed correctly and is available in your system’s PATH. - Incorrect Resizing: If the icons don’t resize correctly, double-check the image file you provided and ensure it’s in a format that ImageMagick can process.
Conclusion
Using a bash script to generate Android launcher icons is a simple yet effective way to streamline your development process. It ensures consistency across your icons and saves you the hassle of manual resizing. Once you’ve set up the script, creating icons becomes a one-command task, freeing up your time for more important aspects of app development.
The Other Information
Android Launcher icon sizes are as following,
48 × 48 (mdpi)
72 × 72 (hdpi)
96 × 96 (xhdpi)
144 × 144 (xxhdpi)
192 × 192 (xxxhdpi)
512 × 512 (Google Play store)
So, when you create an image with one size, we need to convert this image icon to match with different screen sizes.
The below shell script will help you to create android app launcher icons for different screen resolutions.
Create the shell script as below,
$ vim make_android_icon.sh
#!/bin/sh
#argument image should be with png format
#cleanup previous icons
DIR_TO_SAVE_ICONS=$PWD/android_icons
rm -rf $DIR_TO_SAVE_ICONS
ICON_NAME=$1 #take icon name as first argument
echo "Creating android icons of different dimensions for $ICON_NAME"
mkdir -p $DIR_TO_SAVE_ICONS/drawable-xxxhdpi/
mkdir -p $DIR_TO_SAVE_ICONS/drawable-xxhdpi/
mkdir -p $DIR_TO_SAVE_ICONS/drawable-xhdpi/
mkdir -p $DIR_TO_SAVE_ICONS/drawable-hdpi/
mkdir -p $DIR_TO_SAVE_ICONS/drawable-mdpi/
mkdir -p $DIR_TO_SAVE_ICONS/drawable-ldpi/
convert $ICON_NAME -resize 192x192 $DIR_TO_SAVE_ICONS/drawable-xxxhdpi/$ICON_NAME
convert $ICON_NAME -resize 144x144 $DIR_TO_SAVE_ICONS/drawable-xxhdpi/$ICON_NAME
convert $ICON_NAME -resize 96x96 $DIR_TO_SAVE_ICONS/drawable-xhdpi/$ICON_NAME
convert $ICON_NAME -resize 72x72 $DIR_TO_SAVE_ICONS/drawable-hdpi/$ICON_NAME
convert $ICON_NAME -resize 48x48 $DIR_TO_SAVE_ICONS/drawable-mdpi/$ICON_NAME
convert $ICON_NAME -resize 36x36 $DIR_TO_SAVE_ICONS/drawable-ldpi/$ICON_NAME
#create a zip to copy to android app, res folder
cd $DIR_TO_SAVE_ICONS
zip -r $ICON_NAME.zip .
echo "Icon zip $ICON_NAME.zip is ready at $DIR_TO_SAVE_ICONS"
Now, you need to pass the icon / image name which you want to convert to different sizes as first argument to shell script.
$ make_android_icon.sh image.png
Once this script is executed, you will get the different icons at respective drawable folders.
Extract above generated .zip inside res folder of application source.
Reference : https://iconhandbook.co.uk/reference/chart/android/