Kotlin-android: unresolved reference databinding

后端 未结 16 924
借酒劲吻你
借酒劲吻你 2020-12-02 16:23

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

import com.example.app.databinding.FragmentDataBdinding;

public class Data         


        
相关标签:
16条回答
  • 2020-12-02 16:39

    To Solve the problem, you have to put

    apply plugin: 'kotlin-kapt'
    

    at the top of build.gradle (Module: app), then put this line in dependencies

    kapt "com.android.databinding:compiler:[YOUR_ANDROID_PLUGIN_VERSION]"
    

    You can find android plugin version by go to menu

    File > Project Structure > Project
    

    Then Sync Again. If you see this warning, ignore it.

    3rd-party Gradle plug-ins may be the cause

    0 讨论(0)
  • 2020-12-02 16:43

    Configuration data binding in kotlin

    build.gradle (folder app)

    apply plugin: 'kotlin-kapt'
    
    android {
       ...
       dataBinding {
          enabled = true
       }
    }
    
    dependencies {
       // data binding
       kapt "com.android.databinding:compiler:3.1.3"
    }
    

    Enjoy Kotlin...

    0 讨论(0)
  • 2020-12-02 16:46

    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

    0 讨论(0)
  • 2020-12-02 16:47

    Important Update

    You can see in documentation of Android.

    The new compiler in version 3.2 is enabled by default.

    So Just Update your Android Studio to V3.2 or newer. and remove all unnecessary config.

    So just enable dataBinding in app level build.gradle.

    android {
        dataBinding {
            enabled = true
        }
    }
    

    It will do all things for you automatically.

    You can SAFELY REMOVE below lines-

    • Remove databinding.compiler

      kapt 'com.android.databinding:compiler:3.0.1'
      
    • Remove kapt

      kapt {
          generateStubs = true
      }
      

    My complete config

    build.gradle (Project)

    kotlin_version = '1.2.71'    
    classpath 'com.android.tools.build:gradle:3.2.0'
    

    Use gradle latest version. Use kotlin latest version.

    build.gradle (App)

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    compileSdkVersion 28
    targetSdkVersion 28
    
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    

    Important Do not just copy and paste config. See this answer for setting up Kotlin version.

    gradle-wrapper.properties

    distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
    
    0 讨论(0)
  • 2020-12-02 16:47

    Add following in you app build.gradle

    kapt "com.android.databinding:compiler:$android_plugin_version"
    apply plugin: 'kotlin-kapt' // This one at top where plugin belong to
    

    This will do the trick.

    $android_plugin_version is version of com.android.tools.build:gradle in application build.gradle

    Also, add this to your module build.gradle

    android { /// Existing Code kapt { generateStubs = true } }

    0 讨论(0)
  • 2020-12-02 16:48

    In my case, the error was Unresolved reference: RegisterationUserBinding I just used my layout name fragment_registeration_user like this FragmentRegisterationUserBinding and made it in the Databinding layout and the error went away.

    0 讨论(0)
提交回复
热议问题