简体   繁体   English

Kotlin-android:未解决的参考数据绑定

[英]Kotlin-android: unresolved reference databinding

I have following fragment class written in Java using the new databinding library我使用新的数据绑定库在 Java 中编写了以下片段 class

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:我试图在 Kotlin 中重写它并提出以下内容:

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:但是现在 step :app:compileDebugKotlin输出如下:

Error:(16, 38) Unresolved reference: databinding错误:(16, 38) 未解析的引用:数据绑定
Error:(37, 27) Unresolved reference: FragmentDataBinding错误:(37, 27) 未解析的引用:FragmentDataBinding

How can I use android-databinding library with Kotlin?如何将 android-databinding 库与 Kotlin 一起使用?

My top-level build.gradle :我的顶级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):我在应用程序目录中的build.gradle (仅相关部分):

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我正在使用 Android Studio 1.4,构建工具版本 23.0.1、Android SDK 23、SDK 工具 24.4.0

Try use this configuration:尝试使用此配置:

In main build.gradle :在主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 :应用程序build.gradle

android {
    dataBinding {
        enabled = true
    }
}

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

kapt {
    generateStubs = true
}

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.谢谢你。

Update 2: This is a really old answer, instead refer to lrampazzo's answer.更新 2:这是一个非常古老的答案,而是参考 lrampazzo 的答案。

It works with 1.0-rc4, put它适用于 1.0-rc4,把

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

into your dependencies进入你的依赖

Thanks Ghedeon, for the link in comments感谢 Ghedeon,提供评论中的链接

Update : Here's a really simple hello world example project更新这是一个非常简单的 hello world 示例项目

The version of Data Binding compiler is same as gradle version in your project build.gradle file:数据绑定编译器的版本与项目 build.gradle 文件中的 gradle 版本相同:

// 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和 gradle 版本是

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

Here is an example link to complete using of databinding library in kotlin.这是完成在 kotlin 中使用数据绑定库的示例链接。

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

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在 build.gradle (Module: app) 的顶部,然后将此行放在依赖项中

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

You can find android plugin version by go to menu您可以通过转到菜单找到 android 插件版本

File > Project Structure > Project

Then Sync Again.然后再次同步。 If you see this warning, ignore it.如果您看到此警告,请忽略它。

3rd-party Gradle plug-ins may be the cause第 3 方 Gradle 插件可能是原因

Try this.Andrid studio 2.0(2.1)试试这个。Andrid studio 2.0(2.1)

In build.gradlebuild.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"在我的项目中: buildToolsVersion = "23.0.3"

in top level build.gradle在顶级 build.gradle

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

this work for me in androidStudio ver3.1.3这在 androidStudio ver3.1.3 中对我有用

apply plugin: 'kotlin-kapt'

dataBinding {
    enabled = true
}

显示使用示例

In my case, this solved my problem.就我而言,这解决了我的问题。

I added this in the app build.gradle:我在 app build.gradle 中添加了这个:

buildFeatures {
    dataBinding true
    viewBinding true 
}

Configuration data binding in kotlin kotlin 中的配置数据绑定

build.gradle (folder app) build.gradle(文件夹应用程序)

apply plugin: 'kotlin-kapt'

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

dependencies {
   // data binding
   kapt "com.android.databinding:compiler:3.1.3"
}

Enjoy Kotlin...享受科特林...

Important Update重要更新

You can see in documentation of Android.您可以在Android 的文档中看到

The new compiler in version 3.2 is enabled by default.默认情况下启用 3.2 版中的新编译器。

So Just Update your Android Studio to V3.2 or newer .所以只需将您的Android Studio更新到 V3.2更新版本 and remove all unnecessary config.并删除所有不必要的配置。

So just enable dataBinding in app level build.gradle .所以只需在应用级build.gradle启用build.gradle

android {
    dataBinding {
        enabled = true
    }
}

It will do all things for you automatically.它会自动为你做所有的事情。

You can SAFELY REMOVE below lines-您可以安全地删除以下几行-

  • Remove databinding.compiler删除databinding.compiler

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

     kapt { generateStubs = true }

My complete config我的完整配置

build.gradle (Project) build.gradle(项目)

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

Use gradle latest version .使用gradle 最新版本 Use kotlin latest version .使用kotlin 最新版本

build.gradle (App) build.gradle(应用程序)

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.请参阅此答案以设置 Kotlin 版本。

gradle-wrapper.properties gradle-wrapper.properties

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

在我的例子中,错误是Unresolved reference: RegisterationUserBinding我只是使用我的布局名称fragment_registeration_user像这个FragmentRegisterationUserBinding并在数据绑定布局中使用它,错误消失了。

In the Gradle module file add this在 Gradle 模块文件中添加这个

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

To use databinding in Kotlin you need to configure your app to use data binding, enable the dataBinding build option in your build.gradle file in the .app module and you can replace your code with this:要在 Kotlin 中使用数据绑定,您需要将您的应用程序配置为使用数据绑定,在 .app 模块的 build.gradle 文件中启用 dataBinding 构建选项,您可以将代码替换为:

import com.example.app.databinding.FragmentDataBdinding

class ProfileFragment : Fragment() {

    private lateint var mBinding: FragmentProfileBinding

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data, container, false)

        return mBinding.getRoot()
    }
}

For more help check the databinding documentation link: https://developer.android.com/topic/libraries/data-binding .如需更多帮助,请查看数据绑定文档链接: https : //developer.android.com/topic/libraries/data-binding

Add following in you app build.gradle在您的app build.gradle添加以下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 $android_plugin_versionapplication build.gradlecom.android.tools.build:gradle版本

Also, add this to your module build.gradle另外,将此添加到您的模块build.gradle

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

尝试将此添加到您的gradle.properties

android.databinding.enableV2=true

Add Databinding in android Project using when you have use kotlin language.在使用 kotlin 语言时使用在 android 项目中添加数据绑定。

Below steps以下步骤

--First you need to add the below plugin --首先你需要添加以下插件

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

--Second dataBinding enabled true --启用第二个数据绑定 true

**dataBinding {
        enabled = true
    }**

All this point added in app.build.gradle after hit "SYNC NOW"点击“SYNC NOW”后,在 app.build.gradle 中添加了所有这些点

Lets For example you have edit profile activity then how to define binding variable in kotlin??例如,您有编辑配置文件活动,那么如何在 kotlin 中定义绑定变量?

lateinit var buildProfileBinding: ActivityBuildProfileBinding

buildProfileBinding = getBinding()

Here,get binding is method to handle which type of binding object这里,get binding 是处理哪种类型的绑定对象的方法

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

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.但是现在,数据绑定工作得很好。

I want to share my own expirience.我想分享我自己的经验。 To use data databinding with Kotlin in android studio is enough to add在 android studio 中使用 Kotlin 数据绑定就足够了

dataBinding {
    enabled = true
}

After that "sync it" and, it's very important, to "make Project".之后“同步它”,非常重要的是“制作项目”。 After that all your object will be active to use.之后,您的所有对象都将处于活动状态以供使用。

在尝试使用 FragmentDetailsBinding 之前,您必须确保通过将整个布局包装在 "" 标记父项中并将所有 xmlns 移动到布局标记然后构建项目,从而将相应的布局( fragment_details.xml )转换为数据绑定布局

In my case I only forgot to add in layout file the header:就我而言,我只是忘记在布局文件中添加标题:

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

....

If you have applied the following from the documentation :如果您已应用文档中的以下内容

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

In your module's gradle.properties and still get this error message, it's probably because of missing XML data tags .在您的模块的gradle.properties仍然收到此错误消息,可能是因为缺少 XML 数据标签

This is especially the case if you created a ListFragment through Android Studio's Fragment Gallery, one expects Android Studio would automatically convert the layout file for a binding adapter to a binding layout but it DOES NOT and it sure looks like a bug, not fixed as of version 4.2.2!如果您通过 Android Studio 的 Fragment Gallery 创建了ListFragment ,情况尤其如此,人们期望 Android Studio 会自动将绑定适配器的布局文件转换为绑定布局,但它不会,而且它确实看起来像一个错误,截至目前尚未修复版本 4.2.2!

Nevertheless here's the fix:不过这里是修复:

  1. Open up the corresponding XML file (in my case the compiler was nagging about AlarmItemBinding missing reference, so the XML would be alarm_item.xml打开相应的 XML 文件(在我的例子中,编译器一直在AlarmItemBinding缺少引用,所以 XML 将是alarm_item.xml
  2. Right click on the top-level tag (in my case <LinearLayout...> select Show Context Actions右键单击顶级标签(在我的例子中<LinearLayout...>选择Show Context Actions
  3. Select Convert to data binding layout选择Convert to data binding layout
  4. Select Build -> Clean project选择Build -> Clean project
  5. Select Build -> Rebuild project选择Build -> Rebuild project

In your Gradle module (app) file add在您的 Gradle 模块(app)文件中添加

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

BTW, keep in mind: Generated view binding name is based on name of your layout file, NOT on name of your class!顺便说一句,请记住:生成的视图绑定名称是基于布局文件的名称,而不是类的名称!

That means, if you have MainActivity class with corresponding activity_main.xml , the binding will be called ActivityMainBinding .这意味着,如果您有MainActivity class 和相应的activity_main.xml ,绑定将被称为ActivityMainBinding

Also, do not forget to initialize your binding variable by adding另外,不要忘记通过添加来初始化binding变量

binding = ActivityMainBinding.inflate(layoutInflater)

into your onCreate() method.进入你的onCreate()方法。

Do not forget to add this as well:不要忘记添加这个:

setContentView(binding.root)

instead of setContentView(R.layout.activity_main) (that would cause your listeners to not work at all)而不是setContentView(R.layout.activity_main) (这会导致您的听众根本无法工作)

Add below build features in your app build.gradle file在您的应用 build.gradle 文件中添加以下构建功能

android {... buildFeatures { viewBinding true } } android {... buildFeatures { viewBinding 真}}

Then in your activity onCreate method然后在您的活动 onCreate 方法中

binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root)绑定 = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM