Android Developers
Published in

Android Developers

Illustration by Molly Hensley

New Android 11 tools to make apps more private and stable

Contributors: Jing Ji and Ziv Snai

As part of our continuing effort to help you build high quality apps, we often look for areas where we can invest in tools and resources that give you better insights into the performance of your apps.

The big news

In Android 11, we are introducing two new tools, Data Access Audit APIs and Process Exit Reasons, to give you more transparency into your private data access and causes of process exits.

Data Access Auditing APIs

Android encourages developers to be considerate and intentional with their sensitive data access. In Android 11 you will have access to new APIs that will give you more transparency into the usage of private and protected data. This could be useful, for example, for large apps that may have legacy code and those that use third-party libraries or SDKs.

It may not be easy for large apps to attribute private data usage.

The first API is a callback allowing apps to back trace the use of data protected by runtime permissions to the code that triggered the usage. To be notified, any app can set a callback in AppOpsManager which will be invoked each time a section of code uses private data, such as getting location updates. You can create specific logic to track, ingest, and analyze the data.

Data access auditing API in Android 11 helps back track private data usage to your code.

The second API is targeted at complex apps with multiple features. A social app might have a find friends feature and a photo tagging feature. Each of the features use a subset of sensitive data, for example find friends uses location and contacts and photos tag uses location, contacts and camera. In Android 11 you can create a new Context object that allows you to attribute a subset of your app’s code to one or more features. So going forward, every permission usage would be traced to the features associated with the context.

You can also see these APIs in action in our code sample.

In addition to helping you identify private data access, Android 11 also includes new APIs to help pinpoint those not-always-trivial crash issues in the field.

Process Exit Reasons

You also tell us that it can be difficult to track down the cause of your apps getting terminated, which can be due to various reasons: an ANR, a crash, or the user choosing to force stop the app. To help diagnose the cause, some developers are adding customized code in order to build their own analytics to improve the app’s health.

With that in mind, we found a way to make the diagnosis part easier for you.

Android 11 introduces a new ActivityManager API to report historical information related to an app process’s termination.

Your app can use the API to retrieve any available historical process exit diagnostic information, such as whether a process termination is due to ANRs, memory issues, or other reasons.

If the app was terminated due to ANR, the ApplicationExitInfo.getTraceInputStream() will return an InputStream to the stack trace dump of the app prior to the termination. This is especially helpful on newer OS versions, where there’s additional complexity for pulling ANR traces due to privacy and security considerations. After reading from the InputStream, just remember to close it in order to avoid resource leaks.

Additionally, you can use the new ActivityManager.setProcessStateSummary() method to store custom state information. This is a useful way to save arbitrary process data to debug a section of your code that is causing your app to crash. For some developers, knowing what the app’s state prior to the process’s termination is also vital — for example, a game developer might want to know what the current game level the user was on before the process is terminated — a common way is to persist it to storage constantly and read it in the next app launch. Note the size of the input data is very limited. Any saved process state information can be retrieved via the ApplicationExitInfo.getProcessStateSummary() method.

Resources

We hope you find these additional tools helpful in making your apps more privacy-aware and stable. To learn more, check out our developer documentation on Data Access Auditing and Process Exit Reasons APIs.

Articles on modern tools and resources to help you build experiences that people love, faster and easier, across every Android device.

Recommended from Medium

illustration with a gigantic lock and key
Why Developers Should use Flutter in 2020 for App Development
A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sara N-Marandi