Search code examples
androidkotlinandroid-activitydata-bindingandroid-databinding

Android Studio project doesn't generate binding object for the Main Activity


I took the Udacity course to learn developing Android apps with Kotlin, using data binding multiple times during it and having no problems enabling it, but now, trying to set up my first project, I am having some problems with it: Android Studio doesn't find the reference to the MainActivityBinding.

C:\Dev\Projects\Android_Studio\myproject\app\src\main\java\com\guglielmoboi\myproject\MainActivity.kt: (8, 35): Unresolved reference: MainActivityBinding

These are the files I produced:

build.gradle (project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
   ext {
       kotlin_version = "1.4.32"
   }

   repositories {
       google()
       jcenter()
   }

   dependencies {
       classpath "com.android.tools.build:gradle:4.1.3"
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

       // NOTE: Do not place your application dependencies here; they belong
       // in the individual module build.gradle files
   }
}

allprojects {
   repositories {
       google()
       jcenter()
   }
}

build.gradle (:app)

plugins {
   id 'com.android.application'
   id 'kotlin-android'
   id 'kotlin-kapt'
}

android {
   compileSdkVersion 30
   buildToolsVersion "30.0.3"

   defaultConfig {
       applicationId "com.guglielmoboi.myproject"
       minSdkVersion 26
       targetSdkVersion 30
       versionCode 1
       versionName "1.0"

       testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   }

   buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
       }
   }

   **buildFeatures {
       dataBinding true
       viewBinding true
   }**

   compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
   }

   kotlinOptions {
       jvmTarget = '1.8'
   }

}

dependencies {
   /* Room dependencies */
   def room_version = "2.3.0"

   implementation "androidx.room:room-runtime:$room_version"
   kapt "androidx.room:room-compiler:$room_version"

   // optional - Kotlin Extensions and Coroutines support for Room
   implementation "androidx.room:room-ktx:$room_version"

   // optional - Test helpers
   testImplementation "androidx.room:room-testing:$room_version"


   /* Navigation dependencies */
   def nav_version = "2.3.5"

   // Java language implementation
   implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
   implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

   // Kotlin
   implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
   implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

   // Feature module Support
   implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

   // Testing Navigation
   androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

   // Jetpack Compose Integration
   implementation "androidx.navigation:navigation-compose:1.0.0-alpha10"


   implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
   implementation 'androidx.core:core-ktx:1.3.2'
   implementation 'androidx.appcompat:appcompat:1.2.0'
   implementation 'com.google.android.material:material:1.3.0'
   implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
   testImplementation 'junit:junit:4.13.2'
   androidTestImplementation 'androidx.test.ext:junit:1.1.2'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<layout
    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">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

MainActivity.kt

package com.guglielmoboi.myproject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity()
{
    private lateinit var binding: MainActivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Solution

  • Inside MainActivity

    remove setContentView(R.layout.activity_main)

    and add

    val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

    Use binding for all the UI stuff.