72

I'm getting an error in DataBindingMapperImpl.java for one specific data binding which results in the following error when building the project.

ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1.
ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1

ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1
ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1
/Users/casper/Documents/ARCore/Name/app/build/generated/source/kapt/nameDebug/com/company/name/DataBinderMapperImpl.java:10: error: cannot find symbol

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.

followed by the error message below...

I followed the similar post here which resulted in this, which is the end of the error message above.

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)

I have also tried

  1. Clean Project and then Rebuild project
  2. File -> Invalidate Caches / Restart
  3. Turn Android Studio on and off

The layout file connected to the data binding looks like this

<?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">

Solution

The error was caused by a mistake. I did set visibility by

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

and forgot to import

<data>
    <import type="android.view.View"/>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Casper Lindberg
  • 1,053
  • 1
  • 11
  • 16
  • 4
    Please post your Solution as an Answer to help people with this issue as this is how SO tends to work. – Bink May 05 '20 at 18:21

14 Answers14

70

Disclaimer:

The fix below is intended to solve a specific problem with some dependencies conflict, mostly databinding issues can cause this error but are only a consequence of wrong XML or code and the solution below will not work in this case. Double check your XML/code correctness before trying the below solution.

This is a known problem with some databinding versions (which is embedded in Android Studio) and other dependencies like Room which import different versions of org.antlr:antlr4 library.

UPDATE: 12/06/2020 (dd/MM/yyyy)

If you use Room, updating to Room 2.3.0-alpha01 or above should remove the error because they have fixed the problem here: https://issuetracker.google.com/issues/150106190


Put this configuration in the 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")
    }
}

If you still have problems, you can try using the 4.5.3 version above instead of 4.7.1 to downgrade the library

Reference

MatPag
  • 41,742
  • 14
  • 105
  • 114
  • 2
    Thanks for the replay MatPag but it neither of the options work unfortunately. – Casper Lindberg Mar 26 '20 at 11:02
  • 2
    Really strange :/ Double check your XML file, you probably have some error somewhere that is blocking Binding class generation. Start trying removing binding elements from the XML (elements in data too) – MatPag Mar 26 '20 at 11:13
  • Can you help me with .kts (kotlin) version of those commands? Can't make it work. Thanks. – GuilhE Mar 26 '20 at 13:48
  • 2
    @GuilhE Updated the answer adding the Kotlin DSL version – MatPag Mar 26 '20 at 14:05
  • Also, in my case what solved was changing a custom `BindingAdapter` that was receiving "T?". I had to change to "T? = null" to make it work (I can remove the `resolutionStrategy` now) :) – GuilhE Mar 26 '20 at 14:47
  • 1
    I've added a small disclaimer to my answer, it was intended to solve a specific problem. Thank you for the the comment :) – MatPag Mar 26 '20 at 14:52
  • 2
    Thanks a lot for your answer. I updated my question with the solution. – Casper Lindberg Mar 27 '20 at 04:51
  • I think this problem will be 99% related with `databinding` errors. Here's another one: https://stackoverflow.com/questions/60907059/compiler-error-when-applying-theme-with-databinding – GuilhE Mar 28 '20 at 21:24
  • 1
    upgrading room did the job to me. didn't even had binding stuff in my XML... tricky one ! Thanks – agonist_ Aug 12 '20 at 09:14
  • The only answer that deserves looking at is this. All other answers here solve specific problems caused by general lack of skill and/or awareness on the developers' end, and are unrelated to the cause of the problem explained in the question. Worst part is, they either unload the entire content of their files or give you a useless one-liner. – Ace Jan 22 '21 at 16:17
  • 1
    It's worth mentioning that simply upgrading room version to 2.3.0-alpha01 or higher should solve this problem if you didn't mess something else up. – Ace Jan 22 '21 at 16:19
7

In my project, this error was caused by an incorrect query in one of my Room DAOs. Fixing the query removed the error.

It's unfortunate the error message doesn't clearly indicate the source of the error.

Big McLargeHuge
  • 14,841
  • 10
  • 80
  • 108
2

I faced the same issue while i was updating my entity/model class by just adding

private int isSale = 0;

Issue was i did not create setter getter for above attribute so Room Database was confused and throw compile time exception i.e. ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1.

Arihant Jain
  • 131
  • 1
  • 7
1

This was a very frustrating problem for me to solve. As this error was covering up the real problem. And as mentioned by @MatPag above, this indeed is not specific to some problem, it could be anything.

After hours of trying anything that I could find on the web, I had the idea to check the generated files in the Android Project View sidebar, and by chance I noticed some errors in the dataBinding generated files. The way to solve this is to understand those errors and fix them in the XML files. For me it was some conflict in variable names.

hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
1

I got the same error, in my case it was caused due to wrong imports in xml layout.I refactored and changed my package name but that didn't changed the same inside xml files.It showed me the same error.I went through all the fragments,activities and layouts to check if there was any wrong imports/missing imports.After clearing all the import and variable issues, the build was successful.

<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>
Abraham Mathew
  • 2,029
  • 3
  • 21
  • 42
  • Can you add space after "." (full stop) and after ", " (comma)? See also [run-on](https://en.wikipedia.org/wiki/Sentence_clause_structure#Run-on_sentences) [sentences](https://twitter.com/PeterMortensen/status/1199839973215739907) and [infinitive (base) form of the verb with a helper verb, not past tense](https://www.youtube.com/watch?v=1Dax90QyXgI&t=9m14s). – Peter Mortensen Aug 26 '20 at 13:57
1

For me, this issue popped up when I accidentally used a binding xmlns in a Textview that was actually mapped to an adapter that would work with 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>

This is bound to a function here:

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)
        }
    }


}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ansh sachdeva
  • 1,220
  • 1
  • 15
  • 32
  • 1
    Took me hours to find this was the problem, thanks ! I wish the data binding build errors weren't as occult. – Gregory Nowik Aug 23 '20 at 23:31
  • The syntax highlighting for the XML is off (near "`) is not allowed inside an XML tag (in other words, it is not valid XML). – Peter Mortensen Aug 26 '20 at 12:55
  • @PeterMortensen Yes I intentionally did so to grab attention at a common XML mistake. By the way, Thank you for all your edits (on this answer as well as the others) – ansh sachdeva Aug 26 '20 at 21:25
1

My issue was caused because I'd changed data models that were used inside the <variable /> tag in XML file and did not change XML file itself.

Hope it will help somebody

Pavlo Zoria
  • 666
  • 7
  • 7
0

The error showed up due to upgrading com.google.android.material:material from 1.1.0-alpha09 to 1.3.0-alpha01

nyxee
  • 2,773
  • 26
  • 22
0

In my case, all I had to do was add the optional Kotlin extensions and coroutines support for room dependency:

implementation "androidx.room:room-ktx:$version_room"
MrBE
  • 103
  • 2
  • 10
0

The solution that works for me was to exclude the module org.antlr:antlr4-runtime on room dependencies as follows:

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'
}

In build.gradle. This solution is cleanest instead of using resolutionStrategy block.

aldajo92
  • 181
  • 1
  • 5
0

If someone came here for an answer and didn't find it yet, here is my case. The problem was that i didn't specify the requireAll parameter, turns out it must be set explicitly. So here's my code:

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

Also note that you can set default value for attributes like above.

the korovay
  • 563
  • 4
  • 16
0

For me, I used same namespace for bind & tools. It should be different.

<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">
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
0

In my case the issue was caused by a wrong name package in the "nav_graph.xml" file, in the "argument" tag, "app:argType" String.

Lucas
  • 458
  • 4
  • 6
0

In my case, the issue occurred when I added a new field in a Room Entity file. However, once I removed this field, the problem disappeared.

Ruslan
  • 11
  • 2
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34476032) – amycodes Jun 05 '23 at 17:40