Unresolved reference:ActivityMainBinding

23,986

Solution 1

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
}

Solution 2

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

Solution 3

Try to add in build.gradle (Module:app)

buildFeatures {
   viewBinding true
}

Solution 4

It's not about the Gradle file. It is about the XML file.

First, make sure that your layout tag contains just this line:

xmlns:android="http://schemas.android.com/apk/res/android"

or these lines:

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

Then rebuild the project.

Solution 5

Just to sum up all useful actions:

  1. Check if you got top-level <layout> tag in respective .xml file.
  2. Verify build.gradle (app) if binding enabled=true in android section.
  3. Verify imports in activity file:

    import android.databinding.DataBindingUtil
    import com.example.[YOUR_APP_NAME].databinding.ActivityMainBinding
    
  4. Try to rebuild project Build > Rebuild Project after every action just to refresh and check the results.

    Good Luck! It's just beginning of this Android journey:)

Share:
23,986
Alex
Author by

Alex

Updated on July 31, 2022

Comments

  • Alex
    Alex almost 2 years

    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.

  • Rob Ert
    Rob Ert over 4 years
    You can also try to reboot your IDE if it's not refreshing code, but it extremely rare.
  • TarekB
    TarekB over 4 years
    in case someone needs to see the code here is the github link github.com/udacity/andfun-kotlin-android-trivia/blob/…
  • One Face
    One Face about 4 years
    This worked for view binding. Had to include apply plugin: 'kotlin-kapt' in the gradle file.
  • B. Go
    B. Go about 4 years
    Please avoid images of code! You can format code in your answer with the {} button
  • user1283182
    user1283182 about 4 years
    For some questions about unresolved FragmentTitleBinding, please try to replace the default <FrameLayout> tag with <layout> tag in your res/layout/fragment_title.xml
  • Bardo
    Bardo about 4 years
    I had layout_height and width on my Layout root node, but when only namespace attributes where left, the system proposed me to convert it to databinding layout. I cleaned and rebuild project, and the problem got solved.
  • cluster1
    cluster1 almost 4 years
    Yep. That has worked for me! No actual configuration changes necessary. Thanks a lot.
  • thebluepandabear
    thebluepandabear about 3 years
    Worked like a charm! Thanks :)
  • mufazmi
    mufazmi almost 3 years
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
  • HelloWorld
    HelloWorld almost 3 years
    This looks like the "official" way to do it as it is described in Android Codelabs.
  • SMBiggs
    SMBiggs over 2 years
    Yes, this finally lets me add the correct import statement. :)
  • Dieglock
    Dieglock over 2 years
    In my case I had the wrong context on the xml file. Thanks.
  • Admin
    Admin over 2 years
    Doing this all the time is boring. Isn't there a better way to do this?
  • t0in4
    t0in4 over 2 years
    for Kotlin add in plugins { id 'kotlin-kapt' }