Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference:ActivityMainBinding

Tags:

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.

like image 443
Alex Kolydas Avatar asked May 01 '19 08:05

Alex Kolydas


People also ask

What does unresolved reference mean in Kotlin?

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.

What is ActivityMainBinding in Kotlin?

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.

What is ActivityMainBinding 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.

How do I fix unresolved reference error?

Build -> Clean -> Build -> Rebuild. File -> Invalidate Caches and Restart. delete .


2 Answers

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
}
like image 156
Android Avatar answered Sep 17 '22 15:09

Android


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

like image 35
hivana Avatar answered Sep 17 '22 15:09

hivana