Systemless debloat support for all android version

This commit is contained in:
w1nst0n 2021-01-30 01:54:26 +01:00
parent 4a6ae80380
commit d0d73a2c2d

223
debloat_script.sh Executable file → Normal file
View file

@ -33,7 +33,7 @@ done
############################################### MAIN SCRIPT ########################################################## ############################################### MAIN SCRIPT ##########################################################
main() { main() {
readonly VERSION="v2.9 (January 27th 2021)" readonly VERSION="v2.9 (January 30th 2021)"
readonly PAD=$(((48-${#VERSION})/2)) readonly PAD=$(((48-${#VERSION})/2))
readonly BRAND="$(get_brand)" readonly BRAND="$(get_brand)"
@ -41,14 +41,15 @@ main() {
readonly OLDER_THAN_ANDROID_8=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') < 26 )) readonly OLDER_THAN_ANDROID_8=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') < 26 ))
readonly OLDER_THAN_ANDROID_5=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') < 21 )) readonly OLDER_THAN_ANDROID_5=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') < 21 ))
readonly NEWER_THAN_ANDROID_10=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') >= 29 )) readonly NEWER_THAN_ANDROID_9=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') >= 29 ))
readonly SYSTEM_MOUNT_POINT=$(adb shell getprop ro.build.system_root_image | grep "true" && echo "/system_root" || echo "/system") readonly SYSTEM_MOUNT_POINT=$(adb shell getprop ro.build.system_root_image | grep "true" && echo "/system_root" || echo "/system")
declare -a CUSTOM_LIST=() # Trimed APK/package list from the debloat lists only containing packages/APK on the device (populated by generate_custom_list()) declare -a CUSTOM_LIST=() # Trimed APK/package list only containing packages/APK on the device (populated by generate_custom_list())
declare -a EXTERNAL_LIST=() # APK/package list provided by the user (populated by import_external_list()) declare -a EXTERNAL_LIST=() # APK/package list provided by the user (populated by import_external_list())
declare -a USERS=() # User list (populated by debloat_or_restore()) declare -a USERS=() # User list (populated by debloat_or_restore())
declare -ra LISTS=(us_carriers french_carrier german_carriers "$BRAND" google facebook amazon microsoft \ declare -ra LISTS=(us_carriers french_carrier german_carriers "$BRAND" google facebook amazon microsoft \
qualcomm mediatek misc aosp pending) qualcomm mediatek misc aosp pending EXTERNAL_LIST)
declare -i FORCE_UNINSTALL=0 declare -i FORCE_UNINSTALL=0
declare -i RESTORE=0 declare -i RESTORE=0
@ -111,7 +112,7 @@ main() {
elif [[ $REPLY = 2 || $REPLY = 3 || $REPLY = 4 || $REPLY = 5 ]]; then elif [[ $REPLY = 2 || $REPLY = 3 || $REPLY = 4 || $REPLY = 5 ]]; then
clear -x clear -x
(( !BRAND_SUPPORTED )) && printf "\n${BRED}%s\n" "No $BRAND debloat list found. Feel free to contribute ! :)" (( !BRAND_SUPPORTED )) && printf "\n${BRED}%s${NC}\n" "No $BRAND debloat list found. Feel free to contribute ! :)"
case $REPLY in case $REPLY in
2) { title="RESTORE"; RESTORE=1; ROOT=0; };; 2) { title="RESTORE"; RESTORE=1; ROOT=0; };;
@ -120,23 +121,38 @@ main() {
5) { title="DEBLOAT"; RESTORE=0; ROOT=1; };; 5) { title="DEBLOAT"; RESTORE=0; ROOT=1; };;
esac esac
if (( ROOT && NEWER_THAN_ANDROID_9 )); then
magisk_module
continue
fi
if (( ROOT )); then
printf "\n%s\n" "The script will physically remove the apks from your phone"
printf "%s${BBLUE}%s${NC}%s\n" "Do you prefer to use a " "Magisk " "module for a systemless debloat instead? [y/n]"
read -r
if [[ $REPLY =~ [Yy] ]]; then
magisk_module
continue
fi
fi
((!ROOT)) && printf "\n${BORANGE}%s\n" "=================== $title ===================" ((!ROOT)) && printf "\n${BORANGE}%s\n" "=================== $title ==================="
(( ROOT )) && printf "\n${BORANGE}%s\n" "================ $title (ROOT) ===============" (( ROOT )) && printf "\n${BORANGE}%s\n" "================ $title (ROOT) ==============="
printf "%s\n" "# #" printf "%s\n" "# #"
printf "%-12s${NC}%s${BORANGE}%14s\n" "#" "1 - $title a package" "#" | awk '{print tolower($0)}' printf "%-12s${NC}%s${BORANGE}%14s\n" "#" "1 - $title a package" "#" | awk '{print tolower($0)}'
(( BRAND_SUPPORTED )) && printf "#${NC}%8s 2 - ${BRAND} %$((25-${#BRAND}))s ${BORANGE} #\n" (( BRAND_SUPPORTED )) && printf "#${NC}%7s 2 - ${BRAND} %$((25-${#BRAND}))s ${BORANGE} #\n"
printf "%-12s${NC}%s${BORANGE}%27s\n" "#" "3 - GFAM" "#" printf "%-12s${NC}%s${BORANGE}%27s\n" "#" "3 - GFAM" "#"
printf "%-12s${NC}%s${BORANGE}%23s\n" "#" "4 - Carriers" "#" printf "%-12s${NC}%s${BORANGE}%23s\n" "#" "4 - Carriers" "#"
printf "%-12s${NC}%s${BORANGE}%25s\n" "#" "5 - Others" "#" printf "%-12s${NC}%s${BORANGE}%25s\n" "#" "5 - Others" "#"
printf "%-12s${NC}%s${BORANGE}%27s\n" "#" "6 - AOSP" "#" printf "%-12s${NC}%s${BORANGE}%27s\n" "#" "6 - AOSP" "#"
printf "%-12s${NC}%s${BORANGE}%18s\n" "#" "7 - External list" "#" printf "%-12s${NC}%s${BORANGE}%18s\n" "#" "7 - External list" "#"
(( ROOT && !RESTORE && !NEWER_THAN_ANDROID_10 )) && printf "#${NC}%7s 8 - Create a flashable zip${BORANGE}%7s #\n" (( ROOT && !RESTORE )) && printf "#${NC}%7s 8 - Create a flashable zip${BORANGE}%7s #\n"
printf "%s\n" "# #" printf "%s\n" "# #"
printf "%-12s${NC}%s${BORANGE}%15s\n" "#" "0 - Pending list /!\\" "#" printf "%-12s${NC}%s${BORANGE}%15s\n" "#" "0 - Pending list /!\\" "#"
printf "%s\n" "# #" printf "%s\n" "# #"
printf "%s\n${NC}\n" "=================================================" printf "%s\n${NC}\n" "================================================="
read -r -p "Your selection (e.g: 2 3 4 5): " read -r -p "Your selection (e.g: 2 3 4 5): "
if [[ "$REPLY" =~ 7 ]]; then import_external_list && debloat_or_restore EXTERNAL_LIST; fi if [[ "$REPLY" =~ 7 ]]; then import_external_list && debloat_or_restore EXTERNAL_LIST; fi
if [[ "$REPLY" =~ 4 ]]; then lists_selection us_carriers french_carriers german_carriers; fi if [[ "$REPLY" =~ 4 ]]; then lists_selection us_carriers french_carriers german_carriers; fi
@ -165,13 +181,17 @@ import_external_list() {
done done
for i in "${EXTERNAL_LIST[@]}"; do for i in "${EXTERNAL_LIST[@]}"; do
if [[ ROOT -eq 1 && $i =~ ^/.*\.apk$ ]]; then if [[ $ROOT -eq 1 && $RESTORE -eq 0 && $i =~ ^/.*\.apk$ ]]; then
printf "\n${BRED}%s${NC}\n\n" "Please use a package list, not an APK list" printf "\n${BRED}%s${NC}\n\n" "Please use a package list, not an APK list"
exit 1 exit 1
elif [[ ROOT -eq 0 && ! $i =~ ^[a-z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)+[0-9a-zA-Z_]$ ]]; then elif [[ $ROOT -eq 0 && ! $i =~ ^[a-z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)+[0-9a-zA-Z_]$ ]]; then
printf "\n${BRED}%s${NC}%s${BRED}%s${NC}%s\n\n" "$i" " is not a package (format is" " com.android.bips" ")" printf "\n${BRED}%s${NC}%s${BRED}%s${NC}%s\n\n" "$i" " is not a package (format is" " com.android.bips" ")"
exit 1 exit 1
elif [[ $ROOT -eq 1 && $RESTORE -eq 1 && ! $i =~ ^/.*\.apk$ ]]; then
printf "\n${BRED}%s${NC}\n\n" "Please use an APK list"
exit 1
fi fi
done done
} }
@ -186,6 +206,7 @@ associate_path_with_package() {
} }
generate_custom_list() { generate_custom_list() {
clear -x
local -n list="$1" local -n list="$1"
[[ $# -eq 2 ]] && local user_id="$2" [[ $# -eq 2 ]] && local user_id="$2"
CUSTOM_LIST=() CUSTOM_LIST=()
@ -198,12 +219,25 @@ generate_custom_list() {
readarray -t TEMP < <(comm -12 <(for p in "${list[@]}"; do echo "${p}"; done|sort -i) \ readarray -t TEMP < <(comm -12 <(for p in "${list[@]}"; do echo "${p}"; done|sort -i) \
<(for p in "${!PACKAGE_AND_PATH[@]}"; do echo "$p"; done|sort -i)) <(for p in "${!PACKAGE_AND_PATH[@]}"; do echo "$p"; done|sort -i))
for p in "${TEMP[@]}"; do [[ RESTORE -ne 1 ]] && printf "\n${BGREEN}%s${NC}\n\n" "Clearing app data..."
for p in "${TEMP[@]}"; do
[[ RESTORE -ne 1 ]] && printf "${BBLUE}%s${NC}" "$p --> " && adb shell "pm clear $p"
CUSTOM_LIST+=("${PACKAGE_AND_PATH[$p]}") CUSTOM_LIST+=("${PACKAGE_AND_PATH[$p]}")
done done
[[ RESTORE -eq 1 ]] && readarray -t CUSTOM_LIST < <(comm -12 <(for p in "${CUSTOM_LIST[@]}"; do echo "${p}"; done|sort -i) \ if [[ $RESTORE -eq 1 ]]; then
<(sort -i deleted_apks.txt))
if [[ ${#CUSTOM_LIST[@]} -eq 0 ]]; then
readarray -t CUSTOM_LIST <(sort -i deleted_apks.txt)
else
readarray -t CUSTOM_LIST < <(comm -12 <(for p in "${CUSTOM_LIST[@]}"; do echo "${p}"; done|sort -i) \
<(sort -i deleted_apks.txt))
fi
fi
else else
if (( RESTORE )); then if (( RESTORE )); then
readarray -t CUSTOM_LIST < <(comm -12 <(for p in "${list[@]}"; do echo "${p}"; done|sort -i) \ readarray -t CUSTOM_LIST < <(comm -12 <(for p in "${list[@]}"; do echo "${p}"; done|sort -i) \
@ -239,7 +273,7 @@ debloat_or_restore() {
printf "\n${BORANGE}%s${NC}\n" "==== $list debloat list ====" printf "\n${BORANGE}%s${NC}\n" "==== $list debloat list ===="
if (( ROOT )); then if (( ROOT )); then
touch deleted_apks.txt touch -a deleted_apks.txt
generate_custom_list "$list" generate_custom_list "$list"
if (( RESTORE )); then restore_apks "$list"; else root_debloat "$list"; fi if (( RESTORE )); then restore_apks "$list"; else root_debloat "$list"; fi
return return
@ -261,7 +295,7 @@ debloat_or_restore() {
else else
if (( ROOT )); then if (( ROOT )); then
touch deleted_apks.txt touch -a deleted_apks.txt
if (( RESTORE )); then restore_apks; else root_debloat; fi if (( RESTORE )); then restore_apks; else root_debloat; fi
return return
fi fi
@ -315,6 +349,12 @@ create_flashable_zip() {
printf "\n${BRED}%s${NC} " "Your choice (e.g 1 2 3) :" printf "\n${BRED}%s${NC} " "Your choice (e.g 1 2 3) :"
read -r -a selection read -r -a selection
for i in "${selection[@]}"; do
if [[ "${LISTS[$i]}" == EXTERNAL_LIST ]]; then
import_external_list
fi
done
mkdir -p META-INF/com/google/android/ mkdir -p META-INF/com/google/android/
echo "#!/sbin/sh" > "$UPDATE_BINARY" echo "#!/sbin/sh" > "$UPDATE_BINARY"
@ -325,11 +365,14 @@ create_flashable_zip() {
for list in "${selection[@]}"; do for list in "${selection[@]}"; do
if (( list > ${#LISTS[@]} )) || (( list < 1 )); then continue; fi if (( list > ${#LISTS[@]} )) || (( list < 1 )); then continue; fi
generate_custom_list "${LISTS[$list]}" generate_custom_list "${LISTS[$list]}"
backup_apks CUSTOM_LIST backup_apks CUSTOM_LIST
for package in "${CUSTOM_LIST[@]}"; do for package in "${CUSTOM_LIST[@]}"; do
echo "rm -rf $package" >> "$UPDATE_BINARY" echo "rm -rf $package" >> "$UPDATE_BINARY"
done done
[[ $is_empty -eq 1 && ${#CUSTOM_LIST[@]} -gt 0 ]] && is_empty=0 [[ $is_empty -eq 1 && ${#CUSTOM_LIST[@]} -gt 0 ]] && is_empty=0
done done
@ -339,7 +382,7 @@ create_flashable_zip() {
else else
echo "umount $SYSTEM_MOUNT_POINT" >> "$UPDATE_BINARY" echo "umount $SYSTEM_MOUNT_POINT" >> "$UPDATE_BINARY"
echo "echo 'ui_print ----- ALL DONE -----' > /proc/self/fd/\$2" >> "$UPDATE_BINARY" echo "echo 'ui_print ----- ALL DONE -----' > /proc/self/fd/\$2" >> "$UPDATE_BINARY"
zip -rm flashable_zip_UAD_$VERSION.zip META-INF/ 1>/dev/null zip -rm flashable_zip_UAD_${VERSION%%[[:space:]]*}.zip META-INF/ 1>/dev/null
printf "\n${BGREEN}%s${NC}%s\n" "flashable_zip_UAD_$VERSION.zip" " has been generated." printf "\n${BGREEN}%s${NC}%s\n" "flashable_zip_UAD_$VERSION.zip" " has been generated."
fi fi
@ -366,7 +409,10 @@ restore_apks() {
CUSTOM_LIST=("$path") CUSTOM_LIST=("$path")
fi fi
[[ NEWER_THAN_ANDROID_10 -eq 1 ]] && magisk_module && return if [[ $NEWER_THAN_ANDROID_9 -eq 1 ]]; then
magisk_module
return
fi
adb shell "su -c \"mount -o rw,remount $SYSTEM_MOUNT_POINT\"" adb shell "su -c \"mount -o rw,remount $SYSTEM_MOUNT_POINT\""
@ -402,12 +448,13 @@ root_debloat() {
fi fi
backup_apks CUSTOM_LIST backup_apks CUSTOM_LIST
[[ NEWER_THAN_ANDROID_10 -eq 1 ]] && method=systemlessly || method=physically read -r -p "Type YES if you want to physically delete the apk(s): "
read -r -p "Type YES if you want to $method delete the apks: "
if [[ $REPLY = "YES" ]]; then if [[ $REPLY = "YES" ]]; then
[[ NEWER_THAN_ANDROID_10 -eq 1 ]] && magisk_module && return if [[ $NEWER_THAN_ANDROID_9 -eq 1 ]]; then
magisk_module 1
return
fi
local commandes="mount -o rw,remount $SYSTEM_MOUNT_POINT;" local commandes="mount -o rw,remount $SYSTEM_MOUNT_POINT;"
for apk in "${CUSTOM_LIST[@]}"; do for apk in "${CUSTOM_LIST[@]}"; do
@ -425,9 +472,42 @@ root_debloat() {
} }
magisk_module() { magisk_module() {
declare -i new=1 # apk is not in customize.sh clear -x
declare -a selection=()
declare -i is_empty=1
mkdir MAGISK_MODULE && cd "$_" echo
if (( !RESTORE )); then
for i in "${!LISTS[@]}"; do
printf "%-2s%s\n" "$i" " - ${LISTS[$i]}"
done
printf "\n${BRED}%s${NC} " "Your choice (e.g 1 2 3) :"
read -r -a selection
for i in "${selection[@]}"; do
if [[ "${LISTS[$i]}" == EXTERNAL_LIST ]]; then
import_external_list
fi
done
else
printf "%-2s%s\n" "1" " - deleted_apks.txt"
printf "%-2s%s\n" "2" " - EXTERNAL_LIST"
printf "\n${BRED}%s${NC} " "Your choice :"
read -r
selection=(1)
case $REPLY in
1) readarray -t CUSTOM_LIST < deleted_apks.txt ;;
2) { import_external_list && CUSTOM_LIST=("${EXTERNAL_LIST[@]}"); } ;;
*) exit 1 ;;
esac
fi
touch -a deleted_apks.txt
rm -r MAGISK_MODULE || true
mkdir MAGISK_MODULE
cd "$_"
local -r MODULE_DIR=$(pwd) local -r MODULE_DIR=$(pwd)
cat << EOF > module.prop cat << EOF > module.prop
@ -439,46 +519,60 @@ author=w1nst0n
description=custom root debloat script description=custom root debloat script
EOF EOF
if [[ -f ../uad_magisk_$VERSION.zip ]] mkdir -p META-INF/com/google/android/ && cd "$_"
then wget -q https://raw.githubusercontent.com/topjohnwu/Magisk/master/scripts/module_installer.sh
unzip -qn "../uad_magisk_$VERSION.zip" && rm "$_" mv module_installer.sh update-binary
truncate -s-1 customize.sh echo "#MAGISK" > updater-script
else
mkdir -p META-INF/com/google/android/ && cd "$_" cd "$MODULE_DIR"
wget -q https://raw.githubusercontent.com/topjohnwu/Magisk/master/scripts/module_installer.sh [[ RESTORE -eq 0 ]] && echo "REPLACE=\"" > customize.sh
mv module_installer.sh update-binary
echo "#MAGISK" > updater-script for list in "${selection[@]}"; do
cd "$MODULE_DIR" && echo "REPLACE=\"" > customize.sh
if (( list > ${#LISTS[@]} )) || (( list < 1 )); then continue; fi
cd ..
if (( !RESTORE )); then
generate_custom_list "${LISTS[$list]}"
backup_apks CUSTOM_LIST
fi
cd "$MODULE_DIR"
for apk in "${CUSTOM_LIST[@]}"; do
if (( RESTORE )); then
local dir="$(dirname "${apk:1}")"
mkdir -p "$dir"
cp "../apks_backup/$(echo "$apk" | sed -r 's/.*\///g')" "${apk:1}"
grep -Fqv "$apk" ../deleted_apks.txt > temp.tmp || touch temp.tmp; mv temp.tmp ../deleted_apks.txt
else
grep -qxF "$(dirname "$apk")" customize.sh || dirname "$apk" >> customize.sh
grep -qxF "$apk" ../deleted_apks.txt || echo "$apk" >> ../deleted_apks.txt
fi
done
[[ $is_empty -eq 1 && ${#CUSTOM_LIST[@]} -gt 0 ]] && is_empty=0
done
if [[ RESTORE -eq 0 && $is_empty -eq 1 ]]; then
printf "\n${BRED}%s${NC}\n" "Nothing to debloat!"
cd .. && rm -rf MAGISK_MODULE
return 0
fi fi
for apk in "${CUSTOM_LIST[@]}"; do [[ RESTORE -ne 1 ]] && echo "\"" >> customize.sh
zip -FSrm ../uad_magisk_"${VERSION%%[[:space:]]*}_$title".zip ./* 1>/dev/null || true
if ((RESTORE)); then ###### TODO #########
local -r dir="$(dirname "${apk:1}")"
cp "backup_apks/$(echo "$apk" | sed -r 's/.*\///g')" "${apk:1}"
adb push "apks_backup/$apk" /sdcard && adb shell "su -c 'mkdir -p \"$dir\" && mv \"/sdcard/$apk\" \"$dir\"'"
grep -Fqv "$p" deleted_apks.txt > temp.tmp || touch temp.tmp; mv temp.tmp deleted_apks.txt
else
if grep -qxF "$(dirname "$apk")" customize.sh; then
new=0
echo "$apk" >> "deleted_apks.txt"
dirname "$apk" >> customize.sh
fi
fi
done
[[ new -eq 1 && RESTORE -ne 1 ]] && echo "\"" >> customize.sh
zip -rm ../uad_magisk_"$VERSION".zip META-INF/ customize.sh module.prop 1>/dev/null || true
cd .. && rm -rf MAGISK_MODULE cd .. && rm -rf MAGISK_MODULE
printf "\n${BGREEN}%s${NC}%s\n" "uad_magisk_$VERSION.zip" " has been generated." printf "\n${BGREEN}%s${NC}%s\n" "uad_magisk_${VERSION%%[[:space:]]*}.zip" " has been generated."
printf "\n%s${BGREEN}%s${NC}%s\n" "Flash this file from" " Magisk Manager" " and reboot your phone." printf "\n%s${BBLUE}%s${NC}%s${BBLUE}%s${NC}%s\n" "Flash this file from " "Magisk Manager " "or " "TWRP " "and reboot your phone."
printf "\n${BRED}%s${NC}\n\n" "/!\ Do not flash this from your recovery /!\\" printf "\n%s${BBLUE}%s${NC}%s\n" "Removing the module from " "Magisk Manager " "will restore the apks."
printf "\e[5m%s\033[0m" "Press any key to continue" if [[ $BRAND == "samsung" ]]; then
printf "\n${BRED}%s${NC}\n\n" "/!\ TWRP doesn't support Samsung encryption. You won't be able to flash this Magisk module from TWRP /!\\"
fi
printf "\e[5m%s\033[0m" "Press any key to continue"
read -n 1 -r -s read -n 1 -r -s
} }
@ -616,6 +710,17 @@ uad_recovery_mode() {
printf "\n%s${BRED}%s${NC}%s\n\n" "Your apks has been backed-up in" " apks_backup/" " (in case you messed up)" printf "\n%s${BRED}%s${NC}%s\n\n" "Your apks has been backed-up in" " apks_backup/" " (in case you messed up)"
fi fi
[[ $NEWER_THAN_ANDROID_9 -eq 1 ]] && magisk_module && continue
printf "\n%s\n" "The script will physically remove the apks from your phone."
printf "%s${BBLUE}%s${NC}%s\n" "Do you prefer to use a " "Magisk " "module for a systemless debloat instead? [y/n]"
read -r
if [[ $REPLY =~ [Yy] ]]; then
magisk_module
continue
fi
for p in "${CUSTOM_LIST[@]}"; do # $p = path/to/app.apk for p in "${CUSTOM_LIST[@]}"; do # $p = path/to/app.apk
local apk=$(echo "$p" | sed -r 's/.*\///g') # app.apk local apk=$(echo "$p" | sed -r 's/.*\///g') # app.apk
local dir=$(dirname "$p") # path/to local dir=$(dirname "$p") # path/to