Writing an automated script to quickly build, test and run Android APK’s.

Mayuri Khinvasara
Sep 8 · 5 min read

That’s almost saving an hour per day.

Image courtesy : https://developer.android.com/

Step 1 : Define your tasks that you want to automate

Step 2 : Create a bash script

cd /Users/mayuri/CODE/AndroidBuildAutomationSample
vim automate.sh
#Define all paths, constants here
PROJECT_DIR=’/Users/mayuri/CODE/AndroidBuildAutomationSample/’
OUTPUT_DIR=’/Users/mayuri/CODE/AndroidBuildAutomationSample/OUTPUT_DIR/’
#Enter project dir
cd $PROJECT_DIR
print_blue(){
printf "\e[1;34m$1\e[0m"
}
#Start Clean Process
print_blue "\n\n\nStarting"
print_blue "\n\n\nCleaning...\n"
./gradlew clean
print_blue "\n\n\ncleanBuildCache...\n"
./gradlew cleanBuildCache
print_blue "\n\n\n build...\n"
./gradlew build
./gradlew installDebugAndroidTest
print_blue "\n\n\n assembleDebug...\n"
./gradlew assembleDebug
print_blue “installDebug…\n”
./gradlew installDebug
print_blue "\n\n\n Done Installing\n"
./gradlew uninstallDebug

Step 3 : Launch MainActivity via script

#Launch Main Activity
adb shell am start -n "com.sample.androidbuildautomationsample/com.sample.androidbuildautomationsample.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
print_blue "\n\n\n Launched main activity\n"
export PATH=/Users/mayurik/Library/Android/sdk/platform-tools/adb:/Users/mayurik/Library/Android/sdk/tool

Step 4 : Define Output folder and copy APK into that.

#Copy APK to output folder
cp "$PROJECT_DIR"app/build/outputs/apk/debug/app-debug.apk $OUTPUT_DIR
print_blue "\n\n\n Copying APK to outputs Done\n"

Step 5: Grant permissions to make your script executable (Mandatory)

chmod +x automate.sh

Step 6: Run your script

./automate.sh

Step 7: Do some personalizations (Optional)

Step 8 : Additional Changing command line colors (quick way to find out which all tasks were run and errors if any)

# Functions for customizing colors(Optional)
print_red(){
printf "\e[1;31m$1\e[0m"
}
print_green(){
printf "\e[1;32m$1\e[0m"
}
print_yellow(){
printf "\e[1;33m$1\e[0m"
}
print_blue(){
printf "\e[1;34m$1\e[0m"
}

Step 9 : Error Handling

Step 10 : Add your own commands

./gradlew --tasks

Final Script :

#Define all paths, constants here
PROJECT_DIR='/Users/mayuri/CODE/AndroidBuildAutomationSample/'
OUTPUT_DIR='/Users/mayuri/CODE/AndroidBuildAutomationSample/OUTPUT_DIR/'
# Functions for customising print colors (Optional)
print_red(){
printf "\e[1;31m$1\e[0m"
}
print_green(){
printf "\e[1;32m$1\e[0m"
}
print_yellow(){
printf "\e[1;33m$1\e[0m"
}
print_blue(){
printf "\e[1;34m$1\e[0m"
}
#Enter project dir
cd $PROJECT_DIR
#Start Build Process
print_blue "\n\n\nStarting"
print_blue "\n\n\nCleaning...\n"
./gradlew clean
print_blue "\n\n\ncleanBuildCache...\n"
./gradlew cleanBuildCache
print_blue "\n\n\n build...\n"
./gradlew build
print_blue "\n\n\n assembleDebug...\n"
./gradlew assembleDebug
#Install APK on device / emulator
print_blue "installDebug...\n"
./gradlew installDebug
print_blue "\n\n\n Done Installing\n"#Launch Main Activity
adb shell am start -n "com.sample.androidbuildautomationsample/com.sample.androidbuildautomationsample.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
print_blue "\n\n\n Launched main activity\n"#Copy APK to output folder
cp "$PROJECT_DIR"app/build/outputs/apk/debug/app-debug.apk $OUTPUT_DIR
print_blue "\n\n\n Copying APK to outputs Done\n"

Mayuri Khinvasara

Written by

Android Developer 💙 | Learner, Speaker and Writer | Leading GDG & Women Techmakers Pune | Passionate about technology, community | Curious Student for life !

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade