Unresolved reference : ActivityMainBinding in Android Studio 3.6

Viewed 3036

I had updated an Android Studio 3.6 and then it shows me an error "Unresolved reference: ActivityMainBinding". But I have wondered that the project still working the same as the previous android studio version. Just only gave me an error "Unresolved reference: ActivityMainBinding".

7 Answers

DataBinding class will be generated based on your XML file name.

If your xml name is activity_main.xml then the DataBinding class name will be ActivityMainBinding.

If your xml name is main_activity.xml then the DataBinding class name will be MainActivityBinding.

Don't forget to clean and build the project once

~

I also had this problem and the solution was shared from https://stackoverflow.com/a/35883531/7952086

What finally worked for me:

  • clean
  • invalidate/restart caches
  • rebuild

Build -> Clean Project

Build -> Rebuild Project

Hope this will help. Thanks

Me also caused same problem, but resolves with adding "kapt" plugins. Try to apply kapt plugins.

you must make sure that : 1-your connexion internet is on"in manifest "( ) 2-in build gradle add in android{

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

dataBinding {
    enabled = true
}     
                             }

3-sync now then ctrl+espace to the ActivityMainBinding

As of 5.5.2021:

App build.gradle:

buildFeatures{
    viewBinding = true
}

MainActivity.kt:

..
import com.example.myapplication.databinding.ActivityMainBinding

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}
..

activity_main.xml:

For simple view bindings (as opposed to data binding) you can leave the outer tag to default:<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ..>

No <layout> tag required!


Things to note:

  1. My application is called MyApplication. Notice that line com.example.myapplication.databinding.ActivityMainBinding where the application name is converted to lowercase.
  2. The above (1) mentioned line will also be automaticaly added when you type in private lateinit var binding: ActivityMainBinding

This resolved my issues.

Its nothing. You just need to do "Invalidate cache and restart" once.

It happens when some times the binding file is cleared when we clean the project. It will be created automatically.

Thank you.

Related