Unresolved reference:ActivityMainBinding
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.
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:
- Check if you got top-level
<layout>
tag in respective .xml file. - Verify build.gradle (app) if binding
enabled=true
in android section. Verify imports in activity file:
import android.databinding.DataBindingUtil import com.example.[YOUR_APP_NAME].databinding.ActivityMainBinding
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:)
Alex
Updated on July 31, 2022Comments
-
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 errorUnresolved 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 over 4 yearsYou can also try to reboot your IDE if it's not refreshing code, but it extremely rare.
-
TarekB over 4 yearsin case someone needs to see the code here is the github link github.com/udacity/andfun-kotlin-android-trivia/blob/…
-
One Face about 4 yearsThis worked for view binding. Had to include
apply plugin: 'kotlin-kapt'
in the gradle file. -
B. Go about 4 yearsPlease avoid images of code! You can format code in your answer with the {} button
-
user1283182 about 4 yearsFor some questions about unresolved FragmentTitleBinding, please try to replace the default <FrameLayout> tag with <layout> tag in your res/layout/fragment_title.xml
-
Bardo about 4 yearsI 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 almost 4 yearsYep. That has worked for me! No actual configuration changes necessary. Thanks a lot.
-
thebluepandabear about 3 yearsWorked like a charm! Thanks :)
-
mufazmi almost 3 yearsWhile 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 almost 3 yearsThis looks like the "official" way to do it as it is described in Android Codelabs.
-
SMBiggs over 2 yearsYes, this finally lets me add the correct import statement. :)
-
Dieglock over 2 yearsIn my case I had the wrong context on the xml file. Thanks.
-
Admin over 2 yearsDoing this all the time is boring. Isn't there a better way to do this?
-
t0in4 over 2 yearsfor Kotlin add in plugins { id 'kotlin-kapt' }