Documentation ¶
Overview ¶
Package adb enables controlling android devices through the local adb server.
Index ¶
- Constants
- func APKPath(value string) string
- func Command(ctx context.Context, args ...string) *testexec.Cmd
- func InstallVendorKeys() error
- func KillADBLocalServer(ctx context.Context) error
- func RegexpPred(exp *regexp.Regexp) func(string) bool
- type BroadcastResult
- type Device
- func (d *Device) AndroidVersion(ctx context.Context) (int, error)
- func (d *Device) BluetoothMACAddress(ctx context.Context) (string, error)
- func (d *Device) BluetoothStatus(ctx context.Context) (bool, error)
- func (d *Device) BroadcastIntent(ctx context.Context, action string, params ...string) (*BroadcastResult, error)
- func (d *Device) BroadcastIntentGetData(ctx context.Context, action string, params ...string) (string, error)
- func (d *Device) BugReport(ctx context.Context, path string) error
- func (d *Device) ClearLogcat(ctx context.Context) error
- func (d *Device) ClearPIN(ctx context.Context) error
- func (d *Device) Command(ctx context.Context, args ...string) *testexec.Cmd
- func (d *Device) DisableBluetooth(ctx context.Context) error
- func (d *Device) DisableLockscreen(ctx context.Context, disable bool) error
- func (d *Device) DumpLogcat(ctx context.Context, filePath string) error
- func (d *Device) EnableBluetooth(ctx context.Context) error
- func (d *Device) EnableBluetoothHciLogging(ctx context.Context) error
- func (d *Device) EnableVerboseLoggingForTag(ctx context.Context, tag string) error
- func (d *Device) EnableVerboseWifiLogging(ctx context.Context) error
- func (d *Device) FileSize(ctx context.Context, filename string) (int64, error)
- func (d *Device) ForwardTCP(ctx context.Context, androidPort int) (int, error)
- func (d *Device) GMSCoreVersion(ctx context.Context) (int, error)
- func (d *Device) GetProp(ctx context.Context, key string) (string, error)
- func (d *Device) GoogleAccount(ctx context.Context) (string, error)
- func (d *Device) GrantPermission(ctx context.Context, pkg, permission string) error
- func (d *Device) Install(ctx context.Context, path string, installOptions ...InstallOption) error
- func (d *Device) InstallMultiple(ctx context.Context, apks []string, installOptions ...InstallOption) error
- func (d *Device) InstalledPackages(ctx context.Context) (map[string]struct{}, error)
- func (d *Device) IsConnected(ctx context.Context) error
- func (d *Device) ListContents(ctx context.Context, path string) ([]string, error)
- func (d *Device) LockscreenDisabled(ctx context.Context) (string, error)
- func (d *Device) OverridePhenotypeFlag(ctx context.Context, pkg, flag, value, valueType string) error
- func (d *Device) PackageInstalled(ctx context.Context, pkg string) (bool, error)
- func (d *Device) PressKeyCode(ctx context.Context, keycode string) error
- func (d *Device) PullFile(ctx context.Context, src, dst string) error
- func (d *Device) PushFile(ctx context.Context, src, dst string) error
- func (d *Device) PushFileToTmpDir(ctx context.Context, src string) (string, error)
- func (d *Device) ReadFile(ctx context.Context, filename string) ([]byte, error)
- func (d *Device) Reboot(ctx context.Context) error
- func (d *Device) RemoveAll(ctx context.Context, path string) error
- func (d *Device) RemoveContents(ctx context.Context, path string) error
- func (d *Device) RemoveForwardTCP(ctx context.Context, hostPort int) error
- func (d *Device) RemoveMediaFile(ctx context.Context, filePath string) error
- func (d *Device) RemoveReverseTCP(ctx context.Context, androidPort int) error
- func (d *Device) ReverseTCP(ctx context.Context, hostPort int) (int, error)
- func (d *Device) Root(ctx context.Context) error
- func (d *Device) SDKVersion(ctx context.Context) (int, error)
- func (d *Device) SHA256Sum(ctx context.Context, filename string) (string, error)
- func (d *Device) SendIntentCommand(ctx context.Context, action, data string) *testexec.Cmd
- func (d *Device) SetPIN(ctx context.Context) error
- func (d *Device) SetScreenOffTimeout(ctx context.Context, t time.Duration) error
- func (d *Device) SetSystemProperty(ctx context.Context, propertyName, propertyValue string) error
- func (d *Device) ShellCommand(ctx context.Context, name string, args ...string) *testexec.Cmd
- func (d *Device) StartScreenRecording(ctx context.Context, filename, outDir string) (func(context.Context, func() bool) error, error)
- func (d *Device) State(ctx context.Context) (State, error)
- func (d *Device) StayOnWhilePluggedIn(ctx context.Context) error
- func (d *Device) SurfaceFlingerLatencyCommand(ctx context.Context, surfaceViewName string) *testexec.Cmd
- func (d *Device) TempDir(ctx context.Context) (string, error)
- func (d *Device) Uninstall(ctx context.Context, pkg string) error
- func (d *Device) WaitForLockscreenDisabled(ctx context.Context) error
- func (d *Device) WaitForLogcat(ctx context.Context, pred func(string) bool, quitFunc ...func() bool) error
- func (d *Device) WaitForState(ctx context.Context, want State, timeout time.Duration) error
- func (d *Device) WriteFile(ctx context.Context, filename string, data []byte) error
- type InstallOption
- type State
Constants ¶
const AndroidTmpDirPath = "/data/local/tmp"
AndroidTmpDirPath is the path of tmp directory in Android.
Variables ¶
This section is empty.
Functions ¶
func InstallVendorKeys ¶
func InstallVendorKeys() error
InstallVendorKeys installs vendor public/private key for authorizing adb connection.
func KillADBLocalServer ¶
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).
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 ¶
Connect keeps trying to connect to an ADB at the specified address. Returns the device if the connection succeeds.
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 ¶
AndroidVersion returns the Android version.
func (*Device) BluetoothMACAddress ¶
BluetoothMACAddress returns the Bluetooth MAC address.
func (*Device) BluetoothStatus ¶
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) ClearLogcat ¶
ClearLogcat clears all logcat buffers.
func (*Device) DisableBluetooth ¶
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 ¶
DisableLockscreen allows toggling of the lock screen on Android.
func (*Device) DumpLogcat ¶
DumpLogcat dumps logcat's output to the specified file.
func (*Device) EnableBluetooth ¶
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 ¶
EnableBluetoothHciLogging enables verbose bluetooth HCI logging. This function requires adb root access.
func (*Device) EnableVerboseLoggingForTag ¶
EnableVerboseLoggingForTag enables verbose logging for the specified tag.
func (*Device) EnableVerboseWifiLogging ¶
EnableVerboseWifiLogging enables verbose WiFi logging on the device. This function requires adb root access.
func (*Device) FileSize ¶
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 ¶
ForwardTCP forwards the ADB device local port specified to a host port and returns that host port.
func (*Device) GMSCoreVersion ¶
GMSCoreVersion returns the GMS Core version.
func (*Device) GetProp ¶
GetProp returns the Android system property indicated by the specified key.
func (*Device) GoogleAccount ¶
GoogleAccount returns the first found Google account signed in to the Android device.
func (*Device) GrantPermission ¶
GrantPermission grants the requested permission to the specified app package.
func (*Device) Install ¶
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 ¶
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 ¶
IsConnected checks if the device is connected.
func (*Device) ListContents ¶
ListContents returns the contents of the given path.
func (*Device) LockscreenDisabled ¶
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 ¶
PackageInstalled returns true if the given package has been installed.
func (*Device) PressKeyCode ¶
PressKeyCode sends a key event with the specified key code.
func (*Device) PushFileToTmpDir ¶
PushFileToTmpDir copies a file in Chrome OS to Android temp directory. The destination path within the Android is returned.
func (*Device) Reboot ¶
Reboot reboots the adb device. Use WaitForDevice to wait for it to be up again.
func (*Device) RemoveAll ¶
RemoveAll removes all files and directories under the path in Android. The path must be abspath.
func (*Device) RemoveContents ¶
RemoveContents removes the contents under the path in Android. The path must be abspath.
func (*Device) RemoveForwardTCP ¶
RemoveForwardTCP removes the forwarding from an ADB device local port to the specified host port.
func (*Device) RemoveMediaFile ¶
RemoveMediaFile removes the media file specified by filePath from the Android device's storage and media gallery.
func (*Device) RemoveReverseTCP ¶
RemoveReverseTCP removes the forwarding from a host port to the specified ADB device local port.
func (*Device) ReverseTCP ¶
ReverseTCP forwards the host port to an ADB device local port and returns that ADB device port.
func (*Device) SDKVersion ¶
SDKVersion returns the Android SDK version.
func (*Device) SendIntentCommand ¶
SendIntentCommand returns a Cmd to send an intent with "am start" command.
func (*Device) SetScreenOffTimeout ¶
SetScreenOffTimeout sets the Android device's screen timeout. This function requires adb root access.
func (*Device) SetSystemProperty ¶
SetSystemProperty sets the specified system property to the provided value on the device.
func (*Device) ShellCommand ¶
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) StayOnWhilePluggedIn ¶
StayOnWhilePluggedIn sets the screen to never sleep while charging.
func (*Device) SurfaceFlingerLatencyCommand ¶
func (d *Device) SurfaceFlingerLatencyCommand(ctx context.Context, surfaceViewName string) *testexec.Cmd
SurfaceFlingerLatencyCommand returns a command specifically for calling the SurfaceFlinger command for latency, via adb.
func (*Device) TempDir ¶
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) WaitForLockscreenDisabled ¶
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 ¶
WaitForState waits for the device state to be equal to the state passed in.
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