Why is Android Studio failing to build, with an AAPT2 error on images?

Joh*_*rry 7 image upgrade android-studio aapt2

After a recent update, Android Studio fails to build an unmodified project with errors along these lines:

> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade

  > AAPT2 aapt2-4.1.1-6503028-linux Daemon #0: Unexpected error during compile '/path/to/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png', attempting to stop daemon.
    This should not happen under normal circumstances, please file an issue if it does.
Run Code Online (Sandbox Code Playgroud)

Notice that it's failing on an image supplied by Android Studio itself!

What can I do to fix it?

Joh*_*rry 6

This seems to be an error in the newest (newer?) Gradle plugin(s) when it comes to PNG images; Google's IssueTracker lists several newly reported bugs along these lines. One of them mentions that it's a Gradle issue; I made mine work by reverting to a previous version: Gradle Plugin 4.0.1 and Gradle version 6.1.1 work fine.

It will be necessary to ignore AS' nagging about a newer version until this gets fixed.

[Added at the request of @YinOrYan]

To revert to an older version of Gradle:

  1. In the menu, select "Files", then "Project Structure..."
  2. 在弹出的对话框中,选择“项目”(我的版本中的第一项)。
  3. 您可以从下拉菜单中选择所需版本的 Gradle 插件和 Gradle。

这不应丢失任何系统调用,因为这与 SDK 或 API 版本不同。但是,它不是独立于它们的,因此您可以设置它们的位置可能有限制。

像往常一样,当您第一次构建时,或者每当您必须导入依赖项时,您都会希望在线,但否则应该可以离线工作。


Iza*_*ska 5

遗憾的是,这看起来像是旧架构的问题。虽然 Google 团队正在研究修复该问题,但与此同时您仍然可以使用新的 Android Studio 和 AGP,但暂时恢复使用旧版本的 AAPT2:

尝试在 gradle 缓存中找到旧的 aapt2 可执行文件,例如: ~/.gradle/caches/<subdirectories, hashes etc>/aapt2-4.1.0-alpha03-6246747-/aapt2(.exe)

获得路径后,您可以将其添加到 gradle.properties 文件中: android.aapt2FromMavenOverride=path/to/old/aapt2

为了使其更加健壮,您实际上可以将该可执行文件复制到非缓存位置,以防您清理缓存并且它消失。

希望这可以帮助!请密切关注问题跟踪器错误以获取更新。

  • 您不需要使用旧的Android Studio,只需更新AGP版本即可。如果您不想这样做,您可以使用构建工具中的 AAPT2:Android/sdk/build-tools/30.0.x/aapt2 (2认同)