Solve problem commonMain error: Expected class <classname> has no actual declaration in module…

Currently we upgrade the Android studio, and upgrade some third party libraries, and after some days, I accidentally find that the class…

Solve problem commonMain error: Expected class <classname> has no actual declaration in module <project>.shared for JVM

Currently we upgrade the Android studio, and upgrade some third party libraries, and after some days, I accidentally find that the class with expect report error like commonMain error: Expected class ‘ClassName’ has no actual declaration in module <project>.shared for JVM, this is what I have done to solve the problem.

Investigation

Check example project

I installed the gitlab-client before, and I want to update the code and check whether the problem also exists on this project or not, if this project has no problem, then I can find which setting solve the problem, but in my machine, the project report the same error. After that I am more sure the problem is caused by the Kotlin or KMM plugin.

Downgrade kotlin

First I thought it was caused by we upgrade the kotlin to 1.6.21, so I try to downgrade the kotlin to 1.5.31 again, but the error was still there. After this try, I think the problem should not caused by the Kotlin version, the problem maybe the issue of the Android Studio.

Check gradle scripts

Then I try to create an empty project with Android Studio, and find that the new project created by the Android Studio has no error. Now I have a project has no problem, then I can investigate which setting cause the problem, but after I copy the setting on the project to my project, the problem still exists.

The settings that different with my project

dependsOn

In the new generated project, the build.gradle.kts in shared module contain dependsOn setting, like below:dependsOn(commonMain)

So I also added the dependsOn to my setting, but the error still exists.

gradle.properties

In the new project, there is a setting that I didn’t have, the setting is like below:kotlin.mpp.enableCInteropCommonization=true

But it also didn’t work on my project.

Replace the settings on new project with settings on my project

I thought that there maybe some settings on my project cause the error, so I copy the codes and settings to the new project, but it doesn’t report error, but there is a clue, when I didn’t add actual class on androidMain module, the error is like below:Expected class '<classname>' has no actual declaration in module <project>.shared.main for JVM

The difference between the two errors is that, the new project the module is <project>.shared.main, but in my project the module is <project>.shared. So I think the problem might caused by the settings on Android Studio.

The root cause

After some investigation, I think the problem is the settings on the Android Studio.

I checkout the project to a new folder, open with Android Studio, and the error was gone.

Then I delete the .idea folder on my project, and open with Android Studio again, the error also was gone.

So the root cause of the problem is the setting in .idea folder, maybe after I update some third party library or upgrade the Android Studio cause the problem.

Thanks for your reading, if you also encounter the same problem, hope this article help you to solve the problem :)