Kotlin-android: unresolved reference databinding
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
}
Related videos on Youtube
Mikhail
Updated on February 16, 2022Comments
-
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: FragmentDataBindingHow 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 over 8 yearsPlease, feel free to vote for the corresponding issue: https://youtrack.jetbrains.com/issue/KT-8007
-
Hesam almost 6 yearsYou only need to have
apply plugin: 'kotlin-kapt'
in app gradle file in most recent version. -
Dino Sunny about 4 yearsprivate lateinit var binding : FragmentDataBinding Is a better way of initializing
-
-
Miguel Fermin over 8 yearsI still get the same error, am i doing something wrong here? gist.github.com/xdgimf/820c433efa8307821788
-
Jaydeep Solanki over 8 yearshere's my build.gradle file if you need a reference gist.github.com/jaydeep17/9960fdb0e5a1ba85e82d
-
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/MainActivity.kt:
. Do you have the same errors? And what's your android studio version? -
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 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 almost 8 yearsyou 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 almost 8 yearsThis is no longer the latest version of the plugin. It'll cause unnecessary pain.
-
AplusKminus over 7 yearsIt 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 over 7 yearsI can confirm that this answer fixed my issue with databinding and kotlin
-
arekolek about 7 yearsI had to add
apply plugin: 'kotlin-kapt'
also. See stackoverflow.com/a/42974558/1916449 -
Andrii Kovalchuk about 7 yearsI had to add apply plugin: 'kotlin-kapt'
-
Andrii Kovalchuk about 7 yearsI had to add apply plugin: 'kotlin-kapt'
-
leoOrion about 7 yearsI tried this. Now it gives me a circular dependency error. Any idea about this ?
-
Mladen Rakonjac almost 7 yearsI still have this problem :(
-
Brian almost 7 yearsCheck out kotlinlang.org/docs/tutorials/… for official documentation on this.
-
Peter almost 7 yearscorrect one, and dont forget to add :) repositories { maven { url "maven.google.com" } }
-
HBG almost 7 yearsI had tried the above and it only worked when restarting Android Studio. Worth trying if you're still not having any luck.
-
Les almost 7 yearsworked for me except my version is 3.0.0-beta2, be sure to set your version correctly, an sync gradle
-
Shripal Shah over 6 yearsDataBinding Librabry kapt 'com.android.databinding:compiler:2.3.1'
-
Sergio about 6 yearsThis is working for me (gradle wrapper 4.4, tools 3.1.0)
-
Duran Jayson about 6 yearsAdding kapt 'com.android.databinding:compiler:3.0.1' works for me
-
Subhan Ali almost 6 yearsIt worked for me by adding only "apply plugin: 'kotlin-kapt". Thanks.
-
Mohamed ElSawaf over 5 yearsjust added this line is working for me (apply plugin: 'kotlin-kapt') Android Studio 3.2.1 - Kotlin plugin 1.3.0
-
Rahul Rastogi over 5 yearsapply plugin: 'kotlin-kapt' is really important in Kotlin.
-
naqi about 5 yearsMy project is still on gradle 3.0.1 release so this didn't work for me.
-
Khemraj Sharma about 5 yearsYou can update your Android Studio and gradle to achieve more stability.
-
Cesar Castro about 5 years
dataBinding.enabled = true
was what I was forgetting -
Ajay over 4 yearsYou 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 over 4 yearsglad it helped you
-
sud007 over 4 yearsVoted up for the direction mentioning this one as an 'Old answer'. Thoughtful and Helpful!
-
IgorGanapolsky over 4 yearscom.android.databinding:compiler is not needed
-
James Jordan Taylor over 2 yearsThe 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 about 2 years"viewBinding true " worked for me.. Thanks