Blog Post: Our First Open-Source Project
A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server.
The core dadb artifact is a JVM library. Platform-specific transports can be added separately.
dependencies {
implementation("dev.mobile:dadb:<version>")
}Connect to emulator-5554 and install apkFile:
Dadb.create("localhost", 5555).use { dadb ->
dadb.install(apkFile)
}Note: Connect to the odd adb daemon port (5555), not the even emulator console port (5554)
If you are running on Android and have access to USB host APIs, add the Android transport module:
implementation("dev.mobile:dadb-android:<version>")Then create a direct USB transport from UsbManager and UsbDevice:
val dadb = Dadb.create(
transportFactory = UsbTransportFactory(usbManager, usbDevice, "usb:${usbDevice.deviceName}"),
keyPair = AdbKeyPair.readDefault(),
)This transport is Android-only. Desktop JVM users should use the core dadb artifact with direct TCP or adb server.
If you already have an adb server available, you can connect through it instead of providing a direct transport.
This is useful on desktop JVM environments where physical devices are already managed by adb.
val dadb = AdbServer.createDadb(
adbServerHost = "localhost",
adbServerPort = 5037,
deviceQuery = "host:transport:${serialNumber}"
)The following discovers and returns a connected device or emulator. If there are multiple it returns the first one found.
val dadb = Dadb.discover()
if (dadb == null) throw RuntimeException("No adb device found")Use the following API if you want to list all available devices:
val dadbs = Dadb.list()If an adb server is available, Dadb.discover() and Dadb.list() can also return USB-connected physical devices.
// Both of these will include any USB-connected devices if they are available
val dadb = Dadb.discover()
val dadbs = Dadb.list()If your ADB packets do not travel over TCP, Android USB host APIs, or adb server, you can still supply your own transport.
This is useful for tunnels, in-process bridges, and other bidirectional byte streams.
val dadb = Dadb.create(
description = "my transport",
keyPair = AdbKeyPair.readDefault(),
) {
SourceSinkAdbTransport(
source = mySource,
sink = mySink,
description = "my transport",
closeable = myTransport,
)
}dadb.install(exampleApkFile)
dadb.uninstall("com.example.app")dadb.push(srcFile, "/data/local/tmp/dst.txt")
dadb.pull(dstFile, "/data/local/tmp/src.txt")val response = dadb.shell("echo hello")
assert(response.exitCode == 0)
assert(response.output == "hello\n")dadb.tcpForward(
hostPort = 7001,
targetPort = 7001
).use {
// localhost:7001 is now forwarded to device's 7001 port
// Do operations that depend on port forwarding
}Dadb will use your adb key at ~/.android/adbkey by default. If none exists at this location, private and public keys will be generated by dadb.
If you need to specify a custom path to your adb key, use the optional keyPair argument:
val adbKeyPair = AdbKeyPair.read(privateKeyFile, publicKeyFile)
Dadb.create("localhost", 5555, adbKeyPair)Copyright (c) 2021 mobile.dev inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
