Batch Files

Batch Files for Repetitive Commands

Save the commands you type hundreds of times in a script in the project folder: on Windows a .bat file, run by typing its name without the extension; on Linux and macOS any file you mark executable. Both versions below build BookNest, then install and launch it with adb, which addresses exactly the device you name:

r.bat for Windows (not run here: this book's build machine runs Linux)
@echo off
rem r.bat: build BookNest, then install and launch it on one Android target
cd /d "%~dp0"
set TARGET=%1
if "%TARGET%"=="" set TARGET=emulator-5554
set APK=platforms\android\app\build\outputs\apk\debug\app-debug.apk
call cordova build android || exit /b 1
adb -s %TARGET% install -r %APK% || exit /b 1
adb -s %TARGET% shell am start -n com.example.booknest/.MainActivity
r for Linux and macOSShell
#!/usr/bin/env bash
# r: build BookNest, then install and launch it on one Android target
set -euo pipefail
cd "$(dirname "$0")"                  # run from the project folder, wherever you call it from
target="${1:-emulator-5554}"          # ./r emulator-5558, or a USB device serial
apk=platforms/android/app/build/outputs/apk/debug/app-debug.apk
cordova build android
adb -s "$target" install -r "$apk"
adb -s "$target" shell am start -n com.example.booknest/.MainActivity

On Windows, cordova is itself a batch file (cordova.cmd), and a batch file that runs another without call never returns. In bash, set -euo pipefail stops at the first failing command, so a broken build is never installed. install -r replaces the app but keeps its data. Make the script executable once, then run it:

Making the bash script executable and running itShell
chmod +x r
./r emulator-5558
Output
...
BUILD SUCCESSFUL in 1m 17s
...
Performing Streamed Install
Success
Starting: Intent { cmp=com.example.booknest/.MainActivity }