Batch installing Moodle with fallback to the previous versions

Download the previous versions of Moodle, put them into a folder.

In the enclosing folder, create a new file:

#!/bin/bash

## ` ? ? ? ~/L/Cl/OneDrive-H/Uganda Jan 2025/moodle_apk ? `aapt dump badging Moodle_3.9.4.apk | head -3                                ? ? ? at 14:11:28 ? 
## package: name='com.moodle.moodlemobile' versionCode='39400' versionName='3.9.4' platformBuildVersionName='3.9.4' platformBuildVersionCode='39400' compileSdkVersion='28' compileSdkVersionCodename='9'
## sdkVersion:'19'
## targetSdkVersion:'29'
## 
##  ? ? ? ~/L/Cl/OneDrive-H/Uganda Jan 2025/moodle_apk ? aapt dump badging Moodle_3.9.5.apk | head -3                                ? ? ? at 14:11:53 ? 
## package: name='com.moodle.moodlemobile' versionCode='39503' versionName='3.9.5' platformBuildVersionName='10' platformBuildVersionCode='29' compileSdkVersion='29' compileSdkVersionCodename='10'
## sdkVersion:'22'
## targetSdkVersion:'30'
## 
##  ? ? ? ~/L/Cl/OneDrive-H/Uganda Jan 2025/moodle_apk ? aapt dump badging Moodle_4.2.0.apk | head -3                                ? ? ? at 14:12:04 ? 
## package: name='com.moodle.moodlemobile' versionCode='42000' versionName='4.2.0' platformBuildVersionName='13' platformBuildVersionCode='33' compileSdkVersion='33' compileSdkVersionCodename='13'
## sdkVersion:'22'
## targetSdkVersion:'33'
## 
##  ? ? ? ~/L/Cl/OneDrive-H/Uganda Jan 2025/moodle_apk ? aapt dump badging Moodle_4.3.0.apk | head -3                                ? ? ? at 14:12:28 ? 
## package: name='com.moodle.moodlemobile' versionCode='43002' versionName='4.3.0' platformBuildVersionName='13' platformBuildVersionCode='33' compileSdkVersion='33' compileSdkVersionCodename='13'
## sdkVersion:'22'
## targetSdkVersion:'33'
## 
##  ? ? ? ~/L/Cl/OneDrive-H/Uganda Jan 2025/moodle_apk ? aapt dump badging Moodle_4.4.0.apk | head -3                                ? ? ? at 14:12:35 ? 
## package: name='com.moodle.moodlemobile' versionCode='44004' versionName='4.4.0' platformBuildVersionName='13' platformBuildVersionCode='33' compileSdkVersion='33' compileSdkVersionCodename='13'
## sdkVersion:'24'
## targetSdkVersion:'33'
## 
##  ? ? ? ~/L/Cl/OneDrive-H/Uganda Jan 2025/moodle_apk ? aapt dump badging Moodle_4.4.1.apk | head -3                                ? ? ? at 14:12:43 ? 
## package: name='com.moodle.moodlemobile' versionCode='44100' versionName='4.4.1' platformBuildVersionName='14' platformBuildVersionCode='34' compileSdkVersion='34' compileSdkVersionCodename='14'
## sdkVersion:'24'
## targetSdkVersion:'34'
## 
##  ? ? ? ~/L/Cl/OneDrive-H/Uganda Jan 2025/moodle_apk ? aapt dump badging Moodle_4.5.0.apk | head -3                                ? ? ? at 14:12:49 ? 
## package: name='com.moodle.moodlemobile' versionCode='45002' versionName='4.5.0' platformBuildVersionName='14' platformBuildVersionCode='34' compileSdkVersion='34' compileSdkVersionCodename='14'
## sdkVersion:'24'
## targetSdkVersion:'34'


APK_DIR="moodle_apk" # Path to your APK directory
DEVICE_SERIAL=$(adb devices | grep -w 'device' | awk '{print $1}')

if [ -z "$DEVICE_SERIAL" ]; then
    echo "No device connected. Exiting..."
    exit 1
fi

echo "Device detected: $DEVICE_SERIAL"

# Sort APK files by the numerical version in the filename
APK_FILES=$(ls $APK_DIR/Moodle*.apk | sort -Vr -t 'e' -k2)

if [ -z "$APK_FILES" ]; then
    echo "No APK files found in $APK_DIR. Exiting..."
    exit 1cd 
fi

for apk in $APK_FILES; do
    echo "Installing $apk..."
##    adb install -r "$apk" > /dev/null 2>&1
    adb install -r "$apk"

    if [ $? -eq 0 ]; then
        echo "$apk installed successfully."

        # Launch the app to verify (customize the package and activity name below)
        PACKAGE_NAME=$(aapt dump badging "$apk" | awk -F"'" '/package: name=/{print $2}')
        LAUNCH_ACTIVITY=$(aapt dump badging "$apk" | awk -F"'" '/launchable-activity: name=/{print $2}')

        if [ -n "$PACKAGE_NAME" ] && [ -n "$LAUNCH_ACTIVITY" ]; then
            adb shell am start -n "$PACKAGE_NAME/$LAUNCH_ACTIVITY" > /dev/null 2>&1

            # Simulate some automated test or wait
            echo "Launching app: $PACKAGE_NAME/$LAUNCH_ACTIVITY"
            sleep 10 # Adjust this as needed for app initialization

            # Check if the app is running
            RUNNING=$(adb shell pidof "$PACKAGE_NAME")
            if [ -n "$RUNNING" ]; then
                echo "App $PACKAGE_NAME is running successfully. Exiting..."
                exit 0
            else
                echo "App $PACKAGE_NAME failed to launch. Uninstalling and trying next version..."
                adb uninstall "$PACKAGE_NAME" > /dev/null 2>&1
            fi
        else
            echo "Could not determine package or activity. Skipping $apk..."
        fi
    else
        echo "Failed to install $apk. Trying next version..."
    fi
done

echo "No working version found. Exiting..."
exit 1

Leave a Reply

Your email address will not be published. Required fields are marked *