'View Bindings not generating
I'm currently trying out the new ViewBindings but i'm not getting them to work. I'm on Android Studio 3.6.1. I just created a new project and added viewBinding.enabled = true to my modules build.gradle. But when I try to access the MainActivityBinding class, it says it cannot resolve the symbol. Autocomplete doesn't find anything resembling a binding class. I also tried with a different project using Kotlin but no success there. AS4.0 doesn't help either. What do I need to do to generate the ViewBinding classes?
My build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
viewBinding {
enabled = true
}
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Solution 1:[1]
The layout's root tag has to be <layout>. Make sure it's not your case.
Had to change my original layout XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello World!" />
</androidx.constraintlayout.widget.ConstraintLayout>
to
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello World!" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Solution 2:[2]
In my case I was trying to bind View
added build.gradle (module)
buildFeatures{
dataBinding true
viewBinding true
}
previous one was
viewBinding{
enabled = true
}
Solution 3:[3]
i have just changed the names of the layouts and it worked just fine
Solution 4:[4]
if you have entered fragment/layout name manually, check the spelling for keyword of the layout name like "fragment", a mistake in this would generate a binding file with the same name
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | HANiS |
| Solution 2 | Samir |
| Solution 3 | Mohamed Ali |
| Solution 4 | Sushruth S |
