A Kotlin based Android library to connect Android devices directly without ADB binary file.
dependencies {
implementation("com.flyfishxu:kadb:<version>")
}
- Dadb: Kadb is based on the second development of Dadb, and we would like to thank mobile-dev-inc for all their previous efforts!
- Connect to an Android device and execute ADB commands without ADB binary file.
- Support ADB SSL connection (Requires target device running Android 11 and above).
- Support ADB Pair instead of the legacy way to authenticate with the target Android device you want to connect (Requires target device running Android 11 and above).
- Support for Android DocumentFile in push and pull methods.
- Throw an exception if the target device is not authorized (Dadb can't check if the connected device is authorized)
- Lower Android version support compared to Dadb.
Connect to emulator-5554
and install apkFile
:
Kadb.create("localhost", 5555).use { kadb ->
kadb.install(apkFile)
}
Note: Connect to the odd adb daemon port (5555), not the even emulator console port (5554)
Host: 10.0.0.175; Port: 37755; PairCode: 643102
Kadb.pair("10.0.0.175", 37755, "643102")
Note: Pair only works when target device running Android 11 and above
The following discovers and returns a connected device or emulator. If there are multiple it returns the first one found.
val kadb = Kadb.discover()
if (kadb == null) throw RuntimeException("No adb device found")
Use the following API if you want to list all available devices:
val kadbs = Kadb.list()
Prerequisite: Connecting to a physical device requires a running adb server. In most cases, this means that you must have the adb
binary installed on your machine.
The Kadb.discover()
and Kadb.list()
methods now both support USB-connected devices.
// Both of these will include any USB-connected devices if they are available
val kadb = Kadb.discover()
val kadbs = Kadb.list()
If you'd like to connect directly to a physical device via its serial number. Use the following API:
val kadb = AdbServer.createKadb(
adbServerHost = "localhost",
adbServerPort = 5037,
deviceQuery = "host:transport:${serialNumber}"
)
kadb.install(exampleApkFile)
kadb.uninstall("com.example.app")
kadb.push(srcFile, "/data/local/tmp/dst.txt")
kadb.pull(dstFile, "/data/local/tmp/src.txt")
val response = kadb.shell("echo hello")
assert(response.exitCode == 0)
assert(response.output == "hello\n")
kadb.tcpForward(
hostPort = 7001,
targetPort = 7001
).use {
// localhost:7001 is now forwarded to device's 7001 port
// Do operations that depend on port forwarding
}