when I try to directly type the button id in main activity of kotlin , it does not pick it up automatically, instead it says unresolved reference, what is wrong with Android Studio??
when I try to directly type the button id in main activity of kotlin , it does not pick it up automatically, instead it says unresolved reference, what is wrong with Android Studio??
In my case I had the same problem and have solved it this way with Android Studio 4.x:
Put this in your apps build.gradle or build.gradle.kts. The latter in my case because my project is made with Kotlin KMM.
buildFeatures {
viewBinding = true
}
Here is my complete build.gradle.kts so you can see where it belongs to:
plugins {
id("com.android.application")
kotlin("android")
id("androidx.navigation.safeargs.kotlin")
}
dependencies {
implementation(project(":shared"))
implementation("com.google.android.material:material:1.3.0")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("androidx.constraintlayout:constraintlayout:2.0.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.3.4")
implementation("androidx.navigation:navigation-ui-ktx:2.3.4")
}
android {
compileSdkVersion(30)
defaultConfig {
applicationId = "************************"
minSdkVersion(24)
targetSdkVersion(30)
versionCode = 1
versionName = "1.0"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
buildFeatures {
viewBinding = true
}
}
Here you can read all about it. Also if you are using Android Studio lower than 4.x how it has to be done.
https://developer.android.com/topic/libraries/view-binding
Don't forget to sync your project. It will only appear after that step. You can check it by having a look at dataBinding folder which you will find at that path.
build > generated > data_binding_base_class_source_out > debug > out > rest is app specific
and the last folder will be "dataBinding" folder.
Have fun!
It's hard to advise anything without being given any code.
I'm guessing you're using Kotlin synthetic import? Then you would also need to import something like this import kotlinx.android.synthetic.main.activity_main.* to be able to use view elements without findById. Also, make su you have integrated Kotlin extensions plugin.
Also, I can't rule out this possibility, since I can't see any of your code, but maybe check if the view element you're trying to access in MainActivity is in it's layout?