Kotlin-android: unresolved reference databinding

2019-01-16 22:20发布

问题:

I have following fragment class written in Java using 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 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

回答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
}


回答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.0.1'

instead of

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

Thank you.



回答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



回答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



回答5:

Try this.Andrid studio 2.0(2.1)

In build.gradle

android{
   dataBinding {
        enabled = true
    }
...
}
dependencies {
 kapt 'com.android.databinding:compiler:2.0.0-rc1'
....
}

kapt {
    generateStubs = true
}

In my project: buildToolsVersion = "23.0.3"

in top level build.gradle

dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
    }


回答6:

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



回答7:

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...



回答8:

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



回答9:

Update from Android

The new compiler in version 3.2 is enabled by default.

Just update your Android Studio to V3.2. and remove all unnecessary config.

Just this

android {
    dataBinding {
        enabled = true
    }
}

No Need of below

  • Remove databinding.compiler

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

    kapt {
        generateStubs = true
    }
    
  • Remove apply plugin: 'kotlin-kapt' from top of gradle.

My complete config

build.gradle (Project)

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

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"

gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip


回答10:

Add Databinding in android Project using when you have use kotlin language.

Below steps

--First you need to add the below plugin

**apply plugin: 'kotlin-kapt'**

--Second dataBinding enabled true

**dataBinding {
        enabled = true
    }**

All this point added in app.build.gradle after hit "SYNC NOW"

Lets For example you have edit profile activity then how to define binding variable in kotlin??

lateinit var buildProfileBinding: ActivityBuildProfileBinding

buildProfileBinding = getBinding()

Here,get binding is method to handle which type of binding object

protected <T extends ViewDataBinding> T getBinding() {
                return (T) binding;
            }


回答11:

In my case, adding

kapt {
    generateStubs = true
}

Solved the problem for me. I ignored it the first time, I thought it's irrelevant to the problem:

Unresolved reference databinding

But now, data-binding is working just fine.



回答12:

this work for me in androidStudio ver3.1.3

apply plugin: 'kotlin-kapt'

dataBinding {
    enabled = true
}

show usage sample