Mpp: How to add a sourceSet shared between Android and Desktop JVM

Hi folks!

I have an MPP project targeting Android, Desktop and JS. Since I have some code that is shared between Android and Desktop, but does not apply to JS, I’m trying to add another sourceSet called javaCommon.

I tried to follow the “disambiguating targets” section https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#disambiguating-targets to set up an attribute:

Core:

jvm('javaCommon') {
  attributes.attribute(jvmTargetAttribute, 'javaCommon')
}
jvm('desktop') {
  attributes.attribute(jvmTargetAttribute, 'desktop')
}

Debug plugin:

jvm('javaCommon') {
  attributes.attribute(jvmTargetAttribute, 'javaCommon')
}

(The debug plugin doesn’t have any desktop-specific code so targeting javaCommon is fine)

However I ran into a problem for downstream projects that don’t know they should select the desktop version of core, but the javaCommon version of debug-plugin. The attribute seems to be all-or-nothing.

I tried to create a fake ‘desktop’ sourceset for every project I had and leave them empty, but it still doesn’t compile because it can’t choose between the desktop and javaCommon variants. This seems to be triggered by desktop-only modules which I have written like this:

implementation project(path: ':plugins-debug', configuration: 'desktopDefault')

(Is that right?). There doesn’t seem to be a place to put this attribute outside of mpp.

I also tried directly linking to the right source set:

implementation project(path: ':core', configuration: 'default')  // Common
- or -
implementation project(path: ':core', configuration: 'javaCommonDefault')  // Java common
- or -
implementation project(path: ':core', configuration: 'desktopDefault')  // Desktop only

This works great on the command line and is really clean, but this confuses Android Studio. For the common sourceset only (not javacommon), it can’t resolve any classes or methods in that module at all so it’s hard to develop code. It’s like it doesn’t understand “configuration: ‘default’” is a dependency at all.

Any advice for a sane way to manage this?

Thanks!