Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Q: How to reuse target sourceSet for another target? (MPP) #2577

Closed
4ntoine opened this issue Jan 24, 2019 · 6 comments
Closed

Q: How to reuse target sourceSet for another target? (MPP) #2577

4ntoine opened this issue Jan 24, 2019 · 6 comments

Comments

@4ntoine
Copy link

4ntoine commented Jan 24, 2019

I need to reuse jvm sourceSet for android target.
In 1.3.11 it worked:

 fromPreset(presets.jvm, 'jvm')
 fromPreset(presets.android, 'jvm')

In 1.3.20 it stopped working:

The target 'jvm' already exists, but it was not created with the 'android' preset. To configure it, access it by name in kotlin.targets or use the preset function 'jvm' Open File

BTW i believe it should be by default: android target to use jvm sourceSet (and overrides) existing source if present.

I've tried to add jvm default source set to android target but no luck:

  jvm()
  android() {
     sourceSets.add(kotlin.targets.jvm.compilations.main.defaultSourceSet)
  }

Actual impl not found for expected class.

Bug/feature? How can i do it?

@Dominaezzz
Copy link
Contributor

You could point their defaultSourceSets to the same directory using kotlin.srcDir().
Like this https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#configuring-source-sets

@4ntoine
Copy link
Author

4ntoine commented Jan 25, 2019

Well, it can be a solution probably. But it would make all android sources (actual) equal to jvm. I need to add jvm to android but not limit to it.

@Dominaezzz
Copy link
Contributor

I had a similar problem in a project of mine. You could do one of two things.
Declare a common source set for Android and jvm and then have their specific source sets depend on the common one.

Have android and jvm share a source folder, then android can have another specific source folder and jvm can have another source folder.

@4ntoine
Copy link
Author

4ntoine commented Jan 25, 2019

@Dominaezzz That should work. The whole difficulty for me is still the same - how to add sourceset for platform target. Now it's just "add shared between jvm and android targets sourceset instead of just jvm's sourceset". Can you please share build.gradle part for this?

@Dominaezzz
Copy link
Contributor

Sharing a source set.

jvm ()
android()

sourceSets {
    val commonJvmMain by creating {
        dependencies {
            implementation(kotlin("stdlib-common"))
        }
    }
    val jvmMain by getting {
        dependsOn(commonJvmMain)
    }
    val androidMain by getting {
        dependsOn(commonJvmMain)
    }
}

or

Sharing a source directory.

jvm ()
android()

sourceSets {
    val jvmMain by getting {
        kotlin.srcDir("src/commonJvmMain/kotlin")
        resources.srcDir("src/commonJvmMain/resources")
    }
    val androidMain by getting {
        kotlin.srcDir("src/commonJvmMain/kotlin")
        resources.srcDir("src/commonJvmMain/resources")
    }
}

Both of these should work but the first one might play nicer with the IDE.
(Haven't tested this exact config, I don't have my computer at the moment).

@4ntoine
Copy link
Author

4ntoine commented Jan 25, 2019

That worked (Groovy):

       commonJvmMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
            }
        }
        jvmMain {
            dependsOn commonJvmMain
        }
        androidMain {
            dependsOn commonJvmMain
        }

Thanks, @Dominaezzz !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants