I know that this question have been asked before but I tried all the answers given and I still got nothing. I'm trying to define a var binding:ActivityMainBinding
and I got an error
Unresolved reference:ActivityMainBinding
Here is a part of my mainActivity
import com.kolydas.aboutme.databinding.ActivityMainBinding //error
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding //error
//....
}
Here is my App build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.kolydas.aboutme"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding{
enabled = true //enable data binding
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
kapt "com.android.databinding:compiler:3.4.0"
}
And of course in my xml file i have the layout /layout tags in the beginning and in the end.
To conclude, the unresolved reference error happens when Kotlin has no idea what the keyword you're typing in the file points to. It may be a function, a variable, or another construct of the language that you've yet to declare in the code.
It converts the layout name to Pascal case notation and will be adding a Binding suffix to that. If the layout filename is activity_main.xml so the corresponding generated class is named as ActivityMainBinding. This class holds all the bindings info. Source: Databinding in Android.
xml so the corresponding generated class is ActivityMainBinding . This class holds all the bindings from the layout properties (for example, the user variable) to the layout's views and knows how to assign values for the binding expressions.
Build -> Clean -> Build -> Rebuild. File -> Invalidate Caches and Restart. delete .
None of the suggestions worked for me, because the correct way to resolve this issue is to put (app level build.gradle)
buildFeatures{
dataBinding = true
viewBinding = true
}
Sometimes all you need is clean and rebuild the project.
Build -> Clean Project
Build -> Rebuild Project
This would be enough for generating the binding classes.
Generated binding classes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With