224

All of a sudden, I am getting this build error in my Android project:

unexpected element <queries> found in <manifest>

How do I fix it?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491

7 Answers7

395

The Android Gradle Plugin needs to know about new manifest elements, particularly for the manifest merger process. The plugin has a tendency to get confused if it sees elements in the manifest merger that it does not recognize, tossing out build errors like the one in the question.

In this case, Android 11 introduced <queries> as a manifest element, and older versions of the Android Gradle Plugin do not know about that element.

The fact that this occurs from manifest merger means that simply upgrading a dependency might bring about this error. For example, if you upgrade to the latest version of com.awesome:awesome-library, and it contained a <queries> element in its manifest, you might crash with the aforementioned error in your builds, even without any other changes in your code.

Google released a series of patch versions of the Android Gradle Plugin to address this:

  • 3.3.3
  • 3.4.3
  • 3.5.4
  • 3.6.4
  • 4.0.1

If you are using an existing plugin in the 3.3.* through 4.0.* series, upgrade to the associated patch version (or higher) from that list, and you should no longer run into that error (e.g., classpath 'com.android.tools.build:gradle:4.0.1').

If you are using Android Studio 4.1 or higher, with a matching Android Gradle Plugin (e.g., in the 4.1.* series), you should be fine without any changes. Those plugin versions were already aware of <queries>.

See this Android Developers Blog post for more.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 4
    "Google released a series of patch versions of the Android Gradle Plugin to address this" Very nice note if you don't want to go through fixing issues related to upgrading the gradle plugin version. – A.Alqadomi Dec 14 '20 at 12:19
  • 1
    My build tool version was 3.5.0. I have upgraded to 3.5.4, it solved the issue. Good explanation – Waseem Jan 25 '21 at 17:33
  • 1
    Great! For Unity, edit the file in the path. -> /Unity/Hub/Editor/2019.4.20f1/Editor/Data/PlaybackEngines/AndroidPlayer/Tools/GradleTemplates/baseProjectTemplate.gradle – junmo choi Feb 19 '21 at 03:10
  • classpath 'com.android.tools.build:gradle:4.1.2' distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip I still get issue how to fix? – NitZRobotKoder Mar 09 '21 at 08:16
  • 1
    A great explanation.. in my case I got a fix by slightly upgrading gradle version on Android Studio v3.5.2 from: classpath 'com.android.tools.build:gradle:3.5.1' to: classpath 'com.android.tools.build:gradle:3.5.4' – Moeed Ahmed Apr 15 '21 at 18:23
  • 1
    Oh god! This so clears things up for me. I've been wandering around for a week now trying to figure out wth is wrong with that queries tag!! I'm getting this error in Unity. I'm gonna upgrade to the latest version that uses a newer version of Gradle and see if it fixes it. Thanks so much man! God bless you! – ChiragMS Apr 27 '21 at 17:48
  • Unfortunately I am experiencing this problem even when using seemingly patched gradle 3.4.3. Problem started occuring after updating play-services-ads to 20.3.0. – Koger Sep 14 '21 at 09:07
  • Updating Gradle Plugin to 3.5.4 fixed the problem. – Koger Sep 14 '21 at 09:23
  • you can find a list of releases of the gradle plugin here https://developer.android.com/studio/releases/gradle-plugin – Dorian Sep 18 '21 at 09:58
  • What if your not using gradle? – J. M. Feb 13 '22 at 15:50
  • @J.M.: I have no way to answer that, sorry. – CommonsWare Feb 13 '22 at 15:57
  • Also be careful where you put the , if you accidentally nest them under in your manifest, you'll see a similar-looking error – ObjectNameDisplay Nov 04 '22 at 15:29
100

I had this issue in Flutter, but I believe this solution will work for both Flutter and native Flutter development.

Follow these steps

  1. Read this short blog post to get some understanding: Preparing your Gradle build for package visibility in Android 11

  2. Delete the .gradle folder inside the Android folder, i.e., android > .gradle

  3. In the project build.gradle file, upgrade your class path appropriately based on the blog in the link above, e.g., I upgraded to classpath 'com.android.tools.build:gradle:4.0.1'

  4. Upgrade the distribution URL too. It's in android>gradle>gradle-wrapper.properties file appropriately. E.g., I upgraded it to distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

  5. You can invalidate caches and restart your Android Studio. Make sure you have a good Internet connection, because it will download the new Gradle files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bukunmi
  • 2,504
  • 1
  • 18
  • 15
82

I also suddenly had the same issue two days ago in Android Studio 4.1.1. I solved the issue by upgrading the build Gradle version.

Previous setting in the build.gradle file of the project was:

classpath("com.android.tools.build:gradle:3.5.3")

Current setting:

classpath("com.android.tools.build:gradle:3.5.4")

The issue was gone immediately. :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hitesh Prajapati
  • 1,261
  • 10
  • 11
71

Update your Android Studio to the last version and use the last stable Gradle plugin version. At the current time, I use Android Studio version 4.1.3 with Gradle Plugin 6.8.2

For use in queries, you should write queries code in out of application tag, not inside application tag.

For more information, see the photo below:

https://i.stack.imgur.com/ks0AY.jpg

16

I had this error in the react-native-image-crop-picker library, and I solved this problem by updating the Gradle version as mentioned in previous answers.

It was:

classpath("com.android.tools.build:gradle:3.5.3")

Updated to:

classpath("com.android.tools.build:gradle:3.5.4")

And I ran a:

cd android && ./gradlew clean && cd .. && npx react-native run-android

Then it worked OK.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

Due to the new default settings and features for package visibility in Android 11 that need to add <queries>, you must update your Android Gradle plugin.

Google has added some patches to current versions listed in Android Gradle plugin release notes, 4.0.0 (April 2020).

If you want to use a newer version of Android Gradle, you should search for a compatible wrapper from Android Gradle plugin release notes, Update Gradle.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahmoud Mabrok
  • 1,362
  • 16
  • 24
0

Update your Gradle version to 4.0.1 or later.

File android/gradle/wrapper/gradle-wrapper.properties: update the distribution URL to:

distributionUrl=https://services.gradle.org/distributions/gradle-6.7-all.zip

File android/build.gradle: update the Gradle plugin:

classpath 'com.android.tools.build:gradle:4.1.2'

to 4.0.1 or later. Here it is 4.1.2 with Gradle version to 6.5 or later.

You can see the distribution chart at How to update gradle in android studio?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
akash maurya
  • 607
  • 9
  • 16