others-how to solve 'AAPT: error: unexpected element <queries> found in <manifest>' when building android app with gradle?

1. Purpose

In this post, I would demo how to solve the below error when we building an app using gradle in android studio.

/Users/bswen/.gradle/caches/transforms-2/files-2.1/867fe3024d95eb1a682619fb9100c3e9/play-services-ads-lite-20.1.0/AndroidManifest.xml:27:5-43:15: AAPT: error: unexpected element <queries> found in <manifest>.

image-20210510202414771

2. Environment

  • Android Studio 3.5
  • Java 1.8

3. The solution

3.1 What does this error mean?

In short, we are using a new dependency ,which is not compatible with our old android gradle plugin.

According to CommonsWare’s answer:

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

See this Android Developers Blog post for more.

3.2 The gradle plugin that caused this error

This is the old buildscript configurations in our build.gradle file:

buildscript {

repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.5.0'
}

3.3 The gradle plugin that fix this error

This is the new buildscript configurations that fix the error:

buildscript {

repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.5.4'
}

3.4 What is android gradle plugin?

The Android Studio build system is based on Gradle, and the Android Gradle plugin adds several features that are specific to building Android apps. Although the Android plugin is typically updated in lock-step with Android Studio, the plugin (and the rest of the Gradle system) can run independent of Android Studio and be updated separately.

Just as the following picture shows, tha android gradle plugin is used to compile app and its dependencies to binaries and bundle them to apk file.

image-20210510205118529

3.5 What’s AAPT?

AAPT2 (Android Asset Packaging Tool) is a build tool that Android Studio and Android Gradle Plugin use to compile and package your app’s resources. AAPT2 parses, indexes, and compiles the resources into a binary format that is optimized for the Android platform.

Android Gradle Plugin 3.0.0 and higher enable AAPT2 by default, and you typically won’t need to invoke aapt2 yourself. However, if you prefer to use your terminal and your own build system over Android Studio, you can use AAPT2 from the command line. You can also debug build errors related to AAPT2 from the command line. To do so, you can find AAPT2 as a standalone tool in Android SDK Build Tools 26.0.2 and higher.

Just as the below picture shows, the AAPT is responsible for compiling the app resource to R.java and binary resouces.

image-20210510205409459

Now it works!

4. Summary

In this post, I demonstrated how to solve the AAPT: error: unexpected element found in , you should pay attention to the aapt part.

-->