AdvancedSharpAdbClient 2.5.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package AdvancedSharpAdbClient --version 2.5.1                
NuGet\Install-Package AdvancedSharpAdbClient -Version 2.5.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AdvancedSharpAdbClient" Version="2.5.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AdvancedSharpAdbClient --version 2.5.1                
#r "nuget: AdvancedSharpAdbClient, 2.5.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AdvancedSharpAdbClient as a Cake Addin
#addin nuget:?package=AdvancedSharpAdbClient&version=2.5.1

// Install AdvancedSharpAdbClient as a Cake Tool
#tool nuget:?package=AdvancedSharpAdbClient&version=2.5.1                

A .NET client for adb, the Android Debug Bridge (AdvancedSharpAdbClient)

AdvancedSharpAdbClient is a .NET library that allows .NET applications to communicate with Android devices. It provides a .NET implementation of the adb protocol, giving more flexibility to the developer than launching an adb.exe process and parsing the console output.

It's upgraded verion of SharpAdbClient. Added important features.

Changes

2.5.1

  • Now Click, Swipe, SendKeyEvent and SendText no longer return values, they have become voids
  • Added ElementNotFoundException and InvalidKeyEventException

2.5.0

  • First commit

Installation

To install AdvancedSharpAdbClient install the AdvancedSharpAdbClient NuGetPackage. If you're using Visual Studio, you can run the following command in the Package Manager Console:

PM> Install-Package AdvancedSharpAdbClient

Getting Started

AdvancedSharpAdbClient does not communicate directly with your Android devices, but uses the adb.exe server process as an intermediate. Before you can connect to your Android device, you must first start the adb.exe server.

You can do so by either running adb.exe yourself (it comes as a part of the ADK, the Android Development Kit), or you can use the AdbServer.StartServer method like this:

if (!AdbServer.Instance.GetStatus().IsRunning)
{
    AdbServer server = new AdbServer();
    StartServerResult result = server.StartServer(@"C:\adb\adb.exe", false);
    if (result != StartServerResult.Started)
    {
        Console.WriteLine("Can't start adb server");
    }
}

Connecting to device

Before using all the methods, you must initialize the new AdvancedAdbClient class and then connect to the device

If you want to automate 2 or more devices at the same time, you must remember: 1 device - 1 AdvancedAdbClient class

You can look at the examples to understand more

static AdvancedAdbClient client;

static DeviceData device;

static void Main(string[] args)
{
    client = new AdvancedAdbClient();
    client.Connect("127.0.0.1:62001");
    device = client.GetDevices().FirstOrDefault(); // Get first connected device
}

Device automation

Finding element

You can find the element on the screen by xpath

static AdvancedAdbClient client;

static DeviceData device;

static void Main(string[] args)
{
    client = new AdvancedAdbClient();
    client.Connect("127.0.0.1:62001");
    device = client.GetDevices().FirstOrDefault();
    Element el = client.FindElement(device, "//node[@text='Login']");
}

You can also specify the waiting time for the element

Element el = client.FindElement(device, "//node[@text='Login']", TimeSpan.FromSeconds(5));

And you can find several elements

Element[] els = client.FindElements(device, "//node[@resource-id='Login']", TimeSpan.FromSeconds(5));

Getting element attributes

You can get all element attributes

static void Main(string[] args)
{
    ...
    Element el = client.FindElement(device, "//node[@resource-id='Login']", TimeSpan.FromSeconds(3));
    string eltext = el.attributes["text"];
    string bounds = el.attributes["bounds"];
    ...
}

Clicking on an element

You can click on the x and y coordinates

static void Main(string[] args)
{
    ...
    client.Click(device, 600, 600); // Click on the coordinates (600;600)
    ...
}

Or on the element(need xpath)

static void Main(string[] args)
{
    ...
    Element el = client.FindElement(device, "//node[@text='Login']", TimeSpan.FromSeconds(3));
    el.Click();// Click on element by xpath //node[@text='Login']
    ...
}

The Click() method throw ElementNotFoundException if the element is not found

try
{
    el.Click();
}
catch (Exception ex)
{
    Console.WriteLine($"Can't click on the element:{ex.Message}");
}

Swipe

You can swipe from one element to another

static void Main(string[] args)
{
    ...
    Element first = client.FindElement(device, "//node[@text='Login']");
    Element second = client.FindElement(device, "//node[@text='Password']");
    client.Swipe(device, first, second, 100); // Swipe 100 ms
    ...
}

Or swipe by coordinates

static void Main(string[] args)
{
    ...
    device = client.GetDevices().FirstOrDefault();
    client.Swipe(device, 600, 1000, 600, 500, 100); // Swipe from (600;1000) to (600;500) on 100 ms
    ...
}

The Swipe() method throw ElementNotFoundException if the element is not found

try
{
    client.Swipe(device, 0x2232323, 0x954,0x9128,0x11111, 200);
    ...
    client.Swipe(device, first, second, 200);
}
catch (Exception ex)
{
    Console.WriteLine($"Can't swipe:{ex.Message}");
}

Send text

You can send any text except Cyrillic (Russian isn't supported by adb)

The text field should be in focus

static void Main(string[] args)
{
    ...
    client.SendText(device, "text"); // Send text to device
    ...
}

You can also send text to the element (clicks on the element and sends the text)

static void Main(string[] args)
{
    ...
    client.FindElement(device, "//node[@resource-id='Login']").SendText("text"); // Send text to the element by xpath //node[@resource-id='Login']
    ...
}

The SendText() method throw InvalidTextException if text is incorrect

try
{
    client.SendText(device, null);
}
catch (Exception ex)
{
    Console.WriteLine($"Can't send text:{ex.Message}");
}

Clearing the input text

You can clear text input

The text field should be in focus

Recommended

static void Main(string[] args)
{
    ...
    client.ClearInput(device, 25); // The second argument is to specify the maximum number of characters to be erased
    ...
}

It may work unstable

static void Main(string[] args)
{
    ...
    client.FindElement(device, "//node[@resource-id='Login']").ClearInput(); // Get element text attribute and remove text length symbols
    ...
}

Sending keyevents

You can see keyevents here https://developer.android.com/reference/android/view/KeyEvent#constants

static void Main(string[] args)
{
    ...
    client.SendKeyEvent(device, "KEYCODE_TAB");
    ...
}

The SendKeyEvent method throw InvalidKeyEventException if keyevent is incorrect

try
{
    client.SendKeyEvent(device, null);
}
catch (Exception ex)
{
    Console.WriteLine($"Can't send keyevent:{ex.Message}");
}

BACK and HOME buttons

static void Main(string[] args)
{
    ...
    client.BackBtn(device); // Click Back button
    ...
    client.HomeBtn(device); // Click Home button
    ...
}

Device commands

Install and Uninstall applications

static void Main(string[] args)
{
    ...
    PackageManager manager = new PackageManager(client, device);
    manager.InstallPackage(@"C:\Users\me\Documents\mypackage.apk", reinstall: false);
    manager.UninstallPackage("com.android.app");
    ...
}

Start and stop applications

static void Main(string[] args)
{
    ...
    client.StartApp(device, "com.android.app");
    client.StopApp(device, "com.android.app"); // force-stop
    ...
}

Getting a screenshot

static async void Main(string[] args)
{
    ...
    System.Drawing.Image img = client.GetFrameBufferAsync(device, CancellationToken.None).GetAwaiter().GetResult(); // synchronously
    ...
    System.Drawing.Image img = await client.GetFrameBufferAsync(device, CancellationToken.None); // asynchronously
    ...
}

Getting screen xml hierarchy

static void Main(string[] args)
{
    ...
    XmlDocument screen = client.DumpScreen(device);
    ...
}

Send or receive files

void DownloadFile()
{
    using (SyncService service = new SyncService(new AdbSocket(client.EndPoint), device))
    {
        using (Stream stream = File.OpenWrite(@"C:\MyFile.txt"))
        {
            service.Pull("/data/local/tmp/MyFile.txt", stream, null, CancellationToken.None);
        }
    }
}

void UploadFile()
{
    using (SyncService service = new SyncService(new AdbSocket(client.EndPoint), device))
    {
        using (Stream stream = File.OpenWrite(@"C:\MyFile.txt"))
        {
            service.Push(stream, "/data/local/tmp/MyFile.txt", 777 ,DateTimeOffset.Now, null ,CancellationToken.None);
        }
    }
}

Run shell commands

static async void Main(string[] args)
{
    ...
    ConsoleOutputReceiver receiver = new ConsoleOutputReceiver();
    client.ExecuteRemoteCommand("echo Hello, World", device, receiver); // synchronously
    ...
    await client.ExecuteRemoteCommandAsync("echo Hello, World", device, receiver, CancellationToken.None); // asynchronously
    ...
}

Encoding

Default encoding is UTF8,if you want to change it, use

AdvancedAdbClient.SetEncoding(Encoding.ASCII);

Consulting and Support

Please open an issue on if you have suggestions or problems.

History

AdvancedSharpAdbClient is a fork of SharpAdbClient and madb which in itself is a .NET port of the ddmlib Java Library.

Credits: https://github.com/camalot, https://github.com/quamotion

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AdvancedSharpAdbClient:

Package Downloads
TaiwanCaptain.God.AdbTool

godlike adb wrapper

GitHub repositories (5)

Showing the top 5 popular GitHub repositories that depend on AdvancedSharpAdbClient:

Repository Stars
Paving-Base/APK-Installer
An Android Application Installer for Windows
Macro-Deck-App/Macro-Deck
Macro Deck transforms your phone, tablet, or any device equipped with a modern internet browser into an efficient remote macro pad. With this tool, you can execute single or multi-step actions seamlessly with just a single tap.
TheAirBlow/HyperSploit
Bypassed HyperOS restrictions on bootloader unlocking
teamclouday/AndroidMic
Use your Android phone as a mic to Windows PC
prosthetichead/GarlicPress
GarlicPress is a companion application for the RG35xx running GarlicOS. The main aim of the application is to never require you to remove the SDCards from your device.
Version Downloads Last updated
3.3.13 12,427 7/30/2024
3.3.12 11,119 4/17/2024
3.2.11 2,790 3/20/2024
3.1.10 2,477 2/21/2024
3.0.9 40,879 1/1/2024
2.5.8 1,633 10/29/2023
2.5.7 11,424 7/10/2023
2.5.6 657 5/27/2023
2.5.5 68,229 4/4/2023
2.5.4 8,442 1/30/2023
2.5.3 650 12/21/2022
2.5.2 6,630 11/29/2021
2.5.1 595 9/29/2021
2.5.0 436 9/27/2021
1.3.0-g 334 9/26/2021