Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mrglaster/java-adb-library
Java library for working with Android smartphone via ADB
https://github.com/mrglaster/java-adb-library
adb adb-server android emulator java java17 java8 library
Last synced: 24 days ago
JSON representation
Java library for working with Android smartphone via ADB
- Host: GitHub
- URL: https://github.com/mrglaster/java-adb-library
- Owner: mrglaster
- Created: 2024-01-31T14:15:46.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-09-09T23:56:43.000Z (about 2 months ago)
- Last Synced: 2024-09-30T14:06:56.180Z (about 1 month ago)
- Topics: adb, adb-server, android, emulator, java, java17, java8, library
- Language: Java
- Homepage:
- Size: 354 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Java ADB Library
## Description
This library provides a comprehensive set of features to manage Android devices using ADB (Android Debug Bridge). It facilitates tasks such as application and permission management, file system interactions, and device network information retrieval. Key features include:
- Application Management: Install, uninstall, and retrieve detailed properties of applications (permissions, hashes).
- Permission Management: Grant and revoke application permissions using ADB shell.
- File System Operations: List directories, move, rename, push, and pull files on the Android device.
- Network Information: Retrieve network interfaces and IP addresses.
- Device Reboot Modes: Reboot into root, recovery, or bootloader mode.
- Power Management: Manage power-saving modes like Doze and light Doze.
- Device properties management: You can get any property of an Android device either by selecting the appropriate define or by the name of this propertyThis library is ideal for automating various Android device management tasks in development, testing, or security environments.
## Usage
### Installation
1) Build the library with command
```
mvn install
```2) Add the following dependency to your project
```xml
ru.opensource.adb
ADBLibrary
1.1```
3) Done! Now you can use the library in your project
### Usage examples#### ADBClient initialization and getting of decies list
```java
import ru.enplus.adblibrary.connection.ADBClient;
import ru.enplus.adblibrary.exceptions.paths.ADBNotFoundException;
import ru.enplus.adblibrary.exceptions.paths.ADBIncorrectPathException;public class Main {
public static void main(String[] args) {
try (ADBClient adbClient = new ADBClient()) {
// Retrieve connected devices
System.out.println("Connected devices: " + adbClient.getConnectedDevices());
} catch (ADBNotFoundException | ADBIncorrectPathException e) {
e.printStackTrace();
}
}
}
```#### Custom ADB Path Initialization
```java
import ru.enplus.adblibrary.connection.ADBClient;
import ru.enplus.adblibrary.exceptions.paths.ADBIncorrectPathException;public class Main {
public static void main(String[] args) {
String adbPath = "/path/to/adb";
try (ADBClient adbClient = new ADBClient(adbPath)) {
System.out.println("Connected devices: " + adbClient.getConnectedDevices());
} catch (ADBIncorrectPathException e) {
e.printStackTrace();
}
}
}
```
#### Connect to an External ADB Server```java
import ru.enplus.adblibrary.connection.ADBClient;
import ru.enplus.adblibrary.exceptions.base.ADBException;public class Main {
public static void main(String[] args) {
try (ADBClient adbClient = new ADBClient("127.0.0.1", "5037")) {
System.out.println("Connected devices: " + adbClient.getConnectedDevices());
} catch (ADBException e) {
e.printStackTrace();
}
}
}
```#### Execute Custom ADB Commands
```java
import ru.enplus.adblibrary.connection.ADBClient;
import ru.enplus.adblibrary.exceptions.base.ADBException;import java.util.List;
public class Main {
public static void main(String[] args) {
try (ADBClient adbClient = new ADBClient()) {
List output = adbClient.executeCommand("adb shell getprop ro.product.model");
output.forEach(System.out::println);
} catch (ADBException e) {
e.printStackTrace();
}
}
}```
#### Handling Shell Execution with Custom Handler
```java
import ru.enplus.adblibrary.connection.ADBClient;
import ru.enplus.adblibrary.exceptions.base.ADBException;
import ru.enplus.adblibrary.connection.ADBClient.CommandResultHandler;import java.util.List;
public class Main {
public static void main(String[] args) {
try (ADBClient adbClient = new ADBClient()) {
String deviceId = adbClient.getConnectedDevices().get(0);
String command = adbClient.getCommandBase(deviceId) + "ls /sdcard";
List files = adbClient.executeCommandWithHandling(command, new CommandResultHandler>() {
@Override
public List handle(List result) {
// Custom handling of result
return result;
}
});
files.forEach(System.out::println);
} catch (ADBException e) {
e.printStackTrace();
}
}
}```
#### Device information class initialization.
```java
import ru.enplus.adblibrary.connection.ADBClient;
import ru.enplus.adblibrary.domain.device.AndroidDeviceInfo;public class Main {
public static void main(String[] args) {
try (ADBClient adbClient = new ADBClient()) {
String deviceId = adbClient.getConnectedDevices().get(0);
AndroidDeviceInfo deviceInfo = new AndroidDeviceInfo(deviceId, adbClient);
System.out.println("Device Brand: " + deviceInfo.getBrand());
System.out.println("Device Model: " + deviceInfo.getModel());
System.out.println("Android Version: " + deviceInfo.getAndroidVersion());
System.out.println("Android SDK Version: " + deviceInfo.getAndroidSDKVersion());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```#### Retreive property by it's name
```java
import ru.enplus.adblibrary.connection.ADBClient;
import ru.enplus.adblibrary.domain.device.AndroidDeviceInfo;
import ru.enplus.adblibrary.domain.properties.AndroidDeviceProperties;public class Main {
public static void main(String[] args) {
try (ADBClient adbClient = new ADBClient()) {
String deviceId = adbClient.getConnectedDevices().get(0);
AndroidDeviceInfo deviceInfo = new AndroidDeviceInfo(deviceId, adbClient);
System.out.println(deviceInfo.getRawPropertyValue("gsm.sim.state"));
//OR
System.out.println(deviceInfo.getRawPropertyValue(AndroidDeviceProperties.ANDROID_SIM_STATE));
} catch (Exception e) {
e.printStackTrace();
}
}
}
```#### Work with AndroidDevice. Initialization (it inherits AndroidDeviceInfo, so all the methods from this class you can also invoke here)
```java
import ru.enplus.adblibrary.connection.ADBClient;
import ru.enplus.adblibrary.domain.device.AndroidDevice;public class Main {
public static void main(String[] args) {
try (ADBClient adbClient = new ADBClient()) {
String deviceId = adbClient.getConnectedDevices().get(0);
boolean collectApplicationProperties = true;
AndroidDevice device = new AndroidDevice(deviceId, adbClient, collectApplicationProperties);
System.out.println("Device Model: " + device.getModel());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```#### Install an App: Install an application from an APK file.
```java
boolean success = device.installAppFromApk("/path/to/app.apk");
```#### Uninstall an App: Uninstall an application using its package name.
```java
device.uninstallApp("com.example.app");
```#### List Applications: Retrieve a list of applications installed on the device.
```java
List apps = device.getApplications();
```#### You can grant or revoke permissions for applications on the device.
```java
device.grantPermissionShellOnly("com.example.app", "android.permission.CAMERA");
device.revokePermissionShellOnly("com.example.app", "android.permission.CAMERA");
```#### List Network Interfaces:
```java
List interfaces = device.getNetworkInterfaces();
```#### Get IP Address of a Network Interface:
```java
String ipAddress = device.getInterfaceIpAddress("wlan0", "ipv4");
```#### List Directory Contents:
```java
List dirContents = device.listDir("/sdcard/");
```#### Pull a File:
```java
device.pullFromDevice("/sdcard/file.txt", "/local/file.txt");
```#### Push a File:
```java
device.pushToDevice("/local/file.txt", "/sdcard/file.txt");
```#### Move and Rename Files:
```java
device.renameFile("/sdcard/file.txt", "newfile.txt");
device.moveFile("/sdcard/file.txt", "/sdcard/documents/newfile.txt");
```#### Reboot to Root:
```java
device.rebootRoot();
```#### Enable Doze Mode:
```java
device.enableDozeMode();
```#### Reboot to Bootloader:
```java
device.rebootBootloader();
```#### For deeper inspection of installed applications, you can collect their permissions and hashes (it will be done automaticlu if your collectApplicationProperties in constructor is true)
```java
device.collectApplicationProperties();
```#### You can do same using the separate providers
#### Work with file system
```java
AndroidFileSystemProvider fsProvider = new AndroidFileSystemProvider(adbClient, deviceId);
List files = fsProvider.listDir(path);
fsProvider.pullFromDevice(devicePath, outputPath);
fsProvider.pushToDevice(localPath, devicePath);
fsProvider.renameFile(filePath, newFileName);
fsProvider.moveFile(filePath, newPath);
```#### Work with Android device modes
```java
AndroidModeProvider modeProvider = new AndroidModeProvider(adbClient, deviceId);
boolean isRebooted = modeProvider.rebootToRoot();
modeProvider.rebootToRecovery();
modeProvider.rebootToBootloader();
modeProvider.enableDozeMode();
modeProvider.enterLightDozeMode();
modeProvider.enterDeepDozeMode();
modeProvider.enableNormalMode();
```### Work with applications data
```java
ApplicationDataProvider appDataProvider = new ApplicationDataProvider(adbClient, deviceId);
ArrayList applications = appDataProvider.getApplicationsList(0); // 0 means no restriction for count of applications to collectString applicationPath = applications.get(0).getPath();
Float versionNumeric = 8.0f; // Replace with the actual version number
try {
String[] hashes = appDataProvider.getApplicationHashes(applicationPath, versionNumeric);
System.out.println("SHA-1: " + hashes[0]);
System.out.println("SHA-256: " + hashes[1]);
System.out.println("SHA-512: " + hashes[2]);
} catch (IOException | NoSuchAlgorithmException | ADBException e) {
e.printStackTrace();
}AndroidApplication application = new AndroidApplication();
application.setPackageName("com.example.app"); // Replace with the actual package nametry {
appDataProvider.fillApplicationPermissions(application);
System.out.println("Dangerous Permissions: " + application.getDangerousPermissions());
System.out.println("Install Permissions: " + application.getInstallPermissions());
System.out.println("Runtime Permissions: " + application.getRuntimePermissions());
System.out.println("Requested Permissions: " + application.getRequestedPermissions());
} catch (ADBException e) {
e.printStackTrace();
}try {
appDataProvider.fillApplicationHashes(application, versionNumeric);
System.out.println("SHA-1: " + application.getSha1());
System.out.println("SHA-256: " + application.getSha256());
System.out.println("SHA-512: " + application.getSha512());
} catch (IOException | NoSuchAlgorithmException | ADBException e) {
e.printStackTrace();
}
```### Applications Management via ApplicationManagementProvider
```java
ApplicationManagementProvider appManagementProvider = new ApplicationManagementProvider(adbClient, deviceId);
appManagementProvider.uninstallApp(packageName);
boolean success = appManagementProvider.installAppFromApk(apkPath);
```### Work with applications permissions via ApplicationPermissionManagementProvider
```java
ApplicationPermissionManagementProvider permissionManagementProvider = new ApplicationPermissionManagementProvider(adbClient, deviceId);
String packageName = "com.example.app"; // Replace with the actual package name
String permission = "android.permission.CAMERA";
permissionManagementProvider.grantPermissionShellOnly(packageName, permission);//OR
AndroidApplication app = new AndroidApplication(....);
permissionManagementProvider.grantPermissionShellOnly(app, permission);permissionManagementProvider.revokePermissionShellOnly(packageName, permission);
permissionManagementProvider.revokePermissionShellOnly(app, permission);
```### Work with Network via NetworkDataProvider
```java
NetworkDataProvider networkDataProvider = new NetworkDataProvider(adbClient, deviceId);
ArrayList networkInterfaces = networkDataProvider.getNetworkInterfaces();
String ipv4Address = networkDataProvider.getInterfaceIpAddress("wlan0", "ipv4");
String ipv6Address = networkDataProvider.getInterfaceIpAddress("wlan0", "ipv6");
```