Package visibility in Android 11

Package visibility in Android 11

In Android 10 and earlier versions, apps can get a list of all installed apps on the device through methods such as queryIntentActivities(). In most cases, this access permission far exceeds the permissions that the app actually needs. As we continue to increase our focus on privacy protection, we will introduce some new changes in Android 11 to change the way apps query and interact with users' installed apps. To achieve this goal, we have brought better access control to the list of apps installed on a specific device.

[[339193]]

To better "hold accountable" for access to installed apps, apps targeting Android 11 (target API level 30) will only be able to detect some of the filtered installed apps by default. If you want to get more information about the list of installed apps, you need to add elements to the Android manifest in your app to expand the scope of access.

In most common scenarios, including any intents started with startActivity(), you do not need to make any changes. Other scenarios, such as opening a specific third-party application directly from your application's interface, require developers to explicitly declare the application's package name or intent filter signature, as shown below:

  • ...
  • Most common scenarios
  • https://developer.android.google.cn/preview/privacy/package-visibility#use-cases-not-affected
  • Other scenarios
  • https://developer.android.google.cn/preview/privacy/package-visibility-use-cases
  • Intent filter signature
  • https://developer.android.google.cn/preview/privacy/package-visibility#intent-signature

If you use Custom Tab to open URL links, you may call resolveActivity() and queryIntentActivities() to launch a non-browser app (provided that you have an app installed to handle the URL). In Android 11, there is a better way to handle this: use the FLAG_ACTIVITY_REQUIRE_NON_BROWSER flag of the intent instead of querying other apps. If a browser is launched when you call startActivity() with this flag, an ActivityNotFoundException will be thrown. At this time, your app can handle this exception and use Custom Tab to open the URL link instead.

  1. try {
  2. val intent = Intent(ACTION_VIEW, Uri.parse(url)).apply {
  3. // Non-browser applications will handle the URL directly (by default)
  4. // The user can also select a non-browser application in the disambiguation dialog
  5.  
  6. addCategory(CATEGORY_BROWSABLE)
  7. flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_REQUIRE_NON_BROWSER
  8. }
  9.  
  10. startActivity(intent)
  11. } catch (e: ActivityNotFoundException) {
  12. // Only browser applications can be used, or the browser is used by default to handle the intent.
  13. }
  • Custom Tab
  • https://developers.google.cn/web/android/custom-tabs
  • A better way
  • https://developer.android.google.cn/preview/privacy/package-visibility-use-cases#avoid-a-disambiguation-dialog
  • FLAG_ACTIVITY_REQUIRE_NON_BROWSER
  • https://developer.android.google.cn/preview/privacy/package-visibility#web-intent-non-browser-app

In rare cases, your app might need to query or interact with all installed apps on a device, regardless of the components they contain. To allow your app to see all other installed apps, Android 11 introduces the QUERY_ALL_PACKAGES permission. In an upcoming policy update, Google Play will provide guidance for apps that require the QUERY_ALL_PACKAGES permission. You can add elements to your app by setting your API Level to 30 and using Android Studio 3.2+ and the latest Android Gradle plugin. You can find more usage information and use cases for package availability in the developer documentation — Package visibility in Android 11.

  • QUERY_ALL_PACKAGEShttps://developer.android.google.cn/preview/privacy/package-visibility#all-apps
  • Developer documentation — Package visibility in Android 11https://developer.android.google.com/preview/privacy/package-visibility

Android Studio and Gradle support for this feature

If you are using Android Gradle plugin version 4.1 and above, you can use the new element normally, because the old version of the Gradle plugin is not compatible with this element. If you use or depend on a library or SDK that supports Android 11, it may cause manifest conflicts and errors in merging manifests. For example, when building an app, you may see the following error in the Build Output Window:

  1. Android resource linking failed
  2. /Users/sample/AndroidStudioProjects/MyApp/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml:18: error: unexpected element <queries> found in <manifest>

There may also be an error message like this in the Build Output Window, directing you to view the Manifest merger logs:

  1. Manifest merger failed with multiple errors, see logs

When you expand the Merged Manifest view, an additional error message appears:

  1. Error: Missing 'package'   key attribute on element package

Merged Manifest View

  • https://developer.android.google.cn/studio/build/manifest-merge#inspect_the_merged_manifest_and_find_conflicts

Fix issues with Android Gradle plugin

The best way to resolve the above errors is to upgrade the Android Gradle plugin to version 4.1 Beta. However, not all developers can use the latest version, and some projects may rely on older versions of Gradle or code libraries that are incompatible with the 4.1 version of the Android Gradle plugin. Therefore, we recently released a minor version (dot releases) upgrade for the Android Gradle plugin to be compatible with the <queries> element:

  • Android Gradle plugin upgraded to version 4.1 Beta https://developer.android.google.cn/studio/releases/gradle-plugin#updating-plugin
  • Upgrade of dot releases https://developer.android.google.cn/studio/releases/gradle-plugin#4-0-0

For example, if you are using version 4.0.0 of the Android Gradle plugin, you can upgrade the relevant dependencies to the corresponding versions in the project-level build.gradle file.

  1. buildscript {
  2.  
  3. repositories {
  4. google()
  5. jcenter()
  6. }
  7.  
  8. dependencies {
  9. // classpath 'com.android.tools.build:gradle:4.0.0'  
  10. classpath 'com.android.tools.build:gradle:4.0.1'  
  11. }
  12. }

<<:  Android phones are reaping the benefits of high refresh rates, so why is the iPhone 12 still absent?

>>:  Scoping in Android and Hilt

Recommend

WeChat: You must answer this call, it may save you tens of thousands of yuan

Internet fraud has become a social cancer. Whethe...

User operation, how to achieve a breakthrough from 0 to 1?

Users, as one of the most important factors in pr...

WeChat long press for 2 seconds has so many hidden functions. Learn more

[[321342]] According to Tencent's 2019 annual...

Marketing promotion: Why is product promotion ineffective?

When users are faced with product marketing that ...

Analysis of Internet promotion channels!

Our marketing channels are located where there ar...

3 practical methods of user fission growth!

Let me share with you how to achieve fission grow...

Zhengda Anatomy [Daily explanation of muscle pain in the whole body]

Zhengda Anatomy [Daily Explanation of Muscle Pain...

Subway access to Alipay: NFC phones pass through gates directly

Starting today, all self-service ticket machines ...

4 carefully selected WeChat mini-programs to make your phone omnipotent

Here we share 4 carefully selected WeChat applets...