12

Data binding setup:

apply plugin: 'kotlin-kapt'

android {
    dataBinding {
        enabled = true
    }
}

dependencies {
    kapt "com.android.databinding:compiler:3.1.0"
}

The fragment class which uses data binding:

class LandingFragment : Fragment(), Injectable {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
        val dataBinding = LandingFragmentBinding.inflate(inflater, container, false)
        return dataBinding.root
    }
}

Every time the Espresso test is run for this fragment, I get the following exception:

java.lang.NoClassDefFoundError: android.databinding.DataBinderMapperImpl
at android.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:32)
at com.sc.databinding.LandingFragmentBinding.inflate(LandingFragmentBinding.java:42)
at com.sc.ui.landing.LandingFragment.onCreateView(LandingFragment.kt:32)
...
makovkastar
  • 5,000
  • 2
  • 30
  • 50

6 Answers6

27

A bit late, but I resolved this issue by adding DataBinding compiler with kapt as a test dependency:

kaptAndroidTest 'androidx.databinding:databinding-compiler:3.3.2'

Or the version not from AndroidX if your project is not using Jetpack yet.

Rafael Toledo
  • 5,599
  • 6
  • 23
  • 33
  • I found this after one-hour searching. Made my day. Thanks. – Sotti Jul 19 '19 at 15:21
  • 1
    Why is this necessary? What does it do? If this doesn't seem to fix it, try cleaning your build directory. I'm not sure why that works either. – Big McLargeHuge Oct 25 '19 at 23:21
  • 1
    Dude, do you use PayPal or something? :D – Rajkiran Dec 10 '19 at 12:17
  • I hit this error when applying this solution, anyone has idea? org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction – Amos Aug 05 '22 at 04:02
2

Add

kaptTest "androidx.databinding:databinding-compiler:+"

to dependencies on build.gradle files of all your modules.

Osman Yalın
  • 620
  • 7
  • 7
0

I run into this very error. I did 2 things: 1. Added kaptAndroidTest 'androidx.databinding:databinding-compiler:3.5.1' in gradle 2. Used the databinding, that is to say, I create a fake bool variable and injected it for real in a view. It would seem that you cannot just use databinding for retrieving the views instead of issuing the dreaded findViewById. You have to use it at least once in your module. Once you use it you are fine for all the other classes in your module.

0

I have the same this issue, and fixed by adding

kapt {
    generateStubs = true
}

in build.gradle app (all module if using dataBinding)

apply plugin: 'kotlin-kapt'

android {
...
    dataBinding {
        enabled = true
    }
}

kapt {
    generateStubs = true
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ...
    implementation "androidx.core:core-ktx:+"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    kapt "com.android.databinding:compiler:$android_plugin_version"

}

In build.gradle project

buildscript {
    ext.kotlin_version = '1.3.70'
    ext.android_plugin_version = '3.5.2'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath "com.android.tools.build:gradle:$android_plugin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
N.LanLuu
  • 51
  • 6
0

Add dataBinding = true to android { } in your build.gradle file, and everything will be fine, hope this help..

build.gradle:

android {

    // skip ..

    buildFeatures {
        //noinspection DataBindingWithoutKapt
        dataBinding = true
        viewBinding true
    }


    // skip ..
}
Howard Chen
  • 229
  • 3
  • 8
-2

Try to add the android-apt plugin as per this stackoverflow answer:

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
Sandip Fichadiya
  • 3,430
  • 2
  • 21
  • 47
DAIRAV
  • 723
  • 1
  • 9
  • 31
  • 2
    I use kapt annotation processor (you can see it from my Gradle config). Why do I need the third party annotation processor? – makovkastar Apr 04 '18 at 15:26