Unresolved reference: ...
Unresolved reference: ...
DodaTech
3 min read
The “Unresolved reference” error means the Kotlin compiler cannot find a symbol — a variable, function, class, or property — that you are trying to use in your code.
What It Means
Every identifier in Kotlin must be declared or imported before it can be used. The compiler checks every name against the visible scope: local declarations, class members, top-level declarations, and imported symbols. When a name doesn’t match any of these, the compiler emits “Unresolved reference: [name].”
Why It Happens
- You typed a variable or function name incorrectly (typo or wrong case).
- A class or function was declared but not imported with an
importstatement. - The symbol is in a different package and you forgot to specify the import path.
- A dependency (library) that provides the symbol is not added to
build.gradle.kts. - The variable is out of scope — declared inside a block but referenced outside it.
- The symbol is defined in a file that hasn’t been compiled yet.
How to Fix It
1. Check for typos and case sensitivity
Kotlin is case-sensitive. myVariable and myvariable are different symbols:
// ❌ Unresolved reference: myvariable
val myVariable = "Hello"
println(myvariable)
// ✅ Correct case
println(myVariable)2. Add the missing import
// ❌ Unresolved reference: File
fun readFile(path: String) = File(path).readText()
// ✅ Add the import
import java.io.File
fun readFile(path: String) = File(path).readText()3. Verify the package declaration
// File: com/example/utils.kt
// ❌ If the file has no package, the default is the root — but if it declares
// package com.example.tools, you must import from that package:
package com.example.tools
fun helper() = "OK"
// File: main.kt
// ❌ Unresolved reference: helper
fun main() {
println(helper())
}
// ✅ Import from the correct package
import com.example.tools.helper
fun main() {
println(helper())
}4. Add the dependency to your build file
If the symbol comes from an external library (e.g., Kotlinx serialization):
// build.gradle.kts
dependencies {
// ❌ Unresolved reference: kotlinx.serialization
// ✅ Add the dependency
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.0")
}Then sync your Gradle project and re-import.
5. Check variable scope
fun process() {
val result = compute()
if (result > 0) {
val message = "Positive"
}
// ❌ Unresolved reference: message (out of scope)
println(message)
// ✅ Move the declaration outside the block
val message: String
if (result > 0) {
message = "Positive"
} else {
message = "Negative"
}
println(message)
} Previous
Unable to connect to the server: dial tcp ...: i/o timeout
Next
HTTP 204 No Content — What It Means & How to Debug
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro