DroidBot Documentation

repository·master·Indexed 21 days ago

https://github.com/honeynet/droidbot

DroidBot is a lightweight, GUI-model-based test input generator for Android applications that automates UI testing by sending random or scripted events. It generates UI transition graphs (UTG) for analysis without requiring app instrumentation. It supports various input policies (DFS, BFS, Monkey), computer vision mode via OpenCV, and a distributed architecture with master and worker nodes.

Tokens
2.5K
Snippets
10
Records
14
Agent score
68%

What's inside DroidBot

  1. Install DroidBot

    master

    To install DroidBot, clone the repository and install it in editable mode using pip.

    Prerequisites

    Before installing, ensure you have the following configured:

    1. Python (versions 2 and 3 are supported).
    2. Java.
    3. Android SDK.
    4. The platform_tools directory from the Android SDK must be added to your system PATH.
    5. (Optional) OpenCV-Python if you intend to use the -cv mode.

    After installation, verify it works by running droidbot -h.

    git clone https://github.com/honeynet/droidbot.git
    cd droidbot/
    pip install -e .
  2. Run DroidBot for App Testing

    master

    DroidBot generates test input events for Android apps and produces a UI transition graph (UTG).

    Basic Usage

    To start testing an app, provide the path to the .apk file and an output directory:

    droidbot -a <path_to_apk> -o output_dir

    Requirements

    • An .apk file path.
    • A device or emulator connected via adb.
    • Accessibility Service: On some devices, you must manually enable the Accessibility Service to allow DroidBot to retrieve the current view hierarchy.
  3. Use DroidBot in distributed mode

    master

    DroidBot supports a distributed architecture consisting of a master and worker nodes.

    • Master Mode: Start a node as a master using -distributed master. This node uses DroidMaster to coordinate tasks.
    • Worker Mode: Start a node as a worker using -distributed worker. This node uses DroidBot and connects to the master via the address provided in the -master flag.

    When running in master mode, you can also specify QEMU parameters like -qemu_hda (the HDA image) and -qemu_no_graphic (to run without graphics).

  4. Run DroidBot in Distributed Mode

    master

    DroidBot supports a distributed architecture consisting of a master and worker nodes.

    • Master Mode: Use -distributed master and provide the RPC address via -master. The master uses DroidMaster to coordinate.
    • Worker Mode: Use -distributed worker. Workers use DroidBot to execute tests and connect to the master.
    # Start as a master
    python start.py -a app.apk -distributed master -master 127.0.0.1:5000
    
    # Start as a worker
    python start.py -a app.apk -distributed worker -master 127.0.0.1:5000
  5. Run DroidBot via CLI

    master

    DroidBot can be started from the command line using the start.py script. It is used to test Android applications by automating user interactions based on a specified input policy.

    Basic usage requires providing the path to the target APK using the -a flag. You can optionally specify a target device serial with -d, an output directory with -o, and an input policy with -policy.

    python start.py -a /path/to/your/app.apk -o ./output_dir -d emulator-5554
  6. Configure DroidBot CLI options

    master

    Use the following CLI flags to customize DroidBot's behavior:

    FlagDescription
    -a <path_to_apk>Path to the target .apk file.
    -o <output_dir>Directory where results (including UTG) will be saved.
    -d <device_serial>Specify a target device serial number (use adb devices to find it).
    -script <path_to_script.json>Path to a JSON-format script to customize input for specific UI states.
    -keep_envAvoid re-installing the test environment between app tests (useful for large-scale testing).
    -cvEnables computer vision mode. Use this if the app does not support Accessibility-based view retrieval (e.g., Cocos2d or Unity3d games).
    -humanoidEnables communication with Humanoid to generate human-like test inputs.
  7. Initialize the App class

    master

    The App class is used to represent an Android application and extract its metadata (package name, activities, permissions, etc.) from an APK file. You can initialize it by providing the local path to the APK and an optional output directory for results.

    from droidbot.app import App
    
    # Initialize with the path to your APK
    app = App(app_path='/path/to/your/app.apk', output_dir='/path/to/output')
    
    print(f"Package: {app.get_package_name()}")
    print(f"Main Activity: {app.get_main_activity()}")
  8. Reference: DroidBot CLI Arguments

    master

    The following command-line arguments are available when running start.py:

    FlagDestinationDescription
    -ddevice_serialThe serial number of the target device (find via adb devices).
    -aapk_pathRequired. The file path to the target APK.
    -ooutput_dirDirectory where test outputs will be saved.
    -policyinput_policyThe strategy used for test input generation. (See Input Policies)
    -distributeddistributedStart in distributed mode. Choices: master, worker.
    -mastermasterThe RPC address for the DroidMaster.
    -qemu_hdaqemu_hdaThe QEMU's hda image.
    -qemu_no_graphicqemu_no_graphicRun QEMU with the -nograpihc parameter.
    -scriptscript_pathPath to a script used to customize input for specific states.
    -countcountTotal number of events to generate.
    -intervalintervalInterval in seconds between events.
    -timeouttimeoutTimeout in seconds (-1 for unlimited).
    -cvcv_modeUse OpenCV instead of UIAutomator to identify UI components. Requires opencv-python.
    -debugdebug_modeRun in debug mode (dump debug messages).
    -randomrandom_inputAdd randomness to input events.
    -keep_appkeep_appKeep the app on the device after testing.
    -keep_envkeep_envKeep the test environment (e.g., minicap, accessibility service) after testing.
    -use_method_profilingprofiling_methodRecord method trace for each event. Values: full or a sampling rate.
    -grant_permgrant_permGrant all permissions during installation (useful for Android 6.0+).
    -is_emulatoris_emulatorDeclare the target device as an emulator for special handling.
    -accessibility_autoenable_accessibility_hardEnable accessibility service automatically (useful for API < 23).
    -humanoidhumanoidConnect to a Humanoid service (addr:port) for human-like behaviors.
    -ignore_adignore_adIgnore Ad views by checking resource_id.
    -replay_outputreplay_outputThe DroidBot output directory to be replayed.

    <a name="input-policies"></a>

    Input Policies (-policy)

    Policy ValueDescription
    input_policy.POLICY_NONENo events sent; manual interaction required.
    input_policy.POLICY_MONKEYUses adb shell monkey to send events.
    input_policy.POLICY_NAIVE_DFSExplores UI using a naive depth-first strategy.
    input_policy.POLICY_GREEDY_DFSExplores UI using a greedy depth-first strategy.
    input_policy.POLICY_NAIVE_BFSExplores UI using a naive breadth-first strategy.
    input_policy.POLICY_GREEDY_BFSExplores UI using a greedy breadth-first strategy.
  9. Configure DroidBot input policies

    master

    The -policy flag determines how DroidBot generates test inputs. The available policies are:

    • input_policy.POLICY_NONE: No events will be sent; requires manual interaction.
    • input_policy.POLICY_MONKEY: Uses adb shell monkey to send events.
    • input_policy.POLICY_NAIVE_DFS: Explores UI using a naive depth-first strategy.
    • input_policy.POLICY_GREEDY_DFS: Explores UI using a greedy depth-first strategy.
    • input_policy.POLICY_NAIVE_BFS: Explores UI using a naive breadth-first strategy.
    • input_policy.POLICY_GREEDY_BFS: Explores UI using a greedy breadth-first strategy.
  10. Calculate APK file hashes

    master

    The get_hashes(block_size=256) method calculates the MD5, SHA-1, and SHA-256 hashes of the input APK file. This is useful for verifying file integrity or identifying specific versions of an app.

    hashes = app.get_hashes()
    # Returns a list: [md5, sha1, sha256]
    print(f"MD5: {hashes[0]}")
  11. Get the start intent with profiling

    master

    Use get_start_with_profiling_intent(trace_file, sampling=None) to retrieve an Intent object that starts the application while simultaneously enabling a profiler.

    • trace_file: The path where the profiling trace should be saved.
    • sampling: (Optional) An integer representing the sampling rate.
    trace_path = "/tmp/trace.txt"
    # Start with default sampling
    intent = app.get_start_with_profiling_intent(trace_path)
    
    # Start with specific sampling rate
    intent_with_sampling = app.get_start_with_profiling_intent(trace_path, sampling=100)