lxzh / dadb

A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dadb

Maven Central

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

dependencies {
  implementation("dev.mobile:dadb:<version>")
}

Example Usage

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)

Discover a Device

Searches localhost ports 5555 through 5683 for a valid adb device:

val dadb = Dadb.discover("localhost")
if (dadb == null) throw RuntimeException("No adb device found")

Connecting to a physical device

Connect over Wi-Fi - Android 11+ (Wi-Fi Pairing)

  1. Pair your device over wifi using the standard adb pair HOST:PORT CODE command (docs).
    • eg: adb pair 10.0.0.192:45678 123456
  2. Tell your device to listen for connections on tcp port 5555.
    • adb tcpip 5555
  3. Once your device is listening for connections on port 5555, dadb can connect to your device using your device's IP address (same as HOST above).
    • eg: Dadb.connect(10.0.0.192, 5555).

Connect over Wi-Fi - Android 10 and below

  1. Tell your device to listen for connections on tcp port 5555 (docs):
    • Connect your device via USB, then run adb tcpip 5555
  2. Find you device's IP address. (See: Step 6)
  3. Connect to your device's IP address using dadb:
    • eg: Dadb.connect(10.0.0.192, 5555)

USB

Connections over USB are not currently supported.

Install / Uninstall APK

dadb.install(exampleApkFile)
dadb.uninstall("com.example.app")

Push / Pull Files

dadb.push(srcFile, "/data/local/tmp/dst.txt")
dadb.pull(dstFile, "/data/local/tmp/src.txt")

Execute Shell Command

val response = dadb.shell("echo hello")
assert(response.exitCode == 0)
assert(response.output == "hello\n")

TCP Forwarding

dadb.tcpForward(
    hostPort = 7001,
    targetPort = 7001
).use {
    // localhost:7001 is now forwarded to device's 7001 port
    // Do operations that depend on port forwarding
}

Authentication

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)

License

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.

About

A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server

License:Apache License 2.0


Languages

Language:Kotlin 100.0%