14

I have this code and I was told that for Android 11 I need to add the queries tag in manifest:

    final PackageManager pm = getPackageManager();
    Intent main = new Intent(Intent.ACTION_MAIN, null);
    main.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> packages = pm.queryIntentActivities(main, 0); //get a list of installed apps.

I was told it should be this:

  <queries>
    <intent>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent>

  </queries>

Problem is I cannot add the <category> tag. Android Studio says that the element is not allowed there. I can only add the <action> and <data> tags inside <intent>. And it seems other people on SO have this problem too. It's strange because Android's own documentation instructs us to use the <category> tag there.

  • 1
    https://developer.android.com/training/package-visibility/declaring There is no mention of `category`. You may have confused `queries` with `intent-filter`. – Eugen Pechanec Apr 11 '21 at 10:02
  • Here they show the use cases of the `` tag, and they use `` :) https://developer.android.com/training/package-visibility/use-cases –  Apr 11 '21 at 10:35
  • Ignore Android Studio for the moment. Does having `` work? If so, this is an IDE bug: https://issuetracker.google.com/issues/174787530. – CommonsWare Apr 11 '21 at 11:19
  • How could a trillion dollar company not test whether a major feature works or not? –  Apr 11 '21 at 11:22
  • @CommonsWare I tested `resolveActivity()` (from the question yesterday) on Android 11 without a corresponding `` tag and it didn't return null, which I guess means it doesn't require the `` tag? Someone said that implicit intents don't need the queries tag. –  Apr 11 '21 at 12:43
  • @CommonsWare or perhaps some apps are visible regardless whether there's a `` tag? –  Apr 11 '21 at 12:57
  • Package visibility only kicks in when your `targetSdkVersion` hits 30 or higher. And personally I never use `resolveActivity()` -- my package visibility testing has always been with other `PackageManager` methods, such as `queryIntentActivities()`. – CommonsWare Apr 11 '21 at 13:46
  • @CommonsWare I didn't get an answer yesterday btw. You said I should add the `` and `` tags for `resolveActivity()` but `` doesn't seem to be an allowed element either. –  Apr 11 '21 at 14:47
  • @CommonsWare this was the question: https://stackoverflow.com/questions/67039092/what-queries-tags-should-be-added-for-these-methods?noredirect=1#comment118498310_67039092 –  Apr 11 '21 at 14:49
  • @CommonsWare I've been stuck on this trivial issue for 2 days why can't I get an answer? –  Apr 11 '21 at 16:20
  • @CommonsWare also I think you say here https://commonsware.com/blog/2020/04/05/android-r-package-visibility-holes.html that adding `ACTION_MAIN` and `CATEGORY_LAUNCHER` in a `` tag in manifest makes all apps visible so no other `` tag is necessary for `resolveActivity()`? –  Apr 11 '21 at 16:31

1 Answers1

16

The <queries> section is introduced in API 30, so be sure you use it only if you target such version upward.

It is indeed needed when, for instance, you want to launch some other apps from yours, knowing only their package name. Otherwise, using the package manager will return you some null when using functions like packageManager.getLaunchIntentForPackage("com.yyy"). You can find more information here: Package visibility in Android 11

In your case, you can just write the section like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx">

    ...

    <queries>
        <intent>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent>
    </queries>

    ...

</manifest>
Mereo4
  • 485
  • 5
  • 10
  • 'Be sure you use it only if you target such version upward' What if for instance I have some users who would use API 28 and others who would use API 30, how would you apply `` to them only? – Marco Fregoso Dec 28 '21 at 23:14
  • 1
    What I meant is for the `targetSdkVersion` of your gradle file. The `` section will be used the same as other features introduced starting at specific API versions: it will be used when the app is launched on Android versions supporting it, else it's just ignored. – Mereo4 Jan 02 '22 at 10:55
  • Can we have more than 1 queries tag in manifest file? – Muhammad Rafeh Atique Feb 01 '22 at 04:33
  • @Muhammad Rafeh Atique: I don't know, but as it is directly at the root of the `manifest`object, I don't see the point to have more than one. It's a global configuration for your app. If you have multiple manifest files, I imagine their `queries` section should merge as other configurations do. – Mereo4 Feb 02 '22 at 08:41