Kotlin Unresolved Reference Error Fix
In this tutorial, you'll learn about Kotlin Unresolved Reference Error Fix. We cover key concepts, practical examples, and best practices.
The Problem
Your Kotlin code fails to compile:
e: Unresolved reference: findViewById
e: Unresolved reference: ListUtils
e: Unresolved reference: context
The Kotlin compiler cannot find the symbol you are referencing. This means the class, function, or property is either not imported, not defined, or not available in the current classpath/dependencies.
Quick Fix
Step 1: Check imports
WRONG -- using a class without importing it:
fun main() {
val list = listOf(1, 2, 3)
val shuffled = Collections.shuffle(list) // Unresolved reference: Collections
}
RIGHT -- add the import:
import java.util.Collections
fun main() {
val list = listOf(1, 2, 3)
Collections.shuffle(list)
}
In IntelliJ IDEA, press Alt+Enter on the unresolved reference and select "Import".
Step 2: Check if the symbol is in scope
WRONG -- accessing a variable that is not in scope:
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener { // Unresolved reference: button
// ...
}
}
}
RIGHT -- find the view first:
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
// ...
}
}
}
Or use Kotlin Android Extensions (deprecated) or ViewBinding.
Step 3: Add missing Gradle dependencies
WRONG -- using a library function without adding the dependency:
import com.google.gson.Gson
fun serialize(data: Any): String {
return Gson().toJson(data) // Unresolved reference: Gson
}
RIGHT -- add the dependency in build.gradle.kts:
// build.gradle.kts
dependencies {
implementation("com.google.code.gson:gson:2.10.1")
}
Then sync the project:
./gradlew build --refresh-dependencies
Step 4: Fix typos in function and class names
WRONG -- misspelled function name:
val text = "hello"
val upper = text.toUppercase() // Unresolved reference: toUppercase
RIGHT -- use the correct name:
val text = "hello"
val upper = text.uppercase() // correct: uppercase in Kotlin 1.5+
Kotlin changed many standard library function names in 1.5 (capitalize -> replaceFirstChar, toUpperCase -> uppercase). Check the API version you are targeting.
Step 5: Check visibility modifiers
WRONG -- accessing a private member from outside the class:
class MyClass {
private val secret = "hidden"
}
fun main() {
val obj = MyClass()
println(obj.secret) // Unresolved reference: secret is private
}
RIGHT -- make it public or provide a getter:
class MyClass {
val secret = "hidden" // public by default
}
Step 6: Rebuild the project and clear caches
Sometimes the IDE cache gets stale:
./gradlew clean build
# Or delete caches
rm -rf .gradle/
rm -rf build/
./gradlew build
In IntelliJ IDEA: File > Invalidate Caches and Restart.
Use DodaTech's Build Analyzer to resolve missing dependencies, detect visibility issues, and fix import problems across your Kotlin project.
Prevention
- Use IntelliJ IDEA's auto-import feature for Kotlin files.
- Run
./gradlew buildafter adding new dependencies. - Use consistent naming conventions to avoid typos.
- Check the Kotlin API version when migrating between major versions.
- Keep
build.gradle.ktsorganized with clearly named dependency blocks.
Common Mistakes with unresolved reference
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world KOTLIN code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro