Adb command in java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

License

vidstige/jadb

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Читайте также:  Ubuntu nginx php не работает

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

ADB client implemented in pure Java.

The Android Debug Bridge (ADB) is a client-server architecture used to communicate with Android devices (install APKs, debug apps, etc).

The Android SDK Tools are available for the major platforms (Mac, Windows & Linux) and include the adb command line tool which implements the ADB protocol.

This projects aims at providing an up to date implementation of the ADB protocol.

Usage cannot be simpler. Just create a JadbConnection and off you go.

JadbConnection jadb = new JadbConnection(); ListJadbDevice> devices = jadb.getDevices();

Make sure the adb server is running. You can start it by running adb once from the command line.

It’s very easy to send and receive files from your android device, for example as below.

JadbDevice device = . device.pull(new RemoteFile("/path/to/file.txt"), new File("file.txt"));

Some high level operations such as installing and uninstalling packages are also available.

JadbDevice device = . new PackageManager(device).install(new File("/path/to/my.apk"));

An overview of the protocol can be found here: Overview

A list of the available commands that a ADB Server may accept can be found here: Services

The description for the protocol for transferring files can be found here: SYNC.TXT.

Using JADB in your application

Since version v1.1 Jadb support maven as a build system. Although this project is not presented in official apache maven repositories this library can be used as dependencies in your maven/gradle project with the help of jitpack. Jitpack is a system which parses github public repositories and make artifacts from them. You only need to add jitpack as a repository to let maven/gradle to search for artifacts in it, like so

After that you will need to add actual dependency. Jitpack takes groupId, artifactId and version id from repository name, project name and tag ignoring actual values from pom.xml. So you need to write:

 com.github.vidstige jadb v1.2.1  

If you cannot connect to your device check the following.

  • Your adb server is running by issuing adb start-server
  • You can see the device using adb adb devices

If you see the device in adb but not in jadb please file an issue on https://github.com/vidstige/jadb/.

Workaround for Unix Sockets Adb Server

Install socat and issue the following to forward port 5037 to the unix domain socket.

socat TCP-LISTEN:5037,reuseaddr,fork UNIX-CONNECT:/tmp/5037

This project would not be where it is, if it where not for the helpful contributors supporting jadb with pull requests, issue reports, and great ideas. If you would like to contribute, please read through CONTRIBUTING.md.

  • If you fix a bug, try to first create a failing test. Reach out to me for assistance or guidance if needed.

This project is released under the Apache License Version 2.0, see LICENSE.md for more information.

About

Источник

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.

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

Here’s the result of the execution

GitHub Project

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

License

tananaev/adblib

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This project is a fork of the original library developed by Cameron Gutman.

Include dependency via Gradle:

compile 'com.tananaev:adblib:1.3'
dependency> groupId>com.tananaevgroupId> artifactId>adblibartifactId> version>1.3version> dependency>

To be able to connect to the ADB daemon on Android phone, you need to enable it to listen to TCP connections. To do that, connect your phone via USB cable and run following adb command:

Disconnect USB cable before trying to connect using the library. Some phones have problems handling TCP connection when they are connected via USB as well.

More info about Android remote debugging can be found on the official Android developer website.

Sample library usage example:

Socket socket = new Socket("192.168.1.42", 5555); // put phone IP address here AdbCrypto crypto = AdbCrypto.generateAdbKeyPair(new AdbBase64() < @Override public String encodeToString(byte[] data) < return DatatypeConverter.printBase64Binary(data); > >); AdbConnection connection = AdbConnection.create(socket, crypto); connection.connect(); AdbStream stream = connection.open("shell:logcat"); .
Copyright (c) 2013, Cameron Gutman All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

Источник

Оцените статью