Fix Kotlin “Unresolved Reference: println”
println normally works in a correctly configured Kotlin file without an import, so “unresolved reference: println” usually means the file is not being compiled with the Kotlin standard library, the module is misconfigured, or the editor has an out-of-date project model. First determine whether Gradle or the Kotlin compiler fails too; an unresolved println reference can be a real compilation failure, not just an editor warning.
Use this table to match the symptom to the most likely repair.
| Symptom | Likely Cause | Fix |
|---|---|---|
The editor and Gradle both show Unresolved reference: println |
Kotlin standard library is unavailable, excluded, or the module does not apply a Kotlin plugin | Check the module’s Kotlin plugin and dependency setup |
| Only the editor shows the error, but the build succeeds | IDE project model or language server is stale | Reload Gradle or restart the Kotlin language server |
The file ends in .java |
Java has no standalone println function |
Use System.out.println(...) or change the source to Kotlin |
A Kotlin Multiplatform commonMain file has related unresolved references |
Common code is calling platform-only declarations | Move platform code to a platform source set or use expect/actual |
| The problem occurs in generated Kotlin | The generator output or generated-source classpath is incomplete | Fix the generating task and ensure it runs before Kotlin compilation |
What Kotlin Unresolved Reference: println Means
In Kotlin, println() is a top-level function from the Kotlin standard library. Kotlin normally imports it automatically, so this code should compile in a .kt file:
fun main() {
println("Hello, Kotlin")
}
You do not usually need to add this import manually:
import kotlin.io.println
Adding that import can make the intent clearer, but it will not repair a missing Kotlin standard library or a broken build configuration. If the compiler cannot find kotlin.io.println, it cannot use the function whether it is imported implicitly or explicitly.
A bare println error is usually a project-classpath or source-set problem, not a missing Kotlin syntax feature.
Before changing dependencies or clearing caches, establish whether the error comes from the compiler or only from your editor.
How to Fix Kotlin Unresolved Reference: println
Step 1: Check Whether the Project Actually Fails to Build
Build the project from its root directory. On macOS or Linux, run:
./gradlew build
On Windows, run:
gradlew.bat build
Interpret the result before doing anything else:
- If the build succeeds but the editor still marks
printlnas unresolved, the Kotlin code and dependencies are probably valid. Skip to Step 4 and refresh the editor’s project model. - If the build fails with the same unresolved-reference error, continue with the module configuration checks.
- If the error occurs only in one file, compare that file’s extension, source set, package declaration, and imports with a Kotlin file that builds successfully.
Also verify that the source file really is Kotlin:
- Kotlin files use the
.ktextension. - Java files use the
.javaextension. - Java requires
System.out.println("Hello");; it does not have Kotlin’s standaloneprintln()function.
Step 2: Test println in the Same Kotlin Module
Create a temporary Kotlin file in the module that fails, preferably under its normal Kotlin source directory, such as src/main/kotlin.
fun main() {
println("Kotlin standard library is available")
}
Then build the project again to check whether the temporary file fails with the same unresolved-reference error.
If this small file fails too, the issue is module-wide: Kotlin is being compiled without access to its standard library, or the module is not configured as a Kotlin module.
If the test file compiles but the original code fails, inspect the original file for a more local problem:
- Remove an invalid or accidental import that shadows a name.
- Check whether the file is generated and whether its generator has run.
- Check whether the file belongs to a custom source set that is missing dependencies.
- In a multiplatform project, confirm that the file is in the intended source set, such as
commonMainorjvmMain.
Step 3: Repair the Module’s Kotlin Configuration
Open the build file for the module containing the failing file, not only the root build file. The module must apply the appropriate Kotlin plugin:
- JVM projects need the Kotlin JVM plugin.
- Android projects need the Kotlin Android plugin.
- Multiplatform projects need the Kotlin Multiplatform plugin.
A standard Kotlin Gradle setup supplies the Kotlin standard library automatically. Problems often appear after a custom build configuration has removed default dependencies, used a custom compiler classpath, or passed a compiler option such as -no-stdlib.
If your module intentionally manages Kotlin dependencies itself, check your project configuration before adding dependencies.
dependencies {
implementation(kotlin("stdlib"))
}
Do not add this dependency repeatedly to every module as a general workaround. First make sure the affected module applies the Kotlin plugin and is included in the Gradle build. A dependency declaration cannot fix a source file that Gradle does not compile.
After changing the module configuration, reload the Gradle project and build again.
Step 4: Refresh the IDE or Kotlin Language Server
If Gradle builds successfully, treat the unresolved reference as an editor synchronization problem.
In IntelliJ IDEA or Android Studio:
- Use the Gradle tool window to reload all Gradle projects.
- Wait for indexing and dependency resolution to finish.
- Reopen the Kotlin file and confirm that
printlnis no longer red. - If the problem persists, restart the IDE before considering cache invalidation.
In VS Code, restart the Kotlin language server from the Command Palette using Kotlin: Restart the Language Server after the Gradle project resolves successfully.
For a Flutter plugin with Android Kotlin code opened in VS Code, build the plugin’s example Android app first:
cd example
flutter build apk --debug
Then run Kotlin: Restart the Language Server. This gives the language server the generated Android artifacts and resolved Gradle classpath it needs to analyze the plugin source.
Step 5: Rebuild and Run a Real Entry Point
Finish by running the module’s normal build and then run the application, test, or task that contains the println call.
A successful build confirms that Kotlin resolved the symbol. Seeing the expected text in the Run, Debug, test, or terminal output confirms that the relevant code path actually executed.
Do not rely only on an editor’s red underline or lack of one. The compiler result is the final answer when the IDE and build disagree.
Kotlin Multiplatform and println
println is available to normal Kotlin common code, so this is valid in a commonMain source set:
fun reportStatus(message: String) {
println(message)
}
What does not belong in commonMain is a platform-specific API. For example, System.out.println() is JVM-specific because System comes from the Java platform:
fun reportStatus(message: String) {
System.out.println(message)
}
Put JVM-only code in jvmMain, Android-only code in the appropriate Android source set, or define a shared API with expect in common code and actual implementations for each target.
Kotlin Multiplatform can also expose differences between IDE analysis and compilation when one module’s common code accidentally relies on a platform declaration from another module. JetBrains introduced an experimental separate-compilation mode in Kotlin 2.5.0-Beta1 to make those boundary errors more consistent between the IDE and compiler.
Enable it only for a Kotlin Multiplatform project where you need that stricter behavior. Add this line to the project’s gradle.properties file:
kotlin.kmp.separateCompilation=true
The setting is experimental and disabled by default. It is useful for finding invalid common-to-platform references, but it is not a general fix for a missing println; a missing println still points to a broken standard-library classpath or incorrect source configuration. This option changes how common and platform source-set declarations are resolved during compilation.
Other Causes of an Unresolved println
Custom compiler classpaths, generated sources, and mixed Java and Kotlin files can leave println without standard-library access.
Custom Compiler Classpaths
A custom Kotlin compiler invocation can omit the standard library. This is especially likely in hand-written build scripts, code-generation tasks, or CI jobs that invoke the compiler directly.
Check for configurations that:
- pass
-no-stdlib; - replace rather than extend the compiler classpath;
- compile generated sources outside the module’s normal source sets;
- use a Kotlin compiler distribution or plugin version inconsistent with the project.
Restore the standard Kotlin dependency setup, then rerun the same build command used by CI. Fixing only the local IDE classpath can hide a problem that will fail again in automated builds.
Generated Kotlin Sources
If println is unresolved in generated code, do not edit the generated file as the primary fix. The generator may have run before its dependencies were available, or Gradle may not know that the generated directory is a Kotlin source directory.
Fix the task that creates the source, ensure it runs before Kotlin compilation, and rebuild from a clean project state. The regenerated file should then compile with the same standard-library access as hand-written Kotlin files.
Mixed Java and Kotlin Projects
A Java/Kotlin project can use both output styles, but the code must match the language of the file:
println("Kotlin output")
System.out.println("Java output");
If a code sample was copied from Kotlin into Java, use Java’s System.out.println. If Java code was copied into commonMain, remove System.out because it is not portable Kotlin Multiplatform code.
How to Confirm the Fix and Avoid a Repeat
Use this short check after repairing the project:
- The file has a
.ktextension. - The affected module applies a Kotlin plugin.
- The module’s normal Kotlin standard-library dependency is available.
- Gradle completes without an unresolved-reference error.
- The IDE finishes Gradle sync or language-server dependency resolution.
- A real application run or test reaches the
printlncall and shows output.
Keep dependency and Kotlin plugin versions managed in one place where possible. Avoid custom compiler classpaths unless the build has a specific need for them, and test generated-source tasks in CI rather than relying only on IDE analysis.
Frequently Asked Questions
How do I print in Kotlin?
Use println() to print a value followed by a newline.
println("Hello")
Use print() when you do not want Kotlin to append a newline.
print("Hello")
Do I need to import println in Kotlin?
No. Kotlin normally makes println available through its default imports, including the Kotlin I/O package. Adding import kotlin.io.println is optional and will not fix a missing standard library or invalid module configuration.
Why does println work in one Kotlin file but not another?
The failing file may belong to a different Gradle module, source set, generated-source directory, or language context. Compare the file extension, module ownership, build configuration, and source-set location; a file in commonMain also cannot use JVM-only APIs beside println.
Can Unresolved reference: println fail a Kotlin build?
Yes. If Gradle or the Kotlin compiler reports the error, it is a real compilation failure and must be fixed in the module configuration or compiler classpath. If only the editor reports it while Gradle succeeds, reload the project or restart the Kotlin language server.
Should I enable Kotlin Multiplatform separate compilation for this error?
Enable kotlin.kmp.separateCompilation=true only when diagnosing Kotlin Multiplatform source-set and cross-module resolution problems. The experimental option introduced in Kotlin 2.5.0-Beta1 makes common-code boundaries stricter, but it does not replace a missing Kotlin standard library.
Run the Gradle build after each change; once it succeeds and your application or test prints the expected output, remove any temporary test file you added.
