【问题标题】:AAPT2 error in Android Studio 3.0.1Android Studio 3.0.1 中的 AAPT2 错误
【发布时间】:2018-07-09 18:18:59
【问题描述】:

我正在尝试使用 Android Studio 3.0.1 运行“hello world”应用程序并获得以下 AAPT2 错误输出:

Error:(16) error: not well-formed (invalid token).
Error:(16) not well-formed (invalid token).
Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Execution failed for task ':app:mergeDebugResources'.
> Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details...

我无法找到解决方案,有人可以帮我吗?

【问题讨论】:

标签: android aapt2


【解决方案1】:

来自 AAPT2 的“格式不正确”错误意味着您的一个 XML 文件格式不正确,可能缺少右括号或类似内容。在错误上方,它应该说明它来自哪个文件。看看你的 res/ 目录和里面的文件。

【讨论】:

    【解决方案2】:

    在您的gradle.properties 中添加此行android.enableAapt2=false

    【讨论】:

    • AAPT1 将很快被弃用,因此在没有充分理由的情况下禁用 AAPT2 真的不是最好的选择。就像在这种情况下,它只是一个格式错误的 XML 文件,很可能 AAPT1 也无法解析。
    • 这只是为了隐藏真正的问题。
    【解决方案3】:

    禁用 Instant Run,它可能对您有用。对我来说它有效

    【讨论】:

      【解决方案4】:

      就我而言,解决方案有点棘手和有趣。我有一个RelativeLayout 和一个TextView 和一个ProgressBarProgressbar 位于 TextView 之上,如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:weightSum="1"
          tools:context="com.caoa.yakokoe.yakokoe.ui.splash.SplashActivity">
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="0.5">
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:contentDescription="@string/content_desc_logo_green"
                  android:src="@drawable/logo_green" />
          </RelativeLayout>
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="0.5">
      
              <ProgressBar
                  android:id="@+id/splash_progress_bar"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_above="@id/splash_text"
                  android:layout_centerHorizontal="true"
                  android:indeterminate="true"
                  android:visibility="gone" />
      
              <TextView
                  android:id="@+id/splash_text"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_marginBottom="24dp"
                  android:layout_marginTop="16dp"
                  android:text="@string/splash_text_default"
                  android:textAlignment="center"
                  android:visibility="gone" />
          </RelativeLayout>
      </LinearLayout>
      

      这引发了一种错误(忘记了它是什么,但它在“找不到 layout_above 的 id”的行中)。

      解决方案是简单地翻转 ProgressBarTextView 位置,如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:weightSum="1"
          tools:context="com.caoa.yakokoe.yakokoe.ui.splash.SplashActivity">
      
          <!-- Content here -->
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="0.5">
      
              <TextView
                  android:id="@+id/splash_text"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_marginBottom="24dp"
                  android:layout_marginTop="16dp"
                  android:text="@string/splash_text_default"
                  android:textAlignment="center"
                  android:visibility="gone" />
      
              <ProgressBar
                  android:id="@+id/splash_progress_bar"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_above="@id/splash_text"
                  android:layout_centerHorizontal="true"
                  android:indeterminate="true"
                  android:visibility="gone" />
          </RelativeLayout>
      </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        android.enableAapt2=false 不要执行此步骤来暂时隐藏问题。 Aapt1 即将被弃用,Aapt2 必须在 2018 年底之前使用。

        这只是 gradle 构建工具的问题。只需更新您的 gradle 和 gradle 工具即可。

        我在项目级 gradle 的依赖标记中使用 classpath 'com.android.tools.build:gradle:3.3.0-alpha02'' 并且我使用的是 gradle 版本 4.8 。这为我解决了这个问题。

        附加 禁用 Instant run,如果这没有为您解决问题

        【讨论】:

          【解决方案6】:

          我有同样的问题,我改变了

          compileSdkVersion 22 到 compileSdkVersion 25

          targetSdkVersion 22 到 targetSdkVersion 25

          实现 'com.android.support:appcompat-v7:22' 到 实现 'com.android.support:appcompat-v7:25'

          这解决了我的问题。

          【讨论】:

            【解决方案7】:

            这是个老问题,似乎没有人提供找到 AAPt2 错误的正确方法

            AAPT2 错误:查看日志了解详情

            每种情况都会有所不同。

            所以找到正确的错误你应该运行 assembelDebug 正如here 中所建议的那样

            参考下图运行assembleDebug。

            在我的情况下,实际上损坏了 png 文件,并且仅在发布构建时失败。所以我必须运行 assembleRelese 才能找到该问题。

            【讨论】:

              【解决方案8】:

              这对我有帮助。在 build.gradle(Module:app) 中添加这些

              defaultConfig {
              aaptOptions.cruncherEnabled = false
              aaptOptions.useNewCruncher = false
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-05-06
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-03-23
                • 2019-01-23
                相关资源
                最近更新 更多