Run ADB Commands in Java Code for Device Manipulation

Android Debug Bridge (adb) is a command-line tool. It lets you interact with an Android device. The adb commands facilitate a variety of device actions, such as installing and debugging apps, changing battery level, turning on/off the wifi etc. It has three components:

  • A client, which sends commands. The client runs on your development machine. You can invoke a client from a command-line terminal by issuing an adb command.
  • A daemon (adbd), which runs commands on a device. The daemon runs as a background process on each device.
  • A server, which manages communication between the client and the daemon. The server runs as a background process on your development machine.

Today I’ll explain how to manipulate your mobile device’s battery level. As you all know, mobile testing is trivial. There are many components that change the state of the device. Those components are the battery, network and many more.

What happens after the battery is below %15? When your phone gets into power saver mode, network turns itself to off state to save your battery power. After you start using the app, IP address change. If there’s an IP restriction on your app, your session will be terminated. That’s a business rule in finance app’s in Turkey.

Generally, those situations are tested manually. So how can we implement those situations in our mobile automation testing process?

Prerequisite

You need to install Android SDK Platform-Tools package. Here is the website to install it.

https://developer.android.com/studio/releases/platform-tools

Battery Commands

dumpsys battery set level %s – It let you change the level from 0 to 100

dumpsys battery set status %s – It let you change the level to Unknown, Charging, Discharging, Not Charging or Full.

dumpsys battery reset – It let you reset the battery change made through adb.

dumpsys battery set usb %s – It let you change the status of USB connection. It can be ON or OFF.

Using those command via command line

Some command requires device Id, some does not. In order to get the unique device id, you should run “adb devices” command in command line.

Then you should run your command with below syntax.

adb -s DEVICE_ID shell COMMAND

or

adb shell COMMAND

So, if you want to change battery level to 5, you should run

adb -s DEVICE_ID shell dumpsys battery set level 5

Here’s the project that we just started.

A sample function from our BatteryCommand class

public class BatteryCommand extends Command {

    private static final String batteryLevelCommand ="dumpsys battery set level %s";
    private static final String batteryStatusCommand ="dumpsys battery set status %s";
    private static final String batteryResetCommand ="dumpsys battery reset";
    private static final String batteryUSBCommand ="dumpsys battery set usb %s";

    public void setBatteryLevel(String level) throws IOException {
        ProcessExecutor.exec(getDeviceId(),String.format(batteryLevelCommand,level));
        System.out.print("Battery leve is set to "+level);
    }
}

Process Executor class to execute ADB commands.

 public static void exec(String deviceId,String command) throws IOException {
        Process process = null;
        String commandString;
        if(deviceId != null) {
            commandString = String.format("%s", "adb -s " + deviceId + " shell " + command);
        }else
            commandString = String.format("%s", "adb shell " + command);
        System.out.print("Command is "+commandString+"\n");
        try {
            process = ProcessHelper.runTimeExec(commandString);
        } catch (IOException e) {
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.print(line+"\n");
        }
    }

Sample test class to execute our commands.

@Test
public void testBattery() throws IOException, InterruptedException {
        BatteryCommand command = new BatteryCommand();
        command.setDeviceId(device.getUniqueDeviceID());
        command.setBatteryLevel("5");
        command.setBatteryStatus(BatteryLevelEnum.NotCharging);
        command.setUSB(SwitchEnum.OFF);
        command.resetBattery();
}

Here’s the result of the execution

GitHub Project 

https://github.com/swtestacademy/adb-device-manipulation

Thanks.
Canberk Akduygu

9 thoughts on “Run ADB Commands in Java Code for Device Manipulation”

  1. public class BatteryCommand extends Command

    What should be mentioned Command base class? By importing import org.openqa.selenium.remote.Command;
    Its asking to add constructor
    public BatteryCommands(SessionId sessionId, String name) {
    super(sessionId, name);
    // TODO Auto-generated constructor stub
    }

    Could you please post complete end to end code ?

    Reply
  2. In exec function. i have this functionality. Create an InputStreamReader using Process class.
    Just take a look this code.

    Reply
  3. Hi, How are you?
    Can you please help run this project? I made and build this project but it is giving me some errors like I could not find ProcessHelper class. I think this code is not complete. Please also tell me in which IDE I have to build this project I mean to say in Android Studio or IntelliJ IDEA?

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.