Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NitinPraksash9911/edf781ce26f55c8ea95d2b543c275811 to your computer and use it in GitHub Desktop.
Save NitinPraksash9911/edf781ce26f55c8ea95d2b543c275811 to your computer and use it in GitHub Desktop.
This can be used whenever releasing app we have to generate the baseline profile for app-startup time improvement so this script remove overhead of manual doing all the stesp
#!/bin/sh
# this will exit the script if any command fails
set -e
set -o pipefail
## FOLLOW THIS STEPS FIRS TO SET EVN VARIABLE FOR THE FOLLOWING COMMANDS
# Set android home path
export ANDROID_HOME=~/Library/Android/sdk
#1. to install sdk-command-line tools
# https://stackoverflow.com/a/65477410/9076056
#2. this will set temporary env path for sdkmanager and avdmanager command
##https://developer.android.com/studio/command-line
#https://developer.android.com/studio/command-line/sdkmanager
#https://developer.android.com/studio/command-line/avdmanager
export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin
#3. this will set temporary env path for for emulator command
export PATH=$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$PATH
echo "Running macro-bench-mark script..."
sdkmanager --install "system-images;android-30;google_apis;x86"
echo "yes" | avdmanager --verbose create avd --force --name "pixel_4_xl" --device "pixel" --package "system-images;android-30;google_apis;x86" --tag "google_apis" --abi "x86"
#start emulator
emulator -avd pixel_4_xl -wipe-data & EMULATOR_PID=$!
# Wait for Android to finish booting
adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'
# Unlock the Lock Screen
adb shell input keyevent 82
# Clear and capture logcat
adb logcat -c
echo "Rooting the device..."
#https://developer.android.com/topic/performance/baselineprofiles#creating-profile-rules
adb root
#clean
./gradlew clean
#https://github.com/googlesamples/android-testing-templates/blob/master/AndroidTestingBlueprint/README.md#custom-gradle-command-line-arguments
./gradlew macrobenchmark:connected<BuildVarint>BenchmarkAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=<macrobenchmark_module_package_name>.BaselineProfileGeneratorClass
#pull baseline profile from device
adb pull "/sdcard/Android/media/<macrobenchmark_module_package_name>/additional_test_output/BaselineProfileGeneratorClass_startup-baseline-prof.txt"
#ex:- app.example.name.macrobenchmark
# rename baseline profile and move it to app/src/main
mv BaselineProfileGeneratorClass_startup-baseline-prof.txt ${pwd}app/src/main/baseline-prof.txt
kill $EMULATOR_PID
exit 0
#https://developer.android.com/topic/performance/baselineprofiles/create-baselineprofile
@NitinPraksash9911
Copy link
Author

task("generateMacrobenchmarkBaseline") {
commandLine("sh", "file-name.sh")
}
add above task to project-level gradle and also add the scrip file in your root project


from terminal:-
./gradlew generateMacrobenchmarkBaseline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment