Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Jiab77/37d818c9b3cd75422936fd45ad3793ee to your computer and use it in GitHub Desktop.

Select an option

Save Jiab77/37d818c9b3cd75422936fd45ad3793ee to your computer and use it in GitHub Desktop.
Running ADB on and from your own Android device -- Part 2

Running ADB on and from your own Android device -- Part 2

I guess if you are reading this, it's because you liked the first part of this gist 😁

I hope this second part will please you even more!

Note

Google have decided to force all developpers to register to a central authority. This will endanger projects and services like F-droid and so by extention IzzyOnDroid repo and the Droid-ify project.

You can read more about it here: https://f-droid.org/2025/09/29/google-developer-registration-decree.html

Content

Extending Termux

Termux in itself is already quite useful but you can even extend it even more with several extensions!

  • Termux:API: Gives access to notifications, device gps, microphone, camera and so much more
  • Termux:Boot: Gives the possibility to run a script at boot
  • Termux:Widget: Gives the possibility to run a script from the homescreen

These three plugins will be really useful in the next sections. πŸ˜‰

Note

There are a few other Termux extensions that exists like:

  • Termux:Float: Gives the possibility to run Termux in a floating window
  • Termux:GUI: Gives the possibility to use the Android GUI from terminal applications
  • Termux:Styling: Gives the possibility to customize the Termux terminal
  • Termux:Tasker: Gives the possibility to run Termux scripts from Tasker

But they won't be covered here.

In case you want them, you can find them all in Droidi-fy.

Installing Termux:API

Install Termux:API from Droidify or F-Droid and do the following:

  1. Start the Termux application
  2. Run this command: apt update && apt install -y termux-api

Important

You need to have both, the Termux:API application and the termux-api package installed.

The Termux:API application provides the backend required by the commands provided by the termux-api package.

Tip

If you want to see all commands provided by the termux-api package, then type termux- and hit [TAB] twice.

Note

Some commands required by the script for starting Shizuku at boot comes from the termux-api package.

Installing Termux:Boot

Install Termux:API from Droidify or F-Droid and do the following:

  1. Start the Termux application
  2. Run this command: mkdir -pv ~/.termux/boot

Now you can place any scripts that you want to be executed at boot in the ~/.termux/boot folder.

Warning

If there are multiple files, they will be executed in a sorted order.

So if you want to control the execution order of your scripts, you should prefix them with digits like this:

  • 01-first-script.sh
  • 02-second-script.sh
  • 03-third-script

And so on...

Important

The first line of your script must be replaced by:

#!/data/data/com.termux/files/usr/bin/bash for Bash

And by:

#!/data/data/com.termux/files/usr/bin/sh for Sh

IF YOU DON'T DO THAT, YOUR SCRIPTS WILL NOT BE EXECUTED!!

Installing Termux:Widget

Install Termux:Widget from Droidify or F-Droid and do the following:

  1. Start the Termux application
  2. Run this command: mkdir -pv ~/.shortcuts

Now you can place any scripts that you want to be executed from the homescreen in the ~/.shortcuts folder.

Important

The first line of your script must be replaced by:

#!/data/data/com.termux/files/usr/bin/bash for Bash

And by:

#!/data/data/com.termux/files/usr/bin/sh for Sh

IF YOU DON'T DO THAT, YOUR SCRIPTS WILL NOT BE EXECUTED!!

Run priviledged apps (without root)

Ok, now that you (normally) have Termux installed with the three plugins as explained in the previous section, you can keep reading to finally be able to run priviledged apps or commands without having to root your device. 😈

Installing Shizuku

The first thing you have to do in order to be able to run privileged apps or commands is to install Shizuku.

Shizuku in Droidi-fy

Tip

You can find the source code here: https://github.com/RikkaApps/Shizuku

You can also read their API which is pretty interesting.

Note

The recommanded install method is to use Droidi-fy.

Start Shizuku at boot

Unfortunately, you can't start Shizuku automatically at boot without rooting your Android device.

By chance, I've found a way to do it by using a shell script that can be started at boot with the Termux:Boot plugin.

You can use the code below and modify it according to your needs.

#!/data/data/com.termux/files/usr/bin/bash

# Options
[[ -r $HOME/.debug ]] && set -x

# Functions
wait_for_init() {
  termux-wake-lock
  sleep 30s
}
wait_for_end() {
  sleep 10s
  termux-wake-unlock
}
notify() {
  termux-notification -t "Termux ADB" -c "$1" --icon "$2" --id 1337
}
notify_remove() {
  termux-notification-remove 1337
}
toast() {
  termux-toast "$*"
}
adb_server_reset() {
  adb kill-server && adb start-server
}
adb_list_devices() {
  adb devices -l
}
get_adb_port() {
  nmap -v0 127.0.0.1 -T5 -p30000-65535 -oX - | grep -m1 'state="open"' | grep -m1 "<port " | cut -d'"' -f4
}

# Main
notify "Initializing... Please enable Wireless Debugging." "warning"
wait_for_init
notify "Starting ADB..." "info"
toast "Restarting ADB server..."
adb_server_reset &>/dev/null
toast "Detecting ADB connection port..."
FOUND_ADB_PORT=$(get_adb_port)
if [[ -n $FOUND_ADB_PORT ]]; then
  toast "Found port: ${FOUND_ADB_PORT}"
  toast "Connecting to ADB..."
  adb connect "localhost:${FOUND_ADB_PORT}" | termux-toast
  toast "Searching for connected devices..."
  adb_list_devices | termux-toast
  notify "Starting Shizuku..." "lightbulb"
  adb shell /data/app/~~GBpxmYQKGPC_qX6vSCt0eA==/moe.shizuku.privileged.api-ht7T9IX8AmvW1_6jo5sCeA==/lib/arm64/libshizuku.so
  RET_CODE_SHIZUKU=$?
  if [[ $RET_CODE_SHIZUKU == 0 ]]; then
    notify "Shizuku started." "verified"
  else
    notify "Failed to start Shizuku." "error"
  fi
else
  notify "Wireless debugging not started." "report_problem"
fi
wait_for_end
notify_remove

Save the code above in ~/.termux/boot/01-start-shizuku.sh and make it executable.

Warning

The adb shell command responsible for starting Shizuku might be different on your side. If so, please write a comment and I'll make this part configurable with a variable.

Tip

You can name your script with or without the .sh extension. It does not matter as long as your file has the executable bit set.

Start Shizuku from homescreen

In case your Android device is already started and want to manually start Shizuku, you can use the script below.

#!/data/data/com.termux/files/usr/bin/bash

# Options
[[ -r $HOME/.debug ]] && set -x

# Functions
adb_server_reset() {
  adb kill-server && adb start-server
}
adb_list_devices() {
  adb devices -l
}
get_adb_port() {
  nmap -v0 127.0.0.1 -T5 -p30000-65535 -oX - | grep -m1 'state="open"' | grep -m1 "<port " | cut -d'"' -f4
}

# Main
echo -e "\nInitializing...\n"
termux-wake-lock
echo -e "\nRestarting ADB server...\n"
adb_server_reset
echo -e "\nDisplaying connected devices...\n"
adb_list_devices
echo -e "\nDetecting ADB connection port...\n"
FOUND_ADB_PORT=$(get_adb_port)
if [[ -n $FOUND_ADB_PORT ]]; then
  echo " - Found port: ${FOUND_ADB_PORT}"
  echo -e "\nConnecting to ADB...\n"
  adb connect "localhost:${FOUND_ADB_PORT}"
  echo -e "\nDisplaying connected devices...\n"
  adb_list_devices
  echo -e "\nStarting Shizuku...\n"
  adb shell /data/app/~~GBpxmYQKGPC_qX6vSCt0eA==/moe.shizuku.privileged.api-ht7T9IX8AmvW1_6jo5sCeA==/lib/arm64/libshizuku.so
  RET_CODE_SHIZUKU=$?
  if [[ $RET_CODE_SHIZUKU == 0 ]]; then
    echo -e "\nShizuku started.\n"
  else
    echo -e "\nFailed to start Shizuku.\n"
  fi
else
  echo -e "\nWireless debugging not started.\n"
fi
read -rp "Press [Enter] to exit..."
echo -e "\nClosing...\n"
termux-wake-unlock

Save the code above in ~/.shortcuts/start-shizuku and make it executable.

Warning

The adb shell command responsible for starting Shizuku might be different on your side. If so, please write a comment and I'll make this part configurable with a variable.

Note

The script linked to the homescreen widget does not have the .sh extension.

This is because the widget is created based on the file name and I found it ugly to see the file extension.

Remove unwanted applications

Now that you (normally) have Shizuku installed, you can now install Canta to remove any applications that are preinstalled on your Android device that usually can't be removed without root.

Canta in Droidi-fy

Tip

You can find the source code here: https://github.com/samolego/Canta

Note

The recommanded install method is to use Droidi-fy.

Important

Once you have installed Canta, you must authorize it in Shizuku. You should be prompted to do when starting the application.

Caution

Be very careful when removing system applications as you might simply brick (break) your Android device!

Hide any applications and files

Now that you (normally) have Shizuku installed, you can now install Amarok.

Amarok in Droidi-fy

Tip

You can find the source code here: https://github.com/deltazefiro/Amarok-Hider

Note

The recommanded install method is to use Droidi-fy.

Important

Once you have installed Amarok, you must authorize it in Shizuku. You should be prompted to do when starting the application.

Kill any running process

Now that you (normally) have Shizuku installed, you can now install TaskManager in case you want to kill some annoying process or simply want to quickly get more details about it.

TaskManager in Droidi-fy

Tip

You can find the source code here: https://github.com/RohitKushvaha01/TaskManager

Note

The recommanded install method is to use Droidi-fy.

Important

Once you have installed TaskManager, you must authorize it in Shizuku. You should be prompted to do when starting the application.

Run elevated commands in Termux

Thanks to the Shizuku dev who created RISH! πŸ™‡β€β™‚οΈ

rish is an Android program for interacting with a shell that runs on a high-privileged daemon process.

To deploy it, just open Shizuku and do the following:

Step 1 Setup Shizuku for Termux
Step 2 Export Shizuku Rish files
Step 3 Add Termux App ID to Rish

And make the rish file executable if it's not already.

Once done, you can do some tests like below:

Running elevated commands in Termux with Shizuku Rish

If you want to have rish in your Termux path, then simply copy both files rish and rish_shizuku.dex in the $PREFIX/bin folder that way:

cp -v rish* $PREFIX/bin/

Note

You can also run an elevated shell by simply running rish without arguments.

It's simple as that 😁

Now, the rest of the fun is up to you 😜

References

Thanks

I'd like to thank the following persons:

  • K3RN3L P4N1K for his help with Termux:Widget
  • Skyper from the THC group for his help and support in general

Thank you guys!

Author

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