Code Monkey home page Code Monkey logo

dadb's Introduction

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

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()

Connecting to a physical device

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 Dadb.discover() and Dadb.list() methods now both support USB-connected devices.

// Both of these will include any USB-connected devices if they are available
val dadb = Dadb.discover()
val dadbs = Dadb.list()

If you'd like to connect directly to a physical device via its serial number. Use the following API:

val dadb = AdbServer.createDadb(
    adbServerHost = "localhost",
    adbServerPort = 5037,
    deviceQuery = "host:transport:${serialNumber}"
)

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.

dadb's People

Contributors

axelniklasson avatar championswimmer avatar dmitry-zaitsev avatar goooler avatar leland-takamine avatar mfilenko avatar salvatoret avatar tiann avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.