Wrap Shell Script

When debugging and profiling apps with native code, it’s often useful to use debugging tools that need to be enabled at process startup. This requires that you run your app in a fresh process rather than cloning from the zygote. Examples include:

Using the wrap shell script

Using wrap.sh is easy:

  1. Compile a custom debuggable APK that packages the following:
  2. Install the debuggable APK on a device.
  3. Launch the app.

Creating the wrap shell script

When you launch a debuggable APK that contains wrap.sh, the system executes the script and passes the command to start the app as arguments. The script is responsible for starting the app, but can make any environment or argument changes. The script should follow MirBSD Korn shell (mksh) syntax.

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

#!/system/bin/sh
exec "$@"

To use malloc debug via wrap.sh, you would include the following line:

#!/system/bin/sh
LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper "$@"

Packaging wrap.sh

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

It's also necessary to set android:extractNativeLibs=true in <application>. This is the default, but if you explicitly set it to false your wrap.sh script will not work.

You must package the wrap.sh 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 each architecture that your app supports, you must provide a copy of the wrap shell script under that native library directory.

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

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