gadb

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 12 Imported by: 21

README

gadb

ADB Client in pure Golang.

Installation

go get github.com/electricbubble/gadb

Example

package main

import (
	"github.com/electricbubble/gadb"
	"log"
	"os"
	"strings"
)

func main() {
	adbClient, err := gadb.NewClient()
	checkErr(err, "fail to connect adb server")

	devices, err := adbClient.DeviceList()
	checkErr(err)

	if len(devices) == 0 {
		log.Fatalln("list of devices is empty")
	}

	dev := devices[0]

	userHomeDir, _ := os.UserHomeDir()
	apk, err := os.Open(userHomeDir + "/Desktop/xuexi_android_10002068.apk")
	checkErr(err)

	log.Println("starting to push apk")

	remotePath := "/data/local/tmp/xuexi_android_10002068.apk"
	err = dev.PushFile(apk, remotePath)
	checkErr(err, "adb push")

	log.Println("push completed")

	log.Println("starting to install apk")

	shellOutput, err := dev.RunShellCommand("pm install", remotePath)
	checkErr(err, "pm install")
	if !strings.Contains(shellOutput, "Success") {
		log.Fatalln("fail to install: ", shellOutput)
	}

	log.Println("install completed")

}

func checkErr(err error, msg ...string) {
	if err == nil {
		return
	}

	var output string
	if len(msg) != 0 {
		output = msg[0] + " "
	}
	output += err.Error()
	log.Fatalln(output)
}

Thanks

Thank you JetBrains for providing free open source licenses


Repository Description
zach-klippenstein/goadb A Golang library for interacting with adb.
vidstige/jadb ADB Client in pure Java.
Swind/pure-python-adb This is pure-python implementation of the ADB client.
codeskyblue/fa FA(fast adb) helps you win at ADB(Android Debug Bridge).

Documentation

Index

Constants

View Source
const AdbDaemonPort = 5555
View Source
const AdbServerPort = 5037
View Source
const DefaultFileMode = os.FileMode(0664)

Variables

View Source
var DefaultAdbReadTimeout time.Duration = 60
View Source
var ErrConnBroken = errors.New("socket connection broken")

Functions

func SetDebug

func SetDebug(debug bool)

SetDebug set debug mode

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient() (Client, error)

func NewClientWith

func NewClientWith(host string, port ...int) (adbClient Client, err error)

func (Client) Connect added in v0.0.3

func (c Client) Connect(ip string, port ...int) (err error)

func (Client) DeviceList

func (c Client) DeviceList() (devices []Device, err error)

func (Client) DeviceSerialList

func (c Client) DeviceSerialList() (serials []string, err error)

func (Client) Disconnect added in v0.0.3

func (c Client) Disconnect(ip string, port ...int) (err error)

func (Client) DisconnectAll added in v0.0.3

func (c Client) DisconnectAll() (err error)

func (Client) ForwardKillAll added in v0.0.3

func (c Client) ForwardKillAll() (err error)

func (Client) ForwardList added in v0.0.3

func (c Client) ForwardList() (deviceForward []DeviceForward, err error)

func (Client) KillServer

func (c Client) KillServer() (err error)

func (Client) ServerVersion

func (c Client) ServerVersion() (version int, err error)

type Device

type Device struct {
	// contains filtered or unexported fields
}

func (Device) DeviceInfo

func (d Device) DeviceInfo() map[string]string

func (Device) DevicePath

func (d Device) DevicePath() (string, error)

func (Device) EnableAdbOverTCP added in v0.0.3

func (d Device) EnableAdbOverTCP(port ...int) (err error)

func (Device) Forward

func (d Device) Forward(localPort, remotePort int, noRebind ...bool) (err error)

func (Device) ForwardKill

func (d Device) ForwardKill(localPort int) (err error)

func (Device) ForwardList

func (d Device) ForwardList() (deviceForwardList []DeviceForward, err error)

func (Device) HasAttribute added in v0.1.0

func (d Device) HasAttribute(key string) bool

func (Device) IsUsb

func (d Device) IsUsb() (bool, error)

func (Device) List added in v0.0.5

func (d Device) List(remotePath string) (devFileInfos []DeviceFileInfo, err error)

func (Device) Model

func (d Device) Model() (string, error)

func (Device) Product

func (d Device) Product() (string, error)

func (Device) Pull added in v0.0.6

func (d Device) Pull(remotePath string, dest io.Writer) (err error)

func (Device) Push added in v0.0.5

func (d Device) Push(source io.Reader, remotePath string, modification time.Time, mode ...os.FileMode) (err error)

func (Device) PushFile added in v0.0.5

func (d Device) PushFile(local *os.File, remotePath string, modification ...time.Time) (err error)

func (Device) RunShellCommand

func (d Device) RunShellCommand(cmd string, args ...string) (string, error)

func (Device) RunShellCommandWithBytes added in v0.0.4

func (d Device) RunShellCommandWithBytes(cmd string, args ...string) ([]byte, error)

func (Device) Serial

func (d Device) Serial() string

func (Device) State

func (d Device) State() (DeviceState, error)

func (Device) Usb

func (d Device) Usb() (string, error)

type DeviceFileInfo added in v0.0.5

type DeviceFileInfo struct {
	Name         string
	Mode         os.FileMode
	Size         uint32
	LastModified time.Time
}

func (DeviceFileInfo) IsDir added in v0.0.5

func (info DeviceFileInfo) IsDir() bool

type DeviceForward added in v0.0.4

type DeviceForward struct {
	Serial string
	Local  string
	Remote string
}

type DeviceState

type DeviceState string
const (
	StateUnknown      DeviceState = "UNKNOWN"
	StateOnline       DeviceState = "online"
	StateOffline      DeviceState = "offline"
	StateDisconnected DeviceState = "disconnected"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL