Issues | License | NuGet |
---|---|---|
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.
- .NET Framework 3.5 (Maybe unstable)
- .NET Framework 4.0 (Need Microsoft.Bcl.Async)
- .NET Framework 4.5.2
- .NET Framework 4.6.2
- .NET Framework 4.7.2
- .NET Framework 4.8.1
- .NET Standard 1.3 (Not fully supported)
- .NET Standard 2.0 (Support UWP if you don't use unsupport api like Process)
- .NET Core App 3.1
- .NET 6.0
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
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");
}
}
Before using all the methods, you must initialize the new AdbClient 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 AdbClient class
You can look at the examples to understand more
static AdbClient client;
static DeviceData device;
static void Main(string[] args)
{
client = new AdbClient();
client.Connect("127.0.0.1:62001");
device = client.GetDevices().FirstOrDefault(); // Get first connected device
}
You can find the element on the screen by xpath
static AdbClient client;
static DeviceData device;
static void Main(string[] args)
{
client = new AdbClient();
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));
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"];
...
}
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}");
}
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}");
}
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}");
}
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
...
}
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}");
}
static void Main(string[] args)
{
...
client.BackBtn(device); // Click Back button
...
client.HomeBtn(device); // Click Home button
...
}
Some commands require Root
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");
...
}
Or you can use AdbClient.Install
static void Main(string[] args)
{
...
client.Install(device, File.OpenRead("Application.apk"));
...
}
static void Main(string[] args)
{
...
PackageManager manager = new PackageManager(client, device);
manager.InstallMultiplePackage(@"C:\Users\me\Documents\base.apk", new string[] { @"C:\Users\me\Documents\split_1.apk", @"C:\Users\me\Documents\split_2.apk" }, reinstall: false); // Install split app whith base app
manager.InstallMultiplePackage(new string[] { @"C:\Users\me\Documents\split_3.apk", @"C:\Users\me\Documents\split_4.apk" }, "com.android.app", reinstall: false); // Add split app to base app which packagename is 'com.android.app'
...
}
Or you can use AdbClient.Install
static void Main(string[] args)
{
...
client.InstallMultiple(device, File.OpenRead("base.apk"), new Stream[] { File.OpenRead("split_1.apk"), File.OpenRead("split_2.apk") }); // Install split app whith base app
client.InstallMultiple(device, new Stream[] { File.OpenRead("split_3.apk"), File.OpenRead("split_4.apk") }, "com.android.app"); // Add split app to base app which packagename is 'com.android.app'
...
}
static void Main(string[] args)
{
...
client.StartApp(device, "com.android.app");
client.StopApp(device, "com.android.app"); // force-stop
...
}
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
...
}
static void Main(string[] args)
{
...
XmlDocument screen = client.DumpScreen(device);
...
}
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.OpenRead(@"C:\MyFile.txt"))
{
service.Push(stream, "/data/local/tmp/MyFile.txt", 777 ,DateTimeOffset.Now, null ,CancellationToken.None);
}
}
}
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
...
}
Default encoding is UTF8, if you want to change it, use
AdbClient.SetEncoding(Encoding.ASCII);
Please open an issue on if you have suggestions or problems.
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