Class AdbWrapper
java.lang.Object
com.pnfsoftware.jeb.core.units.code.android.adb.AdbWrapper
A controller for the Android Debug Bridge (ADB) tool. Remember to call
initialize()
if
necessary (e.g., first adb run). Most methods in this class return null on error.
This class contains several helpful utility methods; if what you're looking for is not here, use
execute
or executeOn
as a last resort. More complex and/or smarter wrapper functions can also be found in
the AndroidDeviceUtil
class.
This class is thread-safe; the object itself may be used in synchronized blocks for transactions.
TODO: use ProcessWrapper
-
Field Summary
Modifier and TypeFieldDescriptionstatic final long
Standard command execution timeout is 5 seconds. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Change the permissions of a file or folder.boolean
Change the ownership of a file or folder.boolean
Copy a file or folder.long
Retrieve the device date in seconds since 1970-01-01 00:00:00 UTC.dateString
(boolean appendTimezoneDelta) Retrieve the device date in the following format:YYYY-mm-dd_HH:MM:SS
orYYYY-mm-dd_HH:MM:SS_TZD
where TZD is like "+/-HHMM"boolean
deleteFile
(String user, String path, boolean recurse) Delete a file or folder.byte[]
execute
(Long timeout, Collection<String> arguments) Execute a command using ADB.executeAsync
(Long timeout, Collection<String> arguments) Advanced method used to execute an ADB command with as much flexibility as needed.byte[]
executeOn
(Long timeout, String deviceSerial, Collection<String> arguments) Execute a command using ADB.boolean
forwardJdwpToTcp
(int srcJdwpPid, int dstTcpPort) boolean
forwardLocalToTcp
(String srcDomainSocket, int dstTcpPort) boolean
forwardToTcp
(AdbForwardType srcType, String srcDomainSocket, int dstTcpPort) long
getPath()
Get the path to the ADB binary wrapped by this object.getRunAs()
Get the default target device for operations.getToolName
(String name) Get the adb tool version used by this wrapper.boolean
Verify if a file is present.boolean
boolean
installApp
(String apkPath) boolean
installApp
(String apkPath, boolean allowReplace, boolean onSdcard, boolean grantRuntimePermissions) Install an APK.boolean
installAppForce
(String apkPath) void
killProcess
(String user, int pid) Send the TERM signal to request normal termination of a process.void
killProcess
(String user, int pid, int signal) Send a signal to a process.boolean
killProcess
(String user, int pid, String... signalNames) Send a signal to a process.List the PIDs of processes hosting a JDWP transport.listPackages
(AdbPackageManagerOptions options) List the processes running on this device.logcat()
Execute thelogcat
command.Execute thelogcat
command with filter options.boolean
boolean
Download a file from a device.boolean
Upload a file onto a device.readProperty
(String propertyName) Read a device Android property.void
setDefaultTimeout
(long millis) void
void
setTargetDevice
(String deviceSerial) Set the default target device for operations.void
setToolName
(String standardName, String alternateName) Advanced method used to execute a shell ADB command.boolean
startActivity
(String pname, String aname) boolean
startActivity
(String pname, String aname, boolean bEnableDebugging, boolean bWaitForLaunch, boolean bForceStopBeforeStart) Start an activity.boolean
Start the ADB server (on the local machine).boolean
stopAllForwards
(int dstTcpPort) boolean
boolean
stopForwardToTcp
(int dstTcpPort) Issue aadb forward --remove tcp:port
command.boolean
stopProcess
(String user, int pid) Send the STOP signal to a process.void
Stop the ADB server.toString()
boolean
Create an empty file.boolean
uninstallApp
(String packageName) boolean
uninstallApp
(String packageName, boolean keepData) Uninstall an APK.boolean
Usesdev.bootcomplete
property.boolean
Usesinit.svc.bootanim
property.boolean
Uses the Package Manager to find out whether the system is ready for app installation.boolean
adb wait-for-device
command.Find the location of a binary file.
-
Field Details
-
STANDARD_TIMEOUT
public static final long STANDARD_TIMEOUTStandard command execution timeout is 5 seconds.- See Also:
-
-
Method Details
-
initialize
public boolean initialize() -
getPath
Get the path to the ADB binary wrapped by this object.- Returns:
-
setTargetDevice
Set the default target device for operations.- Parameters:
deviceSerial
- the serial number (the case matters)
-
getTargetDevice
Get the default target device for operations. The result may be null, in which case, adb will work if and only if a single device is connected (and that device will be used for operations).- Returns:
- the serial number (the case matters); may be null if no target was explicitly specified
-
setRunAs
-
getRunAs
-
setToolName
-
getToolName
-
setDefaultTimeout
public void setDefaultTimeout(long millis) -
getDefaultTimeout
public long getDefaultTimeout() -
getVersion
Get the adb tool version used by this wrapper.- Returns:
-
startServer
public boolean startServer()Start the ADB server (on the local machine).- Returns:
-
stopServer
public void stopServer()Stop the ADB server. -
listDevices
-
logcat
Execute thelogcat
command.- Returns:
- the raw logcat stream data
- Throws:
IOException
- on error
-
logcat
Execute thelogcat
command with filter options.- Parameters:
pid
- (optional) only the logs issued by the specified processafterDate
- (optional) cut-off date: only logs generated after the provided date are output; format must be likeYYYY-mm-dd HH:MM:SS.xxx
- Returns:
- the raw logcat stream data
- Throws:
IOException
- on error
-
logcatClear
public boolean logcatClear() -
waitForDevice
public boolean waitForDevice()adb wait-for-device
command. Uses a 30 second time-out.- Returns:
-
waitForBootCompletionHeur1
public boolean waitForBootCompletionHeur1()Usesdev.bootcomplete
property. Uses a 30 second time-out.Does not seem reliable.
- Returns:
-
waitForBootCompletionHeur2
public boolean waitForBootCompletionHeur2()Usesinit.svc.bootanim
property. Uses a 30 second time-out.Does not seem reliable.
- Returns:
-
waitForBootCompletionHeur3
public boolean waitForBootCompletionHeur3()Uses the Package Manager to find out whether the system is ready for app installation. Uses a 30 second time-out.Flaky heuristic (Jenkins only), b/c reading 'init.svc.bootanim' does not seem to do the trick on my slow emulator.
- Returns:
-
installApp
-
installAppForce
-
installApp
public boolean installApp(String apkPath, boolean allowReplace, boolean onSdcard, boolean grantRuntimePermissions) Install an APK.- Parameters:
apkPath
-allowReplace
-onSdcard
-grantRuntimePermissions
-- Returns:
-
uninstallApp
-
uninstallApp
Uninstall an APK.- Parameters:
packageName
-keepData
-- Returns:
-
pushFile
Upload a file onto a device.- Parameters:
localPath
-remotePath
-- Returns:
-
pullFile
Download a file from a device.- Parameters:
remotePath
-localPath
-- Returns:
-
listJdwpPids
List the PIDs of processes hosting a JDWP transport. Use a timeout of 1 second.- Returns:
- a list of process ids
-
forwardJdwpToTcp
public boolean forwardJdwpToTcp(int srcJdwpPid, int dstTcpPort) -
forwardLocalToTcp
-
forwardToTcp
-
stopForwardToTcp
public boolean stopForwardToTcp(int dstTcpPort) Issue aadb forward --remove tcp:port
command.- Parameters:
dstTcpPort
-- Returns:
-
stopAllForwards
public boolean stopAllForwards(int dstTcpPort) -
readProperty
Read a device Android property.- Parameters:
propertyName
- property fully-qualified name- Returns:
-
listProcesses
List the processes running on this device.- Returns:
-
listPackages
-
listPackages
-
startActivity
public boolean startActivity(String pname, String aname, boolean bEnableDebugging, boolean bWaitForLaunch, boolean bForceStopBeforeStart) Start an activity. Uses the Activity Manager (am) internally. Example:adb shell am start -n com.xyz.appcheck/.AppCheck
- Parameters:
pname
-aname
-bEnableDebugging
- will wait for debugger to connect. If true, bForceStopBeforeStart is disregarded; also not recommended to set bWaitForLaunch to true, as am would not return until a debugger connectsbWaitForLaunch
- wait for activity to have launched before 'am' returnsbForceStopBeforeStart
- the already running activity, if there is one, will be killed (instead of simply be brought up to the front)- Returns:
-
startActivity
-
stopApp
-
killProcess
Send the TERM signal to request normal termination of a process. -
stopProcess
Send the STOP signal to a process. -
killProcess
Send a signal to a process. -
killProcess
Send a signal to a process. -
hasFile
Verify if a file is present.- Parameters:
user
-path
- full exact path, no wildcard- Returns:
-
touchFile
Create an empty file.- Parameters:
user
-path
-- Returns:
- false indicates sure failure; true indicates potential success (not guaranteed; use
#hasFile(String, String, String)
to confirm)
-
deleteFile
Delete a file or folder.- Parameters:
user
-path
-recurse
-- Returns:
-
copyFile
Copy a file or folder.- Parameters:
user
-srcPath
-dstPath
-- Returns:
-
chmod
Change the permissions of a file or folder.- Parameters:
user
-path
-perm
-recurse
-- Returns:
-
chown
Change the ownership of a file or folder.- Parameters:
user
-path
-userName
- mandatorygroupName
- optionalrecurse
-- Returns:
-
which
Find the location of a binary file.- Parameters:
user
-path
-- Returns:
-
dateEpoch
public long dateEpoch()Retrieve the device date in seconds since 1970-01-01 00:00:00 UTC.- Returns:
- -1 on error, else the Unix timestamp
-
dateString
Retrieve the device date in the following format:YYYY-mm-dd_HH:MM:SS
orYYYY-mm-dd_HH:MM:SS_TZD
where TZD is like "+/-HHMM"Examples: "2016-01-16_12:32:44", 2016-01-16_12:32:44_-0500
- Parameters:
appendTimezoneDelta
- append time zone delta information- Returns:
- the date string
-
shellLog
-
shell
-
shell
-
shell
-
shell
-
executeOn
Execute a command using ADB. Do not provide "-s ..." manually, use the appropriate argument instead.- Parameters:
timeout
- null to use a default timeoutdeviceSerial
- optional, leave null to let ADB decide on which device the command should be executedarguments
-- Returns:
-
execute
Execute a command using ADB. This method may block indefinitely, for some time, or return immediately.- Parameters:
timeout
- null to use the default timeout; else, refer toCommandExec(long)
for timeout meaningarguments
-- Returns:
-
executeAsync
Advanced method used to execute an ADB command with as much flexibility as needed. This method is non-blocking.- Parameters:
timeout
- timeout in ms (use -1 to specify no timeout; use null to use the default timeout)arguments
- adb arguments- Returns:
- a process wrapper object; null on error
- Throws:
IOException
- on error
-
shellAsync
public ProcessWrapper shellAsync(Long timeout, String deviceSerial, String user, List<String> arguments) throws IOException Advanced method used to execute a shell ADB command. This method is non-blocking.- Parameters:
timeout
- timeout in ms (use -1 to specify no timeout; use null to use the default timeout)deviceSerial
- optional target deviceuser
- optional userarguments
- adb arguments- Returns:
- a process wrapper object; null on error
- Throws:
IOException
- on error
-
toString
-