Java Kotlin-android:未解析的参考数据绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33165324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kotlin-android: unresolved reference databinding
提问by Mikhail
I have following fragment class written in Java using the new databinding library
我有以下使用新数据绑定库用 Java 编写的片段类
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
Error:(37, 27) Unresolved reference: FragmentDataBinding
错误:(16, 38) 未解析的引用:数据绑定
错误:(37, 27) 未解析的引用:FragmentDataBinding
How can I use android-databinding library with Kotlin?
如何在 Kotlin 中使用 android-databinding 库?
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
回答by Jaydeep Solanki
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'sa really simple hello world example project
更新:这是一个非常简单的 hello world 示例项目
回答by Garf1eld
Try this.Andrid studio 2.0(2.1)
试试这个。Andrid studio 2.0(2.1)
In build.gradle
在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"
在我的项目中: buildToolsVersion = "23.0.3"
in top level build.gradle
在顶级 build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
}
回答by lrampazzo
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
}
回答by Pratik Butani
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'
You can visit herefor new version
您可以访问此处获取新版本
Thank you.
谢谢你。
回答by Sijansd
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
回答by Shripal Shah
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;
}
回答by Mahmoud
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.
但是现在,数据绑定工作得很好。
回答by RKS
Add following in you 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_version
是com.android.tools.build:gradle
in 的版本application build.gradle
Also, add this to your module build.gradle
另外,将此添加到您的模块中 build.gradle
android {
/// Existing Code
kapt {
generateStubs = true
}
}
android {
/// Existing Code
kapt {
generateStubs = true
}
}
回答by Sowattana Sigen
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 插件可能是原因
回答by Jedsada Tiwongvorakul
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...
享受科特林...