Name Date Size #Lines LOC

..--

src/main/H25-Apr-2025-11555

README.mdH A D25-Apr-20253.1 KiB10980

build.gradleH A D25-Apr-2025835 2824

README.md

1# Standalone Snippet App Example
2
3This tutorial shows you how to create a standalone Mobly snippet app. To create
4a snippet app that controls (instruments) another app under test, please see
5[Example 2](../ex2_espresso/README.md).
6
7## Tutorial
8
91.  Use Android Studio to create a new app project.
10
111.  Link against Mobly Snippet Lib in your `build.gradle` file
12
13    ```
14    dependencies {
15      implementation 'com.google.android.mobly:mobly-snippet-lib:1.4.0'
16    }
17    ```
18
191.  Write a Java class implementing `Snippet` and add methods to trigger the
20    behaviour that you want. Annotate them with `@Rpc`
21
22    ```java
23    package com.my.app;
24    ...
25    public class ExampleSnippet implements Snippet {
26      @Rpc(description="Returns a string containing the given number.")
27      public String getFoo(Integer input) {
28        return "foo " + input;
29      }
30
31      @Override
32      public void shutdown() {}
33    }
34    ```
35
361.  Add any classes that implement the `Snippet` interface in your
37    `AndroidManifest.xml` application section as `meta-data`
38
39    ```xml
40    <manifest
41        xmlns:android="http://schemas.android.com/apk/res/android"
42        package="com.my.app">
43      <application>
44        <meta-data
45            android:name="mobly-snippets"
46            android:value="com.my.app.test.MySnippet1,
47                           com.my.app.test.MySnippet2" />
48        ...
49    ```
50
51
521.  Add an `instrumentation` tag to your `AndroidManifest.xml` so that the
53    framework can launch your server through an `instrument` command.
54
55    ```xml
56    <manifest
57        xmlns:android="http://schemas.android.com/apk/res/android"
58        package="com.my.app">
59      <application>...</application>
60      <instrumentation
61          android:name="com.google.android.mobly.snippet.SnippetRunner"
62          android:targetPackage="com.my.app" />
63    </manifest>
64    ```
65
661.  Build your apk and install it on your phone
67
681.  In your Mobly python test, connect to your snippet .apk in `setup_class`
69
70    ```python
71    class HelloWorldTest(base_test.BaseTestClass):
72      def setup_class(self):
73        self.ads = self.register_controller(android_device)
74        self.dut1 = self.ads[0]
75        self.dut1.load_snippet(name='snippet', package='com.my.app.test')
76    ```
77
786.  Invoke your needed functionality within your test
79
80    ```python
81    def test_get_foo(self):
82      actual_foo = self.dut1.snippet.getFoo(5)
83      asserts.assert_equal("foo 5", actual_foo)
84    ```
85
86## Running the example code
87
88This folder contains a fully working example of a standalone snippet apk.
89
901.  Compile the example
91
92        ./gradlew examples:ex1_standalone_app:assembleDebug
93
941.  Install the apk on your phone
95
96        adb install -r ./examples/ex1_standalone_app/build/outputs/apk/debug/ex1_standalone_app-debug.apk
97
981.  Use `snippet_shell` from mobly to trigger `getFoo()`:
99
100        snippet_shell.py com.google.android.mobly.snippet.example1
101
102        >>> print(s.help())
103        Known methods:
104          getBar(String) returns String  // Returns the given string with the prefix "bar"
105          getFoo(Integer) returns String  // Returns the given integer with the prefix "foo"
106
107        >>> s.getFoo(5)
108        u'foo 5'
109