I have written a previous article that talks about how to speed up the execution of the same type of field-filling operations using kotlin script (kts). Despite all its advantages, autofill can be accelerated even more with the help of a lower-level bash language and the use of hotkeys.
We will use the same example as in the last article.
You can download the full code here:
Let me remind you that to switch to MainActivity, the fields must be filled in, and their values must meet certain requirements:
email: [email protected]
phone: +1 650 123 4567
password: 123456
Here is a video of how it looks if the fields are filled in manually:
Also in the last article, we found all the necessary adb commands to fill in all the text fields and click on the button. Here is the full list:
adb shell input tap 72 1092
adb shell input text '[email protected]'
adb shell input tap 72 1227
adb shell input text '+1 650 123 4567'
adb shell input tap 72 1362
adb shell input text 123456
adb shell input tap 72 1497
Let's try to fill in one line first. To do this, we will use a bunch of tools External Tools + Adb + bash.
Let's launch LoginActivity and tap on the field using the terminal:
adb shell input tap 72 1092
Everything should work.
Next, we go: Android Studio > Preferences > Tools > External Tools.
This is a tool that allows you to run commands and external scripts or programs inside Android Studio. You can also pass arguments there.
However, there are a number of limitations compared to the terminal. For example, you cannot pass multiple commands.
You can read more here.
Let’s get into it.
Click on the Add (“+”) icon. Let’s enter the following values:
Name: my_adb
Program: /Users/{your name}/Library/Android/sdk/platform-tools/adb
Arguments: adb shell input tap 72 1092 (insert the entire line)
Working directory: $ProjectFileDir$ (usually it is added by itself)
Let’s save:
Now go to: Tools > External Tools > my_adb \
If everything is done correctly, the terminal window should start and the command will be executed.
It is striking that the adb command was executed much faster than kotlin script.
We managed to execute one adb command using External Tools. However, the reality is that more often than not it is necessary to enter several consecutive commands.
Let's try to achieve this.
Let's go back to the command editing window. You can see that you cannot enter more than one command there:
This is also indicated by the official support of Jet Brains
But, let me remind you that in order to achieve the result, we need to enter a list of commands:
adb shell input tap 72 1092
adb shell input text '[email protected]'
adb shell input tap 72 1227
adb shell input text '+1 650 123 4567'
adb shell input tap 72 1362
adb shell input text 123456
adb shell input tap 72 1497
To achieve this there is a workaround: create a bash script, and insert into it these commands. And through External Tools to run this script. That's what we'll do:
Insert the path into the terminal and try to execute through it:
If everything is done correctly, the command should be executed, and the fields on the device should be filled in.
Next, go to Android Studio > Preferences > Tools > External Tools.
Creating new command:
Name: my_bash
Program: /Users/{your name}/Downloads/my_bash.bash (file path)
Save
Run Tools > External Tools > my_bash.
If everything is done correctly, the command should be executed, and the fields on the device should be filled in.
In the video it looks something like this:
It can be seen that the fields are filled in much times faster than with manual input.
As in the case of the kotlin script, if you need time from the moment you enter one command to another, you can set the sleep delay (sec).
adb shell input tap 72 1092
sleep 1
adb shell input text '[email protected]'
There is another optimization that can be done — to set hotkeys for a specific command.
To do this, go to Android Studio > Preferences > Keymap > External Tools > External Tools.
Next, find the command and set hotkeys, for example, in my case Control+Option+Command+B.
Save it.
Try to execute this and make sure that everything works. This is the second advantage compared to kotlin script — you can set hotkeys to execute a command.
Small optimizations of frequently repeated operations in the development process lead to long-term savings in time and resources. In the example discussed in