Can't reference Java dependencies in common/shared KMM module

I know I’m missing something really simple here, but for the life of me, I can’t figure out what:

I’d like to use multiplatform to build a cross-platform application (preferably, Android, iOS, macOS, and watchOS). One of the first things I had to do was depend on a Java library, which seems like it should be easy to do. In fact, when I add it as a dependency in my Gradle build file, I can see it show up as a dependency in the “External Libraries” section of my project. But when I try to reference any of its classes (e.g., ICalendar) in my Kotlin files, I get an error, “Unresolved reference: ICalendar.”

I’ve tried this three different ways (all brand-new projects), and only one actually works out:

  1. Kotlin-only (not KMM) application in IntelliJ (2020.3; Kotlin plugin 203-1.4.32-release-IJ7148.5)
  2. KMM mobile application in IntelliJ (same installation as above)
  3. KMM application in Android Studio (4.2 beta 6; Kotlin plugin 202-1.4.32-release-AS8194.7; KMM plugin 0.2.2(202-1.4.31-release-IJ)-93)

The bare bones Kotlin application (#1) is the one that works just fine, but both KMM applications fail with the “unresolved reference” error. There are only 2 things I’ve edited in the stock KMM templates that are created after going through the IDE project creation flow:

Add the dependency to my shared module’s build.gradle.kts file

// shared/build.gradle.kts
// ...

kotlin {
	// ...

	sourceSets {
		val commonMain by getting {
			dependencies {
				implementation("net.sf.biweekly:biweekly:0.6.6")
			}
		}

		// ...
	}
}

Try to add a reference to a class in the library in a Kotlin source file

// shared/src/commonMain/kotlin/Test.kt
import biweekly.ICalendar

class Test {
    fun hi() {
        val ical = ICalendar()
    }
}

Both the import statement and the actual contructing of the object produce errors (in the linked projects, I didn’t include the import statement, but the IDE doesn’t even ask me to add it – it doesn’t see the ICalendar class in the first place).

Any ideas? This seems so basic, and not only that, but the boilerplate KMM project templates depend on JUnit for the test source sets, which seems to work just fine (although notably, there are no tests in the commonTest module, and trying to reference JUnit classes from there also gives an “unresolved reference” error).

I’ve attached a ZIP file of the three example projects, in case you’re interested.

kotlin-java-library-dependency-issue.zip (2.4 MB)

Because basically the Java library can only be used on the JVM, and so you aren’t allowed to use it in the common source sets. Your only solution is to either find an alternative KMP library, or use the ICalendar library on the JVM and other platform-specific libraries on the other platforms

A ha! That makes a ton of sense. For some reason I thought the Java code would also be cross-compiled into the native binaries, but I guess it’s only the Kotlin code that benefits from it. Thanks for the quick response!

1 Like