Adding new image assets to our Android app is tedious. Since our designers target iOS first, there are a few things the Android team has to do when we get new graphics:

  1. Ensure graphics have valid file names (no hyphens, no unnecessary @2x in the name).
  2. Ensure similar graphics (e.g. icons in the drawer) are the same size.
  3. Provide the graphic at all possible densities.

I’ve created a script to speed up the renaming process. Before running it, I only copy the retina (…@2x…) graphics to my working directory, and then run it on my Mac or Linux machine:

#!/bin/bash

# Standardizes image file names to be usable as Android resources.

# Make sure we have some files to rename before continuing
if [ -z "$1" ]; then
	echo "usage: renamer.sh file [file2...]"
	echo
	exit 1
fi

for INFILE; do
	# Do the stuff
	if [ "$INFILE" != "${INFILE//-/_}" ]; then
		# Replace hyphens with underscores
		NEWFILE=${INFILE//-/_}
		mv -i "$INFILE" "$NEWFILE"
	else
		NEWFILE=$INFILE
	fi

	if [ "$NEWFILE" != "${NEWFILE//@2x/}" ]; then
		# Remove retina-ified filenames
		mv -i "$NEWFILE" "${NEWFILE//@2x/}"
	fi

	# Sanity check
	echo "Moved $INFILE to ${NEWFILE//@2x/}"
done

Also on GitHub.

This is useful for doing multiple images at once, e.g. renamer.sh icon-*.png, and, of course, it helps when you like the designer’s naming scheme.

$ logout

Advertisements