Kotlin-android: unresolved reference databinding

79,236

Solution 1

Try use this configuration:

In main build.gradle:

buildscript {
    ext.kotlin_version = '<kotlin-version>'
    ext.android_plugin_version = '2.2.0-alpha4'
    dependencies {
        classpath "com.android.tools.build:gradle:$android_plugin_version"
    //... rest of the content
    }
}

App build.gradle:

android {
    dataBinding {
        enabled = true
    }
}

dependencies {
    kapt "com.android.databinding:compiler:$android_plugin_version"
}

kapt {
    generateStubs = true
}

Solution 2

I found new solution, hope it will helps you.

First of all check whether plugin applied:

apply plugin: 'kotlin-kapt'

then

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

You might have an error in dependency:

USE

kapt 'com.android.databinding:compiler:3.1.4'

instead of

compile 'com.android.databinding:compiler:3.1.4'

You can visit here for new version

Thank you.

And use layout tags in layout file before using its binding class , you can easily do that : In layout file your root view (e.g.- Constraint Layout) ALT+Enter -> convert to data binding class

Solution 3

Update 2: This is a really old answer, instead refer to lrampazzo's answer.

It works with 1.0-rc4, put

kapt 'com.android.databinding:compiler:1.0-rc4' 

into your dependencies

Thanks Ghedeon, for the link in comments

Update: Here's a really simple hello world example project

Solution 4

The version of Data Binding compiler is same as gradle version in your project build.gradle file:

// at the top of file 
apply plugin: 'kotlin-kapt'


android {
  dataBinding.enabled = true
}

dependencies {
  kapt "com.android.databinding:compiler:3.0.0-beta1"
}

and the gradle version is

classpath 'com.android.tools.build:gradle:3.0.0-beta1'

Here is an example link to complete using of databinding library in kotlin.

https://proandroiddev.com/modern-android-development-with-kotlin-september-2017-part-1-f976483f7bd6

Solution 5

In my case, this solved my problem.

I added this in the app build.gradle:

buildFeatures {
    dataBinding true
    viewBinding true 
}
Share:
79,236

Related videos on Youtube

Mikhail
Author by

Mikhail

Updated on February 16, 2022

Comments

  • Mikhail
    Mikhail over 2 years

    I have following fragment class written in Java using the new databinding library

    import com.example.app.databinding.FragmentDataBdinding;
    
    public class DataFragment extends Fragment {
    
        @Nullable
        private FragmentDataBinding mBinding;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data, container, false);
            return mBinding.getRoot();
        }
    }
    

    It compiles and runs fine.
    I tried to rewrite it in Kotlin and came up with the following:

    import com.example.app.databinding.FragmentDataBdinding
    
    class ProfileFragment : Fragment() {
    
        private var mBinding: FragmentDataBinding? = null
    
        override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data, container, false)
            return mBinding!!.getRoot()
        }
    }
    

    But now step :app:compileDebugKotlin outputs the following:

    Error:(16, 38) Unresolved reference: databinding
    Error:(37, 27) Unresolved reference: FragmentDataBinding

    How can I use android-databinding library with Kotlin?

    My top-level build.gradle:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.3.0'
            classpath 'com.android.databinding:dataBinder:1.0-rc4'
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    

    My build.gradle in app dir (only relevant parts):

    apply plugin: 'com.android.application'
    apply plugin: 'com.android.databinding'
    apply plugin: 'kotlin-android'
    
    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    }
    
    buildscript {
        ext.kotlin_version = '0.14.451'
        repositories {
            mavenCentral()
        }
    
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
        }
    }
    repositories {
        mavenCentral()
        maven {
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
    }
    

    I'm using Android Studio 1.4, Build tools version 23.0.1, Android SDK 23, SDK tools 24.4.0

    • Ghedeon
      Ghedeon over 8 years
      Please, feel free to vote for the corresponding issue: https://youtrack.jetbrains.com/issue/KT-8007
    • Hesam
      Hesam almost 6 years
      You only need to have apply plugin: 'kotlin-kapt' in app gradle file in most recent version.
    • Dino Sunny
      Dino Sunny about 4 years
      private lateinit var binding : FragmentDataBinding Is a better way of initializing
  • Miguel Fermin
    Miguel Fermin over 8 years
    I still get the same error, am i doing something wrong here? gist.github.com/xdgimf/820c433efa8307821788
  • Jaydeep Solanki
    Jaydeep Solanki over 8 years
    here's my build.gradle file if you need a reference gist.github.com/jaydeep17/9960fdb0e5a1ba85e82d
  • Mikhail
    Mikhail over 8 years
    @Jaydeep Have just tried it, and it's working. However Android studio doesn't highlight syntax and generates lots of IDE errors with message Exception while analyzing expression at (12,23) in /projectpath/KotlinDataBinding/app/src/main/java/com/example‌​/kotlindatabinding/M‌​ainActivity.kt:. Do you have the same errors? And what's your android studio version?
  • Jaydeep Solanki
    Jaydeep Solanki over 8 years
    @Arkadiy It's already been reported youtrack.jetbrains.com/issue/KT-8007 BTW I'm on studio v1.5 Preview 2
  • Avinash R
    Avinash R almost 8 years
    @Irampazzo, 2.2.0-alphaX is available, and FYI the version of data-binding is same as that of android-gradle-plugin itself. so you can externalize that version and use it as the compiler's version.
  • Avinash R
    Avinash R almost 8 years
    you should use the same version for both the compiler and the android build tools gradle plugin. Otherwise it can cause unpredictable build issues.
  • Avinash R
    Avinash R almost 8 years
    This is no longer the latest version of the plugin. It'll cause unnecessary pain.
  • AplusKminus
    AplusKminus over 7 years
    It didn't work for me without the kapt { generateStubs = true } in there as well. Please update your answer. Also note of the latest comment by Avinash R: 1.0-rc4 is no longer the latest version of the plugin.
  • Rabie Jradi
    Rabie Jradi over 7 years
    I can confirm that this answer fixed my issue with databinding and kotlin
  • arekolek
    arekolek about 7 years
    I had to add apply plugin: 'kotlin-kapt' also. See stackoverflow.com/a/42974558/1916449
  • Andrii Kovalchuk
    Andrii Kovalchuk about 7 years
    I had to add apply plugin: 'kotlin-kapt'
  • Andrii Kovalchuk
    Andrii Kovalchuk about 7 years
    I had to add apply plugin: 'kotlin-kapt'
  • leoOrion
    leoOrion about 7 years
    I tried this. Now it gives me a circular dependency error. Any idea about this ?
  • Mladen Rakonjac
    Mladen Rakonjac almost 7 years
    I still have this problem :(
  • Brian
    Brian almost 7 years
    Check out kotlinlang.org/docs/tutorials/… for official documentation on this.
  • Peter
    Peter almost 7 years
    correct one, and dont forget to add :) repositories { maven { url "maven.google.com" } }
  • HBG
    HBG almost 7 years
    I had tried the above and it only worked when restarting Android Studio. Worth trying if you're still not having any luck.
  • Les
    Les almost 7 years
    worked for me except my version is 3.0.0-beta2, be sure to set your version correctly, an sync gradle
  • Shripal Shah
    Shripal Shah over 6 years
    DataBinding Librabry kapt 'com.android.databinding:compiler:2.3.1'
  • Sergio
    Sergio about 6 years
    This is working for me (gradle wrapper 4.4, tools 3.1.0)
  • Duran Jayson
    Duran Jayson about 6 years
    Adding kapt 'com.android.databinding:compiler:3.0.1' works for me
  • Subhan Ali
    Subhan Ali almost 6 years
    It worked for me by adding only "apply plugin: 'kotlin-kapt". Thanks.
  • Mohamed ElSawaf
    Mohamed ElSawaf over 5 years
    just added this line is working for me (apply plugin: 'kotlin-kapt') Android Studio 3.2.1 - Kotlin plugin 1.3.0
  • Rahul Rastogi
    Rahul Rastogi over 5 years
    apply plugin: 'kotlin-kapt' is really important in Kotlin.
  • naqi
    naqi about 5 years
    My project is still on gradle 3.0.1 release so this didn't work for me.
  • Khemraj Sharma
    Khemraj Sharma about 5 years
    You can update your Android Studio and gradle to achieve more stability.
  • Cesar Castro
    Cesar Castro about 5 years
    dataBinding.enabled = true was what I was forgetting
  • Ajay
    Ajay over 4 years
    You are great! I spent a lot of time to apply multiple suggestions to resolve the issue but helpless till found this answer. Changing the binding according to fragment layout name resolves the issue. Thanks a lot.
  • EL TEGANI MOHAMED HAMAD GABIR
    EL TEGANI MOHAMED HAMAD GABIR over 4 years
    glad it helped you
  • sud007
    sud007 over 4 years
    Voted up for the direction mentioning this one as an 'Old answer'. Thoughtful and Helpful!
  • IgorGanapolsky
    IgorGanapolsky over 4 years
    com.android.databinding:compiler is not needed
  • James Jordan Taylor
    James Jordan Taylor over 2 years
    The last piece was what worked for me, specifically: "And use layout tags in layout file before using its binding class , you can easily do that : In layout file your root view (e.g.- Constraint Layout) ALT+Enter -> convert to data binding class". After doing that I was given the option to import the binding in the code-behind Kotlin class.
  • Swapnil Kale
    Swapnil Kale about 2 years
    "viewBinding true " worked for me.. Thanks