adb

package
v0.0.0-...-683b059 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package adb enables controlling android devices through the local adb server.

Index

Constants

View Source
const AndroidTmpDirPath = "/data/local/tmp"

AndroidTmpDirPath is the path of tmp directory in Android.

Variables

This section is empty.

Functions

func APKPath

func APKPath(value string) string

APKPath returns the absolute path to a helper APK.

func Command

func Command(ctx context.Context, args ...string) *testexec.Cmd

Command creates an ADB command with appropriate environment variables.

func InstallVendorKeys

func InstallVendorKeys() error

InstallVendorKeys installs vendor public/private key for authorizing adb connection.

func KillADBLocalServer

func KillADBLocalServer(ctx context.Context) error

KillADBLocalServer kills the existing ADB local server if it is running.

We do not use adb kill-server since it is unreliable (crbug.com/855325). We do not use killall since it can wait for orphan adb processes indefinitely (b/137797801).

func RegexpPred

func RegexpPred(exp *regexp.Regexp) func(string) bool

RegexpPred returns a function to be passed to WaitForLogcat that returns true if a given regexp is matched in that line.

Types

type BroadcastResult

type BroadcastResult struct {
	// The result value of the broadcast.
	Result int
	// Optional: Additional data to be passed with the result.
	Data *string
	// Optional: A bundle of extra data passed with the result.
	// TODO(springerm): extras is a key-value map and should be parsed.
	Extras *string
}

BroadcastResult is the parsed result of an Android Activity Manager broadcast.

type Device

type Device struct {
	// TransportID is used to distinguish the specific device.
	TransportID string

	// Serial is used as a backup to distinguish the specific device if TransportID is empty.
	Serial string

	// These are properties of the device returned by `adb devices -l` that may be blank.
	Device  string
	Model   string
	Product string
}

Device holds the resources required to communicate with a specific ADB device.

func Connect

func Connect(ctx context.Context, addr string, timeout time.Duration) (*Device, error)

Connect keeps trying to connect to an ADB at the specified address. Returns the device if the connection succeeds.

func Devices

func Devices(ctx context.Context) ([]*Device, error)

Devices returns a list of currently known ADB devices.

func WaitForDevice

func WaitForDevice(ctx context.Context, predicate func(device *Device) bool, timeout time.Duration) (*Device, error)

WaitForDevice waits for an ADB device with the set properties.

func (*Device) AndroidVersion

func (d *Device) AndroidVersion(ctx context.Context) (int, error)

AndroidVersion returns the Android version.

func (*Device) BluetoothMACAddress

func (d *Device) BluetoothMACAddress(ctx context.Context) (string, error)

BluetoothMACAddress returns the Bluetooth MAC address.

func (*Device) BluetoothStatus

func (d *Device) BluetoothStatus(ctx context.Context) (bool, error)

BluetoothStatus returns true if bluetooth is enabled, false if disabled.

func (*Device) BroadcastIntent

func (d *Device) BroadcastIntent(ctx context.Context, action string, params ...string) (*BroadcastResult, error)

BroadcastIntent broadcasts an intent with "am broadcast" and returns the result.

func (*Device) BroadcastIntentGetData

func (d *Device) BroadcastIntentGetData(ctx context.Context, action string, params ...string) (string, error)

BroadcastIntentGetData broadcasts an intent with "am broadcast" and returns the result data.

func (*Device) BugReport

func (d *Device) BugReport(ctx context.Context, path string) error

BugReport returns bugreport of the device.

func (*Device) ClearLogcat

func (d *Device) ClearLogcat(ctx context.Context) error

ClearLogcat clears all logcat buffers.

func (*Device) ClearPIN

func (d *Device) ClearPIN(ctx context.Context) error

ClearPIN clears a screen lock PIN on the Android device.

func (*Device) Command

func (d *Device) Command(ctx context.Context, args ...string) *testexec.Cmd

Command creates an ADB command on the specified device.

func (*Device) DisableBluetooth

func (d *Device) DisableBluetooth(ctx context.Context) error

DisableBluetooth disables bluetooth on the Android device. This function requires adb root access. Disabling bluetooth can be flaky, so retry until Bluetooth status returns true, ensuring it has been enabled.

func (*Device) DisableLockscreen

func (d *Device) DisableLockscreen(ctx context.Context, disable bool) error

DisableLockscreen allows toggling of the lock screen on Android.

func (*Device) DumpLogcat

func (d *Device) DumpLogcat(ctx context.Context, filePath string) error

DumpLogcat dumps logcat's output to the specified file.

func (*Device) EnableBluetooth

func (d *Device) EnableBluetooth(ctx context.Context) error

EnableBluetooth enables bluetooth on the Android device. This function requires adb root access. Enabling bluetooth can be flaky, so retry until Bluetooth status returns true, ensuring it has been enabled.

func (*Device) EnableBluetoothHciLogging

func (d *Device) EnableBluetoothHciLogging(ctx context.Context) error

EnableBluetoothHciLogging enables verbose bluetooth HCI logging. This function requires adb root access.

func (*Device) EnableVerboseLoggingForTag

func (d *Device) EnableVerboseLoggingForTag(ctx context.Context, tag string) error

EnableVerboseLoggingForTag enables verbose logging for the specified tag.

func (*Device) EnableVerboseWifiLogging

func (d *Device) EnableVerboseWifiLogging(ctx context.Context) error

EnableVerboseWifiLogging enables verbose WiFi logging on the device. This function requires adb root access.

func (*Device) FileSize

func (d *Device) FileSize(ctx context.Context, filename string) (int64, error)

FileSize returns the size of the specified file in bytes. Returns an error if the file does not exist. Note: In contrast to PkgFileSize, FileSize accesses files via adb commands.

func (*Device) ForwardTCP

func (d *Device) ForwardTCP(ctx context.Context, androidPort int) (int, error)

ForwardTCP forwards the ADB device local port specified to a host port and returns that host port.

func (*Device) GMSCoreVersion

func (d *Device) GMSCoreVersion(ctx context.Context) (int, error)

GMSCoreVersion returns the GMS Core version.

func (*Device) GetProp

func (d *Device) GetProp(ctx context.Context, key string) (string, error)

GetProp returns the Android system property indicated by the specified key.

func (*Device) GoogleAccount

func (d *Device) GoogleAccount(ctx context.Context) (string, error)

GoogleAccount returns the first found Google account signed in to the Android device.

func (*Device) GrantPermission

func (d *Device) GrantPermission(ctx context.Context, pkg, permission string) error

GrantPermission grants the requested permission to the specified app package.

func (*Device) Install

func (d *Device) Install(ctx context.Context, path string, installOptions ...InstallOption) error

Install installs an APK file to the Android system. By default, it uses InstallOptionReplaceApp and InstallOptionAllowVersionDowngrade.

func (*Device) InstallMultiple

func (d *Device) InstallMultiple(ctx context.Context, apks []string, installOptions ...InstallOption) error

InstallMultiple installs a split APK to the Android system. By default, it uses InstallOptionReplaceApp and InstallOptionAllowVersionDowngrade.

func (*Device) InstalledPackages

func (d *Device) InstalledPackages(ctx context.Context) (map[string]struct{}, error)

InstalledPackages returns a set of currently-installed packages, e.g. "android". This operation is slow (700+ ms), so unnecessary calls should be avoided.

func (*Device) IsConnected

func (d *Device) IsConnected(ctx context.Context) error

IsConnected checks if the device is connected.

func (*Device) ListContents

func (d *Device) ListContents(ctx context.Context, path string) ([]string, error)

ListContents returns the contents of the given path.

func (*Device) LockscreenDisabled

func (d *Device) LockscreenDisabled(ctx context.Context) (string, error)

LockscreenDisabled returns True when the lockscreen has been disabled.

func (*Device) OverridePhenotypeFlag

func (d *Device) OverridePhenotypeFlag(ctx context.Context, pkg, flag, value, valueType string) error

OverridePhenotypeFlag sets the specified Phenotype flag to the provided value for all accounts on the device.

func (*Device) PackageInstalled

func (d *Device) PackageInstalled(ctx context.Context, pkg string) (bool, error)

PackageInstalled returns true if the given package has been installed.

func (*Device) PressKeyCode

func (d *Device) PressKeyCode(ctx context.Context, keycode string) error

PressKeyCode sends a key event with the specified key code.

func (*Device) PullFile

func (d *Device) PullFile(ctx context.Context, src, dst string) error

PullFile copies a file in Android to Chrome OS with adb pull.

func (*Device) PushFile

func (d *Device) PushFile(ctx context.Context, src, dst string) error

PushFile copies a file in Chrome OS to Android with adb push.

func (*Device) PushFileToTmpDir

func (d *Device) PushFileToTmpDir(ctx context.Context, src string) (string, error)

PushFileToTmpDir copies a file in Chrome OS to Android temp directory. The destination path within the Android is returned.

func (*Device) ReadFile

func (d *Device) ReadFile(ctx context.Context, filename string) ([]byte, error)

ReadFile reads a file in Android file system with adb pull.

func (*Device) Reboot

func (d *Device) Reboot(ctx context.Context) error

Reboot reboots the adb device. Use WaitForDevice to wait for it to be up again.

func (*Device) RemoveAll

func (d *Device) RemoveAll(ctx context.Context, path string) error

RemoveAll removes all files and directories under the path in Android. The path must be abspath.

func (*Device) RemoveContents

func (d *Device) RemoveContents(ctx context.Context, path string) error

RemoveContents removes the contents under the path in Android. The path must be abspath.

func (*Device) RemoveForwardTCP

func (d *Device) RemoveForwardTCP(ctx context.Context, hostPort int) error

RemoveForwardTCP removes the forwarding from an ADB device local port to the specified host port.

func (*Device) RemoveMediaFile

func (d *Device) RemoveMediaFile(ctx context.Context, filePath string) error

RemoveMediaFile removes the media file specified by filePath from the Android device's storage and media gallery.

func (*Device) RemoveReverseTCP

func (d *Device) RemoveReverseTCP(ctx context.Context, androidPort int) error

RemoveReverseTCP removes the forwarding from a host port to the specified ADB device local port.

func (*Device) ReverseTCP

func (d *Device) ReverseTCP(ctx context.Context, hostPort int) (int, error)

ReverseTCP forwards the host port to an ADB device local port and returns that ADB device port.

func (*Device) Root

func (d *Device) Root(ctx context.Context) error

Root restarts adbd with root permissions.

func (*Device) SDKVersion

func (d *Device) SDKVersion(ctx context.Context) (int, error)

SDKVersion returns the Android SDK version.

func (*Device) SHA256Sum

func (d *Device) SHA256Sum(ctx context.Context, filename string) (string, error)

SHA256Sum returns the sha256sum of the specified file as a string.

func (*Device) SendIntentCommand

func (d *Device) SendIntentCommand(ctx context.Context, action, data string) *testexec.Cmd

SendIntentCommand returns a Cmd to send an intent with "am start" command.

func (*Device) SetPIN

func (d *Device) SetPIN(ctx context.Context) error

SetPIN sets a screen lock PIN on the Android device.

func (*Device) SetScreenOffTimeout

func (d *Device) SetScreenOffTimeout(ctx context.Context, t time.Duration) error

SetScreenOffTimeout sets the Android device's screen timeout. This function requires adb root access.

func (*Device) SetSystemProperty

func (d *Device) SetSystemProperty(ctx context.Context, propertyName, propertyValue string) error

SetSystemProperty sets the specified system property to the provided value on the device.

func (*Device) ShellCommand

func (d *Device) ShellCommand(ctx context.Context, name string, args ...string) *testexec.Cmd

ShellCommand returns a command in Android shell via adb.

func (*Device) StartScreenRecording

func (d *Device) StartScreenRecording(ctx context.Context, filename, outDir string) (func(context.Context, func() bool) error, error)

StartScreenRecording starts recording the screen. Returns a cleanup function that should be deferred, that does the following: 1. Stops screen recording. 2. Pulls the screen recording from the device to outDir, if hasError returns true. 3. Cleans up the screen recording on the device.

func (*Device) State

func (d *Device) State(ctx context.Context) (State, error)

State gets the state of an ADB device.

func (*Device) StayOnWhilePluggedIn

func (d *Device) StayOnWhilePluggedIn(ctx context.Context) error

StayOnWhilePluggedIn sets the screen to never sleep while charging.

func (*Device) TempDir

func (d *Device) TempDir(ctx context.Context) (string, error)

TempDir creates a temporary directory under AndroidTmpDirPath in Android, then returns its absolute path. It is caller's responsibility to remove all the contents in the directory after its use. One of the typical use cases will be as follows:

tmpdir, err := a.MktempDir(ctx)
if err != nil {
  ... // error handling
}
defer a.RemoveAll(tmpdir)
... // Main code using tmpdir.

func (*Device) Uninstall

func (d *Device) Uninstall(ctx context.Context, pkg string) error

Uninstall uninstalls a package from the Android system.

func (*Device) WaitForLockscreenDisabled

func (d *Device) WaitForLockscreenDisabled(ctx context.Context) error

WaitForLockscreenDisabled waits for the device to report the PIN is disabled.

func (*Device) WaitForLogcat

func (d *Device) WaitForLogcat(ctx context.Context, pred func(string) bool, quitFunc ...func() bool) error

WaitForLogcat keeps scanning logcat. The function pred is called on the logcat contents line by line. This function returns successfully if pred returns true. If pred never returns true, this function returns an error as soon as the context is done. An optional quitFunc will be polled at regular interval, which can be used to break early if for example the activity which is supposed to print the exp has crashed

func (*Device) WaitForState

func (d *Device) WaitForState(ctx context.Context, want State, timeout time.Duration) error

WaitForState waits for the device state to be equal to the state passed in.

func (*Device) WriteFile

func (d *Device) WriteFile(ctx context.Context, filename string, data []byte) error

WriteFile writes to a file in Android file system with adb push.

type InstallOption

type InstallOption string

InstallOption defines possible options to pass to "adb install".

const (
	InstallOptionLockApp               InstallOption = "-l"
	InstallOptionReplaceApp            InstallOption = "-r"
	InstallOptionAllowTestPackage      InstallOption = "-t"
	InstallOptionSDCard                InstallOption = "-s"
	InstallOptionAllowVersionDowngrade InstallOption = "-d"
	InstallOptionGrantPermissions      InstallOption = "-g"
	InstallOptionEphemeralInstall      InstallOption = "--instant"
	InstallOptionFromPlayStore         InstallOption = "-i com.android.vending"
)

ADB install options listed in "adb help".

type State

type State string

State describes the state of an ADB device.

const (
	StateOffline  State = "offline"
	StateDevice   State = "device"
	StateNoDevice State = "no device"
	// StateUnknown is only used when an error is returned and the state is unknown.
	StateUnknown State = "unknown"
)

Possible ADB device states as listed at https://developer.android.com/studio/command-line/adb#devicestatus

Jump to

Keyboard shortcuts

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