【问题标题】:ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1用于代码生成的 ANTLR Tool 版本 4.5.3 与当前运行时版本 4.7.1 不匹配
【发布时间】:2020-07-06 22:46:42
【问题描述】:

我在 DataBindingMapperImpl.java 中遇到一个特定数据绑定的错误,这会在构建项目时导致以下错误。

用于代码生成的 ANTLR 工具版本 4.5.3 与当前运行时版本 4.7.1 不匹配。
用于解析器编译的 ANTLR 运行时版本 4.5.3 与当前运行时版本 4.7.1 不匹配

用于代码生成的 ANTLR 工具版本 4.5.3 与当前运行时版本 4.7.1 不匹配
用于解析器编译的 ANTLR 运行时版本 4.5.3 与当前运行时版本 4.7.1 不匹配
/Users/casper/Documents/ARCore/Name/app/build/generated/source/kapt/nameDebug/com/company/name/DataBinderMapperImpl.java:10:错误:找不到符号

import com.company.name.databinding.ActivitySplashScreenBindingImpl;

                                                ^

symbol:   class ActivitySplashScreenBindingImpl

> Task :app:kaptNameDebugKotlin FAILED
> Task :app:mergeExtDexNameDebug
location: package com.company.name.databinding
FAILURE: Build failed with an exception.

接着是下面的错误信息...

我关注了类似的帖子here 导致了这个,这是上面错误消息的结尾。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptNameDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)

我也试过

  1. 清理项目,然后重建项目
  2. 文件 -> 使缓存无效/重新启动
  3. 打开和关闭 Android Studio

连接到数据绑定的布局文件是这样的

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable
        name="viewmodel"
        type="com.company.name.ui.splashScreen.viewModel.SplashScreenViewModel"/>
    <variable
        name="tryAgainBtnHandler"
        type="com.company.name.ui.splashScreen.viewModel.interfaces.TryAgainBtnHandler"/>
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.splashScreen.view.SplashScreenActivity">

解决方案

错误是由错误引起的。我确实设置了可见性

android:visibility="@{viewmodel.errorContainerVisible ? View.VISIBLE : View.GONE}"

忘记导入

<data>
    <import type="android.view.View"/>

【问题讨论】:

  • 请将您的解决方案作为答案发布,以帮助人们解决此问题,因为这就是 SO 的工作方式。

标签: android android-studio kotlin data-binding antlr4


【解决方案1】:

免责声明:

以下修复旨在解决某些特定问题 依赖项冲突,主要是数据绑定问题可能导致此错误,但 仅是错误的 XML 或代码的结果,并且 在这种情况下,下面的解决方案将不起作用。在尝试以下解决方案之前,请仔细检查您的 XML/代码的正确性。

这是一些数据绑定版本(嵌入在Android Studio 中)和其他依赖项(如Room)的已知问题,它们导入不同版本的org.antlr:antlr4 库。

更新:2020 年 12 月 6 日(日/月/年)

如果您使用 Room,更新到 Room 2.3.0-alpha01 或更高版本应该会消除错误,因为他们已在此处修复了问题:https://issuetracker.google.com/issues/150106190


把这个配置放到app里build.gradle

//groovy
configurations.all {
    resolutionStrategy.force "org.antlr:antlr4-runtime:4.7.1"
    resolutionStrategy.force "org.antlr:antlr4-tool:4.7.1"
}

//kotlin DSL
configurations.all {
    resolutionStrategy {
        force("org.antlr:antlr4-runtime:4.7.1")
        force("org.antlr:antlr4-tool:4.7.1")
    }
}

如果还是有问题,可以尝试使用上面的4.5.3版本 而不是4.7.1 来降级库

Reference

【讨论】:

  • 感谢重播 MatPag,但不幸的是,这两个选项都不起作用。
  • 真的很奇怪:/ 仔细检查您的 XML 文件,您可能在某处有一些错误阻止了绑定类的生成。开始尝试从 XML 中删除绑定元素(数据中的元素)
  • @GuilhE 更新了答案,添加了 Kotlin DSL 版本
  • 我在回答中添加了一个小免责声明,旨在解决特定问题。谢谢你的评论:)
  • 非常感谢您的回答。我用解决方案更新了我的问题。
【解决方案2】:

在我的项目中,此错误是由我的一个 Room DAO 中的不正确查询引起的。修复查询消除了错误。

很遗憾,错误消息没有明确指出错误的来源。

【讨论】:

    【解决方案3】:

    我在通过添加更新实体/模型类时遇到了同样的问题

    private int isSale = 0;
    

    问题是我没有为上述属性创建 setter getter,因此 Room 数据库被混淆并引发编译时异常,即用于代码生成的 ANTLR 工具版本 4.5.3 与当前运行时版本 4.7 不匹配.1.

    【讨论】:

    • 啊!节省我的一天时间
    【解决方案4】:

    这对我来说是一个非常令人沮丧的问题。因为这个错误掩盖了真正的问题。正如上面@MatPag 所提到的,这确实不是特定于某个问题,它可能是任何问题。

    在尝试了我可以在网络上找到的任何东西几个小时后,我有了检查 Android Project View sidebar 中生成的文件的想法,偶然发现dataBinding 生成的文件中有一些错误。解决此问题的方法是了解这些错误并在 XML 文件中修复它们。对我来说,变量名有些冲突。

    【讨论】:

      【解决方案5】:

      对我来说,当我不小心在 Textview 中使用了绑定 xmlns 时出现了这个问题,该绑定实际上映射到了可以与 ImageView 一起使用的适配器。

      <?xml version="1.0" encoding="utf-8"?>
      
      <layout xmlns:binding="http://schemas.android.com/apk/res-auto">
      
          <data>
              <variable
                  name="pokemonObj"
                  type="in.curioustools.architectures.models.Pokemon" />
          </data>
      
          <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="wrap_content"
              android:layout_margin="4dp"
              android:background="@drawable/bg_rect_curve_8_solid_light_fffef2"
              android:gravity="center"
              android:orientation="vertical"
      
              android:padding="8dp"
              tools:ignore="ContentDescription">
      
              <ImageView
                  android:id="@+id/eachrow_iv_pokemon"
                  android:layout_width="80dp"
                  android:layout_height="80dp"
                  android:layout_gravity="center"
                  android:background="@drawable/bg_circle_white_fff"
                  android:padding="4dp"
                  android:scaleType="fitXY"
                  binding:url="@{pokemonObj.imageUrl}"
      
                  tools:src="@android:drawable/ic_menu_camera" />
      
              <TextView
                  android:id="@+id/eachrow_tv_pokemon_name"
                  android:layout_width="150dp"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_margin="8dp"
                  android:fontFamily="@font/roboto_slab_bold"
                  android:gravity="center"
                  android:textColor="@color/black"
                  android:textSize="16sp"
                  binding:url="@{pokemonObj.name}" <!-- WRONG -->
      
                  tools:background="#eee"
                  tools:text="Pikachu" />
      
          </LinearLayout>
      </layout>
      

      这里绑定了一个函数:

      public class AllBindingAdapters {
          // All are bound to the xmlns:binding schema
      
          companion object {
      
              @JvmStatic
              @BindingAdapter(" binding:url")
              public fun bindImage(view: ImageView, receivedUrl: String?) {
                  GlideAnimatedLoader.loadImage(view, receivedUrl)
              }
          }
      
      
      }
      

      【讨论】:

      • 花了我几个小时才发现这是问题所在,谢谢!我希望数据绑定构建错误没有那么神秘。
      • XML 的语法突出显示已关闭(“<!-- WRONG -->) 不允许在 XML 标记内(换句话说,它不是有效的 XML) .
      • @PeterMortensen 是的,我故意这样做是为了引起人们对常见 XML 错误的注意。顺便说一句,感谢您的所有编辑(关于此答案以及其他答案)
      【解决方案6】:

      我遇到了同样的错误,在我的情况下,这是由于 xml 布局中的错误导入引起的。我重构并更改了我的包名称,但在 xml 文件中并没有改变相同的错误。它向我显示了同样的错误。我遍历所有片段、活动和布局,检查是否有任何错误的导入/缺少导入。清除所有导入和变量问题后,构建成功。

      <data class="MainActivityBinding">
      
          <variable
              name="vm"
              type="com.abcd.efg.home.MapViewModel" />
      </data>
      
      <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/drawer_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          tools:openDrawer="start">
      
          <androidx.constraintlayout.widget.ConstraintLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent">
      
              <com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:app="http://schemas.android.com/apk/res-auto"
                  android:id="@+id/includeAppBar"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:theme="@style/AppTheme.AppBarOverlay"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintStart_toStartOf="parent"
                  app:layout_constraintTop_toTopOf="parent">
      
                  <FrameLayout
                      android:id="@+id/toolbar_container"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content">
      
                      <androidx.appcompat.widget.Toolbar
                          android:id="@+id/toolbar"
                          android:layout_width="match_parent"
                          android:layout_height="?attr/actionBarSize"
                          android:background="@color/colorPrimary"
                          app:popupTheme="@style/AppTheme.PopupOverlay"
                          app:title="@string/app_name"
                          app:titleTextColor="@color/white" />
                  </FrameLayout>
      
              </com.google.android.material.appbar.AppBarLayout>
      
              <fragment
                  android:id="@+id/navigation"
                  android:name="androidx.navigation.fragment.NavHostFragment"
                  android:layout_width="0dp"
                  android:layout_height="0dp"
                  app:defaultNavHost="true"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintStart_toStartOf="parent"
                  app:layout_constraintTop_toBottomOf="@id/includeAppBar"
                  app:navGraph="@navigation/nav_main" />
      
          </androidx.constraintlayout.widget.ConstraintLayout>
      
          <com.google.android.material.navigation.NavigationView
              android:id="@+id/sideNV"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_gravity="start"
              android:background="@color/drawerBackground"
              android:fitsSystemWindows="true"
              android:shadowColor="@color/white"
              app:headerLayout="@layout/nav_header_home"
              app:menu="@menu/activity_main_drawer" />
      
      </androidx.drawerlayout.widget.DrawerLayout>
      

      【讨论】:

      【解决方案7】:

      我的问题是因为我更改了 XML 文件中 &lt;variable /&gt; 标记内使用的数据模型,而没有更改 XML 文件本身。

      希望对大家有所帮助

      【讨论】:

        【解决方案8】:

        由于将 com.google.android.material:material1.1.0-alpha09 升级到 1.3.0-alpha01 而出现错误

        【讨论】:

          【解决方案9】:

          就我而言,我所要做的就是添加可选的 Kotlin 扩展和协程支持以支持 room 依赖项:

          implementation "androidx.room:room-ktx:$version_room"
          

          【讨论】:

            【解决方案10】:

            对我有用的解决方案是在房间依赖项上排除模块 org.antlr:antlr4-runtime,如下所示:

            implementation ("androidx.room:room-ktx:$version_room"){
                exclude group: 'org.antlr', module: 'antlr4-runtime'
            }
            kapt("androidx.room:room-compiler:$room_version") {
                exclude group: 'org.antlr', module: 'antlr4-runtime'
            }
            

            build.gradle。这个解决方案是最干净的,而不是使用resolutionStrategy 块。

            【讨论】:

              【解决方案11】:

              如果有人来这里寻求答案但还没有找到,这就是我的情况。问题是我没有指定requireAll 参数,结果必须明确设置。所以这是我的代码:

              @BindingAdapter("firstAttr", "secondAttr", requireAll = false)
              fun View.yourFunction(firstAttr: Boolean, secondAttr: Boolean = false) { ... }
              

              另外请注意,您可以为上述属性设置默认值。

              【讨论】:

                【解决方案12】:

                对我来说,我为绑定和工具使用了相同的命名空间。应该不一样。

                <layout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:bind="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools">
                

                【讨论】:

                  猜你喜欢
                  • 2020-06-22
                  • 2015-12-22
                  • 1970-01-01
                  • 2015-07-07
                  • 1970-01-01
                  • 2018-06-07
                  • 2012-03-16
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多