Skip to content

Instantly share code, notes, and snippets.

@iqfareez
Last active November 4, 2023 07:19
Show Gist options
  • Save iqfareez/f57405016cc8bcc51cdcd2879c88807a to your computer and use it in GitHub Desktop.
Save iqfareez/f57405016cc8bcc51cdcd2879c88807a to your computer and use it in GitHub Desktop.
Flutter APK Renamer

This Bash script is designed to rename APK files generated by Flutter.

Before: app-arm64-v8a-release.apk
After: MyAwesomeApp-armeabi-v8a-v1.0.0.apk

Note

Version number if extracted from pubspec.yaml file

Usage

The script requires two mandatory arguments and accepts an optional third:

  • AppName: The new name to replace the app part of the APK filenames.
  • /path/to/directory: The directory where the APK files are located.
  • path/to/pubspec.yaml: Optional path to the YAML file containing the version information. Default to pubspec.yaml. Example Command:
./rename_apk.sh MyApp build/app/outputs/apk/release

or

./rename_apk.sh MyApp build/app/outputs/apk/release /path/to/custom_pubspec.yaml

Tips: You can use the script directly without downloading using curl. Example:

curl -s https://gist.githubusercontent.com/iqfareez/f57405016cc8bcc51cdcd2879c88807a/raw/5d9bbed7231a9e56bf24b88ab5286cee73dae173/rename_apk.sh | bash -s MyApp build/app/outputs/flutter-apk  

Motivation

By default, Flutter output the generated apk or appbundle as app-<abi>-release.apk. The naming is boring, but can customize the filename using archivesBaseName property in app/build.gradle file. However, Flutter throws false positive error when building the app:

Gradle build failed to produce an .aab file. It's likely that this file was generated under [...], but the tool couldn't find it.

This can become headache especially in CI workflow. Thus, I make this script so that we can rename the apk files without depending on archivesBaseName property.

#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -lt 2 ]; then
echo "Usage: $0 AppName /path/to/directory [path/to/version.yaml]"
exit 1
fi
# Assign arguments to variables
APP_NAME="$1"
DIRECTORY="$2"
YAML_FILE="${3:-pubspec.yaml}" # Default to pubspec.yaml if not provided
# Check if the provided directory path is valid
if [ ! -d "$DIRECTORY" ]; then
echo "Error: The path '$DIRECTORY' is not a valid directory."
exit 1
fi
# Check if the YAML file exists
if [ ! -f "$YAML_FILE" ]; then
echo "Error: The file '$YAML_FILE' does not exist."
exit 1
fi
# Extract the version from the YAML file
# It assumes you have 'yq' installed on your system
version=$(yq e '.version' "$YAML_FILE" | cut -d'+' -f1)
# If version is empty, don't proceed
if [ -z "$version" ]; then
echo "Error: Version not found in the YAML file."
exit 1
fi
# Navigate to the specified directory
cd "$DIRECTORY" || exit
# Loop through all files that match the pattern "app-*-release.apk"
for file in app*-release.apk; do
# Skip if it's a directory or no files found
[ -f "$file" ] || continue
# Construct the new filename by replacing 'app' with the actual AppName and keeping the rest intact
new_filename="${file/app/$APP_NAME}"
new_filename="${new_filename/-release/-v$version}"
# Rename the file
mv "$file" "$new_filename"
# Output original filename to renamed filename
echo "'$file' has been renamed to '$new_filename'"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment