Skip to content

Instantly share code, notes, and snippets.

@HemanthJabalpuri
Created November 20, 2023 05:59
Show Gist options
  • Save HemanthJabalpuri/35ce839b3ecb041a40a7a67e4ed466ab to your computer and use it in GitHub Desktop.
Save HemanthJabalpuri/35ce839b3ecb041a40a7a67e4ed466ab to your computer and use it in GitHub Desktop.
Shell script to debloat using UAD json
#!/system/bin/sh
ADBTMP=/data/local/tmp
AAPT=aapt_arm_pie
terminaltty=$(tty)
tmpd="$PWD"; [ "$PWD" = "/" ] && tmpd=""
case "$0" in
/*) cdir="$0";;
*) cdir="$tmpd/${0#./}";;
esac
cdir="${cdir%/*}"
cp "$cdir/$AAPT" $ADBTMP || exit 1
chmod 777 $ADBTMP/$AAPT
uad="$cdir/uad_lists.json"
getAppName() {
name="$($ADBTMP/$AAPT d badging $1 | grep application-label:)"
eval name=${name/"application-label:"/}
[ "$name" = "" ] && name="NULL OR EMPTY"
echo "$name"
}
debloatit() {
pac="$1"
echo "Remove? (d - disable)(u - uninstall)"
read choice <"$terminaltty"
case "$choice" in
y|Y) pm uninstall --user 0 $pac || pm disable-user --user 0 $pac;;
d|D) pm disable-user --user 0 $pac;;
u|U) pm uninstall --user 0 $pac;;
*) return;;
esac
}
reinstall() {
pac="$1"
disabled=0
pm list packages -d | grep -q "${pac}$" && disabled=1
apkpath="$(pm dump $pac | awk '/path/{ print $2 }' | tail -n 1)"
[ -z "$apkpath" ] && return #why empty?
label="$(getAppName $apkpath)"
echo "Reinstall ${label} ( $pac )? (y|Y)"
read choice <"$terminaltty"
case "$choice" in
y|Y)
if [ "$disabled" -eq 0 ]; then cmd package install-existing --user 0 $pac
else pm enable $pac
fi
;;
*) return;;
esac
}
rmedpkgs="$((pm list packages -u && pm list packages) | sort | uniq -u)"
rmedpkgs="${rmedpkgs}
$(pm list packages -d)"
rmedpkgs="${rmedpkgs//"package:"/}"
if [ "$1" = reinstall ]; then
for app in $rmedpkgs; do
reinstall $app
done
else
apps="$(pm list packages)"
apps="${apps//"package:"/}"
echo "$apps" | while read app; do
[ -z "$app" ] && continue
if echo "$rmedpkgs" | grep -q "^$app\$"; then continue; fi
string="\"id\": \"$app\""
if grep -q "$string" "$uad"; then
data="$(grep -A 6 "$string" "$uad")"
desc="$(echo "$data" | grep '"description":' | cut -d \" -f 4)"
rem="$(echo "$data" | grep '"removal":' | cut -d \" -f 4)"
if [ "$rem" = "Recommended" ]; then
echo "\n[$rem] $app - $desc\n"
debloatit $app
fi
fi
done
fi
rm -f $ADBTMP/$AAPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment