Skip to content

Most visited

Recently visited


Wrap Shell Script

When debugging and profiling apps with native code, it’s often useful to be able to bundle command-line developer tools in your APK and run them automatically on the device when the app launches.

Beginning in Android 8.0, you can opt to create and package a user-defined shell script that the system automatically runs in a fresh process when you launch a debuggable app. This feature lets you write a script to invoke native developer tools to perform such operations as:

Using the wrap shell script

To run native developer tools on the device using the wrap shell script, follow these steps:

  1. Compile a custom debuggable APK that packages the following:
  2. Install the debuggable APK on a device. Depending on the Android platform version on the device, you may need to modify access control over processes for the wrap shell script to run.
    • Android 8.1 (API level 27) or higher: No such modification is needed.
    • Android 8.0 (API level 26): The device must have SELinux enforcement disabled. You can disable SELinux enforcement via adb on userdebug or eng builds. Supported devices with these permission levels are:
      • An Android emulator with a userdebug system image, or
      • A Google device flashed with an AOSP eng build or root image.
  3. Launch the app normally on your device, either via the app launcher or from the command line.
  4. It is strongly recommended to re-enable SELinux enforcement on your device when done.

Creating the wrap shell script

You can create a shell script named to customize your runtime environment. In your script, you can set environment variables and modify runtime arguments. The script should follow MirBSD Korn shell (mksh) syntax.

When you launch a debuggable APK that contains, the system executes the script and grants it control over the app process. The system passes the command to start the requested app as an argument to the invocation. Note that your script is responsible for starting the app.

The following snippet shows how to write a simple file that just starts the app:


To start the malloc debug tool using, you would include the following line:

LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper $@


To take advantage of, you must build a debuggable APK. Make sure that the android:debuggable=”true”setting is configured in the <application> element in your Android manifest.

You must package the script with the native libraries of the app. If your app does not contain native libraries, add the lib directory manually to your project directory. For all architectures that your app supports, you must provide a copy of the wrap shell script under their respective native-library architecture directories.

The following project directory example shows the file layout to support both the x86 and ARMv8 architectures:

# App Directory
|- AndroidManifest.xml
|- …
|- lib
   |- x86
      |- ...
   |- arm64-v8a
      |- ...

Make sure that your script is executable (that is, its executable bit is set). For example:

$ ls -l lib/x86/
-rwxr-x--x 1 user user 0 Jan 01  1980 lib/x86/
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)