How to fix "unexpected element <queries> found in <manifest>" error?

129,321

Solution 1

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.

Solution 2

I had this issue in flutter but i believe this solution will work for both flutter and native android dev.

Follow these steps

  1. Read this short blog to get some understanding: click here
  1. Delete the .gradle folder inside the android folder ie android>.gradle

  2. In the project build.gradle file, upgrade ur 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'

  3. Upgrade the distribution url too. Its 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

  4. 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.

Thanks.

Solution 3

I also had same issue on Android Studio 4.1.1, suddenly, 2 days ago. I solved the issue by upgrading build gradle version.

previous setting in build.gradle file of 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. :)

Solution 4

For Flutter

To understanding why this happens see the above @CommonsWare's answer

This is because <queries> tag was introduced with new package visibility options for Android 11 and upwards (SDK 30+). Because of that, you need to update your build.gradle with a version that includes this changes. Below is a list of supported gradle options.

The best solution to deal with these errors is to Upgrade your Android Gradle plugin and Update Gradle

You’ll have to update your Gradle version to one of the compatible versions that have the latest query tags (which were introduced with Android 11).

To Update the Android Gradle plugin

Specify the plugin version in the YourAppDirectory/android/build.gradle file. The plugin version applies to all modules built-in that Android Studio project. The following example sets the plugin to version 4.0.1:

buildscript {
    repositories {
        // Gradle 4.1 and higher include support for Google's Maven repo using
        // the google() method. And you need to include this repo to download
        // Android Gradle plugin 3.0.0 or higher.
        google()
        ...
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1’
    }

If the specified plugin version has not been downloaded, Gradle downloads it the next time you build your project or click Tools > Android > Sync Project with Gradle Files from the Android Studio menu bar.

To Update Gradle

Specify the Gradle version by editing the Gradle distribution reference in the YourAppDirectory/android/gradle/wrapper/gradle-wrapper.properties file.

Don't forget to update your ditributionUrl in your gradle-wrapper.properties as well. For example, for gradle 4.0.1, you should have:

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

We know that not everyone is ready to upgrade to the latest version, though, and you may be relying on old versions of Gradle or libraries that aren’t compatible with 4.1.

Solution 5

Fixing the error is very simple. 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

guys please attentions, for use queries you should write queries code in out of application tag not inside application tag

for more info see below photo

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

Share:
129,321

Related videos on Youtube

CommonsWare
Author by

CommonsWare

Mark Murphy, founder of CommonsWare, writes a lot. Books From 2008 through 2021, Mark wrote a series of books on Android app development. These books are now available for free download from the CommonsWare site. His most recent books have been focused on second-generation Android app development (Kotlin and the Android Jetpack): Elements of Android Jetpack, a new introductory book on Android app development, focused on second-generation techniques. Exploring Android, a series of step-by-step tutorials for building an Android app from scratch. This book demonstrates app development using Kotlin, coroutines, and the Jetpack libraries. Elements of Android Room, to help you incorporate Google's reactive object wrapper around SQLite. Elements of Android R, to help you deal with the changes introduced by Android 11 (code-named "R"). It covers everything from scoped storage changes to data access auditing to sharing UIs between apps to bubbles. Elements of Kotlin, a guide to the Kotlin programming language, which is rapidly gaining in popularity for Android app development. Elements of Kotlin Coroutines, exploring Kotlin's new first-class reactive programming system. In the beginning, Android development was focused on Java and, partially, on the Android Support Library. Mark's earliest books share that focus: The Busy Coder's Guide to Android Development, the first and largest book on Android app programming Android's Architecture Components GraphQL and Android Things Other Than Books He runs the jetc.dev weekly newsletter for Jetpack Compose. Heck, he even has a blog. All code in Stack Overflow questions, answers, or comments written by Mark Murphy are hereby licensed under the Apache Software License 2.0, unless otherwise noted where that code appears on Stack Overflow.

Updated on April 29, 2022

Comments

  • CommonsWare
    CommonsWare about 2 years

    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?

  • A.Alqadomi
    A.Alqadomi over 3 years
    "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.
  • Waseem
    Waseem over 3 years
    My build tool version was 3.5.0. I have upgraded to 3.5.4, it solved the issue. Good explanation
  • junmo choi
    junmo choi over 3 years
    Great! For Unity, edit the file in the path. -> /Unity/Hub/Editor/2019.4.20f1/Editor/Data/PlaybackEngines/An‌​droidPlayer/Tools/Gr‌​adleTemplates/basePr‌​ojectTemplate.gradle
  • Carlos Martínez
    Carlos Martínez about 3 years
    This works in react native too, very useful
  • NitZRobotKoder
    NitZRobotKoder about 3 years
    classpath 'com.android.tools.build:gradle:4.1.2' distributionUrl=https\://services.gradle.org/distributions/g‌​radle-6.8.2-all.zip I still get issue how to fix?
  • NitZRobotKoder
    NitZRobotKoder about 3 years
    classpath 'com.android.tools.build:gradle:4.1.2' distributionUrl=https\://services.gradle.org/distributions/g‌​radle-6.8.2-all.zip I still get issue how to fix?
  • NitZRobotKoder
    NitZRobotKoder about 3 years
    classpath 'com.android.tools.build:gradle:4.1.2' distributionUrl=https\://services.gradle.org/distributions/g‌​radle-6.8.2-all.zip I still get issue how to fix?
  • Bukunmi
    Bukunmi about 3 years
    Did you invalidate caches after the updates?
  • Moeed Ahmed
    Moeed Ahmed about 3 years
    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'
  • ChiragMS
    ChiragMS about 3 years
    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!
  • john7ric
    john7ric about 3 years
    Life saver. Thank you!!. this answer has the reason why and solution.
  • JoKr
    JoKr about 3 years
    After I updated I get a different error from Flutter which suggest downgrading lol: issuetracker.google.com/issues/158060799
  • Tushar Gogna
    Tushar Gogna almost 3 years
    getting dexGuard issue with this: supported versions include [2.3.0:3.5.*]
  • Vladimir Salguero
    Vladimir Salguero almost 3 years
    I solved the problem with your answer. I have Android Studio 4.2.1 Thanks!
  • kobowo
    kobowo almost 3 years
    Can confirm this works for Android Studio 4.2.1 and Flutter 2 (I migrated from 1.22.6)
  • minchaej
    minchaej almost 3 years
    best solution ever :)
  • siripan
    siripan almost 3 years
    Having problem with flutter camera plugin recently, this solution worked like flawlessly. Thank you.
  • Koger
    Koger over 2 years
    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
    Koger over 2 years
    Updating Gradle Plugin to 3.5.4 fixed the problem.
  • Dorian
    Dorian over 2 years
    you can find a list of releases of the gradle plugin here developer.android.com/studio/releases/gradle-plugin
  • Mike Casan Ballester
    Mike Casan Ballester over 2 years
    VERY IMPORTANT guys please attentions, for use queries you should write queries code in out of application tag not inside application tag
  • Abdullah
    Abdullah over 2 years
    I faced the same problem in React Native with react-native-inappbrowser-reborn. This solution solved my problem
  • J. M.
    J. M. over 2 years
    Could have used full photo, wheres other application tag
  • J. M.
    J. M. over 2 years
    What if your not using gradle?
  • CommonsWare
    CommonsWare over 2 years
    @J.M.: I have no way to answer that, sorry.
  • J. M.
    J. M. over 2 years
    Thats alright thanks 4 the reply
  • HJo
    HJo over 2 years
    any reason why you don't bump up your grade to latest? flutter by default currently using 4.1.0 in their starter projects so I guess I'll go with that