Name Date Size #Lines LOC

..--

examples/H25-Apr-2025-1,5871,035

gradle/wrapper/H25-Apr-2025-76

third_party/sl4a/H25-Apr-2025-3,4962,311

.gitignoreH A D25-Apr-202582 108

Android.bpH A D25-Apr-2025972 3128

CHANGELOGH A D25-Apr-20251.3 KiB3730

CONTRIBUTINGH A D25-Apr-20251.4 KiB2824

LICENSEH A D25-Apr-202511.1 KiB203169

METADATAH A D25-Apr-2025687 2119

MODULE_LICENSE_APACHE2HD25-Apr-20250

OWNERSH A D25-Apr-2025202 119

README.mdH A D25-Apr-20253 KiB6749

build.gradleH A D25-Apr-2025658 2924

gradle.propertiesH A D25-Apr-20251 KiB3022

gradlewH A D25-Apr-20254.9 KiB161120

gradlew.batH A D25-Apr-20252.3 KiB9166

settings.gradleH A D25-Apr-2025315 109

README.md

1# Getting Started with Snippets for Mobly
2
3Mobly Snippet Lib is a library for triggering device-side code from host-side
4[Mobly](http://github.com/google/mobly) tests. This tutorial teaches you how to
5use the snippet lib to trigger custom device-side actions.
6
7Note: Mobly and the snippet lib are not official Google products.
8
9
10## Prerequisites
11
12-   These examples and tutorials assume basic familiarity with the Mobly
13    framework, so please follow the
14    [Mobly tutorial](http://github.com/google/mobly) before doing this one.
15-   You should know how to create an Android app and build it with gradle. If
16    not, follow the
17    [Android app tutorial](https://developer.android.com/training/basics/firstapp/index.html).
18
19
20## Overview
21
22The Mobly Snippet Lib allows you to write Java methods that run on Android
23devices, and trigger the methods from inside a Mobly test case. The Java methods
24invoked this way are called `snippets`.
25
26The `snippet` code can either be written in its own standalone apk, or as a
27[product flavor](https://developer.android.com/studio/build/build-variants.html#product-flavors)
28of an existing apk. This allows you to write snippets that instrument or
29automate another app.
30
31
32## Under The Hood
33
34A snippet is launched by an `am instrument` call. Snippets use a custom
35`InstrumentationTestRunner` derived from `AndroidJUnitRunner`. This allows
36for snippets that interact with a main app's classes, such as Espresso snippets,
37and allows you to get either the test app's or the main app's context from
38`InstrumentationRegistry`.
39
40Once started, the special runner starts a web server which listens for requests
41to trigger snippets. The server's handler locates the corresponding methods by
42reflection, runs them, and returns results over the TCP socket. All common
43built-in variable types are supported as arguments.
44
45
46## Usage
47
48The [examples/](examples/) folder contains examples of how to use the
49mobly snippet lib along with detailed tutorials.
50
51*   [ex1_standalone_app](examples/ex1_standalone_app): Basic example of a
52    snippet which is compiled into its own standalone apk.
53*   [ex2_espresso](examples/ex2_espresso): Example of a snippet which
54    instruments a primary app to drive its UI using
55    [Espresso](https://google.github.io/android-testing-support-library/docs/espresso/).
56*   [ex3_async_event](examples/ex3_async_event): Example of how to use the
57    @AsyncRpc annotation to handle asynchronous callbacks.
58*   [ex4_uiautomator](examples/ex4_uiautomator): Example of how to create
59    snippets that automate the UI actions using UIAutomator. Unlike Espresso
60    UIAutomator works even without access to app source code.
61*   [ex5_schedule_rpc](examples/ex5_schedule_rpc): Example of how to use the
62    'scheduleRpc' RPC to execute another RPC at a later time, potentially after
63    device disconnection.
64*   [ex6_complex_type_conversion](examples/ex6_complex_type_conversion): Example of how to pass a
65    non-primitive type to the Rpc methods and return non-primitive type from Rpc methods by
66    supplying a type converter.
67