Bug: 213332752

Clone this repo:
  1. ad7daba mobly-snippet-lib: Set a min_sdk to allow to be installed on android S devices am: 684989f3d7 am: 8ba1fb90cd by Roshan Pius · 7 months ago main-16k master
  2. 8ba1fb9 mobly-snippet-lib: Set a min_sdk to allow to be installed on android S devices am: 684989f3d7 by Roshan Pius · 7 months ago
  3. 684989f mobly-snippet-lib: Set a min_sdk to allow to be installed on android S devices by Roshan Pius · 7 months ago
  4. 119b666 Set sdk_version on mobule snippet lib am: c1a1245a8a am: 21869df031 am: 5fb5461850 am: 1299454335 by Julien Desprez · 11 months ago android13-dev android13-frc-adbd-release android13-frc-art-release android13-frc-cellbroadcast-release android13-frc-conscrypt-release android13-frc-documentsui-release android13-frc-extservices-release android13-frc-ipsec-release android13-frc-media-release android13-frc-media-swcodec-release android13-frc-networking-release android13-frc-neuralnetworks-release android13-frc-os-statsd-release android13-frc-permission-release android13-frc-resolv-release android13-frc-scheduling-release android13-mainline-adbd-release android13-mainline-adservices-release android13-mainline-appsearch-release android13-mainline-art-release android13-mainline-cellbroadcast-release android13-mainline-conscrypt-release android13-mainline-extservices-release android13-mainline-go-adbd-release android13-mainline-go-adservices-release android13-mainline-go-appsearch-release android13-mainline-go-art-release android13-mainline-go-cellbroadcast-release android13-mainline-go-conscrypt-release android13-mainline-go-documentsui-release android13-mainline-go-extservices-release android13-mainline-go-ipsec-release android13-mainline-go-media-release android13-mainline-go-media-swcodec-release android13-mainline-go-mediaprovider-release android13-mainline-go-networking-release android13-mainline-go-neuralnetworks-release android13-mainline-go-odp-release android13-mainline-go-os-statsd-release android13-mainline-go-permission-release android13-mainline-go-resolv-release android13-mainline-go-scheduling-release android13-mainline-go-sdkext-release android13-mainline-go-tethering-release android13-mainline-go-tzdata4-release android13-mainline-go-uwb-release android13-mainline-go-wifi-release android13-mainline-ipsec-release android13-mainline-media-release android13-mainline-media-swcodec-release android13-mainline-mediaprovider-release android13-mainline-networking-release android13-mainline-os-statsd-release android13-mainline-permission-release android13-mainline-resolv-release android13-mainline-sdkext-release android13-mainline-tethering-release android13-mainline-tzdata4-release android13-mainline-uwb-release android13-mainline-wifi-release aml_adb_331011040 aml_adb_331011050 aml_ads_331131000 aml_art_331012050 aml_ase_331011020 aml_cbr_330810000 aml_cbr_330911010 aml_cbr_331013010 aml_con_331011010 aml_ext_331012020 aml_go_adb_330913000 aml_go_ads_330913000 aml_go_art_330913000 aml_go_ase_330913000 aml_go_cbr_330912000 aml_go_con_330913000 aml_go_doc_330912000 aml_go_ext_330912000 aml_go_ips_330911000 aml_go_med_330913000 aml_go_mpr_330912000 aml_go_net_330913000 aml_go_neu_330912000 aml_go_odp_330912000 aml_go_per_330912000 aml_go_res_330912000 aml_go_sch_330911000 aml_go_sdk_330810000 aml_go_sta_330911000 aml_go_swc_330913000 aml_go_tet_330914010 aml_go_tz4_330912000 aml_go_uwb_330912000 aml_go_wif_330911000 aml_ips_331014020 aml_med_331012020 aml_mpr_330811020 aml_mpr_330911040 aml_mpr_331011070 aml_net_330811010 aml_net_330910010 aml_net_331011030 aml_per_330811030 aml_per_330912010 aml_per_331019040 aml_res_330810000 aml_res_330910000 aml_res_331011050 aml_sdk_330810010 aml_sdk_330810050 aml_sta_330910000 aml_sta_331010010 aml_swc_331012020 aml_tet_330812150 aml_tet_330911010 aml_tet_331012080 aml_tz4_331012000 aml_tz4_331012040 aml_tz4_331012050 aml_uwb_330810010 aml_uwb_331015040 aml_wif_330810040 aml_wif_330910030 aml_wif_331016070 t_frc_adb_330444000 t_frc_art_330443060 t_frc_ase_330444010 t_frc_cbr_330443000 t_frc_con_330443020 t_frc_doc_330443000 t_frc_doc_330443060 t_frc_ext_330443000 t_frc_ips_330443010 t_frc_med_330443030 t_frc_net_330443000 t_frc_neu_330443000 t_frc_neu_330443030 t_frc_per_330444010 t_frc_res_330443000 t_frc_sch_330443010 t_frc_sch_330443040 t_frc_sta_330443010 t_frc_swc_330443010 t_frc_swc_330443040 t_frc_tz4_330443010
  5. 1299454 Set sdk_version on mobule snippet lib am: c1a1245a8a am: 21869df031 am: 5fb5461850 by Julien Desprez · 11 months ago

Getting Started with Snippets for Mobly

Mobly Snippet Lib is a library for triggering device-side code from host-side Mobly tests. This tutorial teaches you how to use the snippet lib to trigger custom device-side actions.

Note: Mobly and the snippet lib are not official Google products.


  • These examples and tutorials assume basic familiarity with the Mobly framework, so please follow the Mobly tutorial before doing this one.
  • You should know how to create an Android app and build it with gradle. If not, follow the Android app tutorial.


The Mobly Snippet Lib allows you to write Java methods that run on Android devices, and trigger the methods from inside a Mobly test case. The Java methods invoked this way are called snippets.

The snippet code can either be written in its own standalone apk, or as a product flavor of an existing apk. This allows you to write snippets that instrument or automate another app.

Under The Hood

A snippet is launched by an am instrument call. Snippets use a custom InstrumentationTestRunner derived from AndroidJUnitRunner. This allows for snippets that interact with a main app‘s classes, such as Espresso snippets, and allows you to get either the test app’s or the main app's context from InstrumentationRegistry.

Once started, the special runner starts a web server which listens for requests to trigger snippets. The server's handler locates the corresponding methods by reflection, runs them, and returns results over the TCP socket. All common built-in variable types are supported as arguments.


The examples/ folder contains examples of how to use the mobly snippet lib along with detailed tutorials.

  • ex1_standalone_app: Basic example of a snippet which is compiled into its own standalone apk.
  • ex2_espresso: Example of a snippet which instruments a primary app to drive its UI using Espresso.
  • ex3_async_event: Example of how to use the @AsyncRpc annotation to handle asynchronous callbacks.
  • ex4_uiautomator: Example of how to create snippets that automate the UI actions using UIAutomator. Unlike Espresso UIAutomator works even without access to app source code.
  • ex5_schedule_rpc: Example of how to use the ‘scheduleRpc’ RPC to execute another RPC at a later time, potentially after device disconnection.
  • ex6_complex_type_conversion: Example of how to pass a non-primitive type to the Rpc methods and return non-primitive type from Rpc methods by supplying a type converter.