Autofill Fields with Adb and External Tools

Written by leonidivankin | Published 2023/02/17
Tech Story Tags: android-app-development | adb | android-dev | kotlin-script | bash-script | android-studio | mobile-app-development | app-development

TLDRAutofill 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] that talks about how to speed up the execution of the same type of field-filling operations using **kotlin script (kts)via the TL;DR App

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.

Choosing an example

We will use the same example as in the last article.

You can download the full code here:

https://gist.github.com/d8b96ef6d97927e811cfa292071d824a

https://gist.github.com/6616b63942b3ec90ccf1fb4ad0329953

https://gist.github.com/2c1a7e43b0b2d38c21af75d139e6ac4d

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:

https://youtu.be/oX3NIZMYpyo

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

Configuring External Tools for one command

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.

Configuring External Tools for multiple consecutive commands

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:

  1. Open the text editor
  2. Insert the lines there
  3. Save as my_bash.bash
  4. Run the command chmod +x /Users/{your name}/Downloads/my_bash.bash

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.

  1. Next, go to Android Studio > Preferences > Tools > External Tools.

  2. Creating new command:

Name: my_bash

Program: /Users/{your name}/Downloads/my_bash.bash (file path)

  1. Save

  2. 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:

https://youtube.com/shorts/R484DB_RScc?feature=share

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.

Conclusion

Small optimizations of frequently repeated operations in the development process lead to long-term savings in time and resources. In the example discussed in this article, we saved time by automatically filling in authorization fields using kotlin script, winning several tens of seconds. In the current example, we saved a few more seconds by using a lower-level language and using hotkeys. In a year, the seconds saved will turn into hours.


Written by leonidivankin | I'm an android developer
Published by HackerNoon on 2023/02/17