By Shakil Ahmed, Android developer & software engineer
How to Fix 'Unresolved Reference' Errors in Android Studio (Kotlin)
Getting 'Unresolved reference' in Kotlin? Learn the quick fix, why it happens, and every common cause, from missing imports to KSP generated code.
The Unresolved reference error is one of the first errors every Kotlin developer sees in Android Studio. It means the compiler cannot find a name you used, such as a class, function, or variable. The good news: it is almost always a small problem, and you can fix it in a few minutes once you know where to look.
The Error
You will see the name in red in the editor. When you build, the Build window shows something like this:
e: file:///C:/Users/you/MyApp/app/src/main/java/com/example/myapp/MainActivity.kt:24:9 Unresolved reference 'Retrofit'.
Older Kotlin versions print it without quotes:
e: MainActivity.kt: (24, 9): Unresolved reference: Retrofit
In Jetpack Compose you will often see these versions:
Unresolved reference 'getValue'.
Property delegate must have a 'getValue(Nothing?, KProperty<*>)' method. None of the following functions are suitable.
Unresolved reference 'items'.
The word after "Unresolved reference" is the important part. That is the name Kotlin cannot find. The numbers (24:9) are the line and column.
Quick Fix (Try This First)
- Click on the red name and press
Alt + Enter(Option + Enteron macOS). Choose Import if Android Studio offers it. - Sync Gradle. Click Sync Now in the yellow bar, or use File > Sync Project with Gradle Files.
- Check the dependency. If the class comes from a library (Retrofit, Room, Coil), make sure the library is in
app/build.gradle.kts. - Build the project with Build > Make Project (
Ctrl + F9). This creates generated code for Room and Hilt. - Check the spelling. Kotlin is case-sensitive.
textviewis nottextView.
These five steps fix most cases. If the error is still there, read the causes below.
Why This Happens
Think of the Kotlin compiler like a librarian. You ask for a book called Retrofit. The librarian looks at the shelves you gave them access to. If the book is not on those shelves, the librarian says, "I cannot find it." That is "unresolved reference".
There are only a few reasons the librarian cannot find the book:
- You did not tell them which shelf to look on (missing import).
- The book was never brought into the library (missing dependency).
- The library catalogue is old (Gradle not synced or IDE cache is stale).
- The book has not been printed yet (generated code not built).
- You spelled the title wrong (typo).
All Causes and How to Fix Them
1. Missing import
Symptoms: The class exists in your project or library, but the file has no import line for it.
Fix: Put the cursor on the red name and press Alt + Enter, then choose Import. If there are several choices, pick the correct package. For example, Text for Compose is androidx.compose.material3.Text, not a View class.
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Tip: Turn on auto-import. Go to File > Settings > Editor > General > Auto Import and enable Add unambiguous imports on the fly for Kotlin. Android Studio will then add simple imports for you.
2. Missing dependency in app/build.gradle.kts
Symptoms: Alt + Enter does not offer an import. The package itself (for example retrofit2) is red.
Fix: Add the library to the dependencies block of your module file (app/build.gradle.kts, not the root one), then sync.
// app/build.gradle.kts
dependencies {
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.retrofit2:converter-gson:2.11.0")
}
Use the version from the library's official page. If you are new to Retrofit, my Retrofit beginner guide shows the full setup.
3. Gradle not synced
Symptoms: You just added a dependency, but the code is still red. A yellow bar says "Gradle files have changed since last project sync."
Fix: Click Sync Now, or File > Sync Project with Gradle Files. Wait for the sync to finish. If the sync itself fails, the import will never work. See How to Fix 'Gradle Sync Failed' in Android Studio.
4. Missing Compose imports (getValue, setValue, items)
This is very common in Jetpack Compose. The code looks correct, but Kotlin cannot use the by keyword.
Symptoms:
var count by remember { mutableStateOf(0) } // red: getValue / setValue
Fix: The by keyword needs two helper functions. Add these imports:
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
For a val with by (for example collectAsStateWithLifecycle()), you only need getValue. For a var, you need both.
The second common one is items inside LazyColumn:
import androidx.compose.foundation.lazy.items
LazyColumn {
items(users) { user ->
Text(user.name)
}
}
Without this import, Kotlin only sees the items(count: Int) version, so passing a list fails. You can learn more about state in Jetpack Compose buttons and state and lists in the LazyColumn guide.
5. Typo or wrong package name
Symptoms: The name is red only in one place. Or you moved a file and now other files cannot find it.
Fix:
- Check uppercase and lowercase letters.
viewmodelandviewModelare different. - Check the
packageline at the top of the file. It must match the folder. - If you renamed a class by hand, use Refactor > Rename (
Shift + F6) next time. It updates every usage.
package com.example.myapp.ui // must match folder: com/example/myapp/ui
6. KSP or kapt generated code not built yet (Room, Hilt)
Symptoms: Names like AppDatabase_Impl, Hilt_MyApplication, or DaggerAppComponent are red. You did not write these classes. A code generator creates them during the build.
Fix:
- Make sure the KSP plugin and the compiler dependency are added:
// app/build.gradle.kts
plugins {
alias(libs.plugins.ksp)
}
dependencies {
implementation(libs.androidx.room.runtime)
implementation(libs.androidx.room.ktx)
ksp(libs.androidx.room.compiler)
}
- Run Build > Make Project. The generated files appear after a successful build.
- If the build fails, fix the first error in the Build window. Generated code is missing because an earlier error stopped the generator.
Warning: If you use Android Gradle Plugin 9 or newer, the old
kotlin-kaptplugin does not work with built-in Kotlin. Google recommends moving to KSP. Room and Hilt both support KSP.
My Room database guide shows a complete working Room setup.
7. Version catalog alias typo (libs.xxx)
New projects use a version catalog file: gradle/libs.versions.toml. In the build file, you write libs.something. If the name does not match, you get an unresolved reference inside the Gradle file.
Symptoms:
Unresolved reference 'retrofit'.
The red text is in build.gradle.kts, on a line like implementation(libs.retrofit).
Fix: Open libs.versions.toml and check the alias. Dashes and underscores in the TOML file become dots in Kotlin.
[versions]
retrofit = "2.11.0"
[libraries]
retrofit-core = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
// app/build.gradle.kts
implementation(libs.retrofit.core) // "retrofit-core" becomes libs.retrofit.core
After editing the TOML file, always sync Gradle.
8. Stale IDE cache (last resort)
Symptoms: The project builds successfully, but the editor still shows red names. Or the error comes and goes for no reason.
Fix: Try these in order:
- File > Repair IDE. It walks you through steps like re-scanning the project and reindexing.
- File > Invalidate Caches..., then tick the options and click Invalidate and Restart.
Tip: Do not jump to Invalidate Caches first. It takes time to re-index, and it rarely fixes a real missing import or dependency. Use it only when the build passes but the editor disagrees.
How to Prevent It
- Turn on auto-import so simple imports are added for you.
- Add dependencies through the version catalog and sync right after every change.
- Use Refactor > Rename instead of renaming by hand.
- Build often (
Ctrl + F9) when you use Room, Hilt, or other KSP libraries. - When you copy code from a blog or AI tool, also copy the imports. Many tutorials hide them. If you use AI to help, ask it to include imports, as I explain in use AI to learn programming faster.
- Read the full error in the Build window, not just the red underline.
Frequently Asked Questions
Why does my code build but still show "Unresolved reference" in red? This is usually a stale IDE index. Try File > Repair IDE first. If it continues, use File > Invalidate Caches... and restart.
Why is getValue unresolved when I use by remember?
The by keyword needs the getValue function, and var also needs setValue. Add import androidx.compose.runtime.getValue and import androidx.compose.runtime.setValue.
Alt + Enter does not show "Import". What should I do?
The class is not on the classpath. Add the library to app/build.gradle.kts, sync Gradle, and try again. Also check if you spelled the class name correctly.
Why is AppDatabase_Impl or Hilt_... unresolved?
These classes are generated by KSP or kapt during the build. Check that the compiler dependency uses ksp(...), then run Build > Make Project. Fix any earlier build errors first.
Summary
When you see Unresolved reference, go through this checklist:
- Pressed
Alt + Enterand imported the class - Library is added in
app/build.gradle.kts - Gradle sync finished without errors
- Compose imports added (
getValue,setValue,items) - Spelling, case, and package name are correct
- Ran Build > Make Project for Room or Hilt generated code
-
libs.xxxalias matcheslibs.versions.toml - Tried Repair IDE or Invalidate Caches only as a last step
If you are still stuck, read the error carefully and search the exact name. For runtime crashes (not compile errors), Logcat is your best friend. You can also learn how to ask AI to fix code errors the right way. For official details, see the Android developers guide to adding build dependencies.
Related articles
Save data on the phone with Room. Build a Compose shopping list that works offline and is still there after you close the app.
Use Preferences DataStore to save small settings like dark mode and the user's name, and make your whole Compose app follow them.
Use Retrofit to download real fruit data from a free API and show it in your Jetpack Compose list. Simple steps for total beginners.
Learn the Kotlin you need for Android: val and var, types, if/else and when, functions, lists, loops, null safety and data classes, with tiny examples.