Keywords: Android | Gradle | Manifest | Queries | Build Error
Abstract: This article delves into the causes of the Android build error 'unexpected element <queries> found in <manifest>', focusing on incompatibility issues with the Android Gradle Plugin version. It provides solutions such as upgrading to specific versions (e.g., 3.3.3, 3.5.4) and includes additional steps like clearing caches and updating the Gradle wrapper, based on the best answer and supplementary references, to help developers quickly fix errors and optimize build processes.
In Android development, build errors often hinder project progress. Recently, many developers have encountered a specific error: unexpected element <queries> found in <manifest>. This error typically triggers during the build process and is closely related to how the Android Gradle Plugin handles manifest files.
Analysis of the Error Cause
This error message indicates that the Android Gradle Plugin encountered an unexpected XML element <queries> within the <manifest> section. Introduced in Android 11 for package visibility, this element is not recognized by older plugin versions. The core issue lies in the manifest merger process, where an outdated plugin fails to parse the element, resulting in a build error. For instance, when a dependency library is updated to include the <queries> element, this error can occur even without code changes.
Solution: Upgrading the Android Gradle Plugin
The key to fixing this error is to upgrade the Android Gradle Plugin to a version that supports the <queries> element. Google has released a series of patch versions: 3.3.3, 3.4.3, 3.5.4, 3.6.4, and 4.0.1. If the current plugin version is within the 3.3.* to 4.0.* range, it should be upgraded to the corresponding patch version or higher. For users of Android Studio 4.1 and above, the matching plugin versions (e.g., the 4.1.* series) already have built-in support and require no additional adjustments. The upgrade method involves updating the classpath in the project-level build.gradle file, for example: classpath 'com.android.tools.build:gradle:4.0.1'.
Supplementary Steps and Best Practices
Based on supplementary answers, it is recommended to perform the following steps to ensure a smooth build: First, delete the .gradle folder in the Android project to clear caches; second, update the Gradle wrapper's distribution URL (located in the gradle-wrapper.properties file), such as to distributionUrl=https://services.gradle.org/distributions/gradle-6.1.1-all.zip; finally, execute the "Invalidate Caches and Restart" operation in Android Studio. These steps help resolve residual issues that may arise after upgrading the plugin and optimize the build environment.
Summary and Outlook
By upgrading the Android Gradle Plugin to compatible versions and incorporating auxiliary measures like cache clearing, developers can effectively resolve build errors related to <queries>. As the Android ecosystem evolves, timely updates to toolchains and adherence to official guidelines (e.g., referencing the Android Developers Blog) are crucial for maintaining project stability. In the future, similar errors may recur with the introduction of new APIs, so it is advisable to regularly check plugin versions and dependency compatibility.