# Multiplatform multiple native targets, same source code

**URL:** <https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276>\
**Category:** Native\
**Created:** [January 23, 2019, 1:12am UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276 "2019-01-23T01:12:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 23, 2019, 1:12am UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/1 "2019-01-23T01:12:45Z")

</div>

Say I have 2 expected declarations in my common module:

- the first one relies on K/N stdlib (`kotlin.system.getTimeMillis` for example)
- the other one relies on specific target os/arch stuff.

Is there a clever way to not write again the common stuff? Maybe something like:

```auto
common
   |
   |-native-common
      |
      |-native-A
      |-native-B

```

---

<div class="post-metadata">

**Author:** ![msink](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/msink/32/3336_2.png) [@msink](https://discuss.kotlinlang.org/u/msink)\
**Post date:** [January 23, 2019, 7:23am UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/2 "2019-01-23T07:23:02Z")

</div>

AFAIK in upcoming 1.3.20 this way works in Gradle, but not supported in IDEA.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 23, 2019, 12:22pm UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/3 "2019-01-23T12:22:58Z")

</div>

Yeah but which target should I pick? There is no “common-native” target or similar.

---

<div class="post-metadata">

**Author:** ![msink](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/msink/32/3336_2.png) [@msink](https://discuss.kotlinlang.org/u/msink)\
**Post date:** [January 23, 2019, 12:51pm UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/4 "2019-01-23T12:51:13Z")

</div>

Something like this:

```auto
sourceSets {
    nativeCommon
    nativeA {
        dependsOn nativeCommon
    }
    nativeB {
        dependsOn nativeCommon
    }
}

```

But as I said - it works from Gradle, but not from IDEA.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 23, 2019, 1:21pm UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/5 "2019-01-23T13:21:29Z")

</div>

Using Kotlin DSL of plugin 1.3.20-eap?

---

<div class="post-metadata">

**Author:** ![msink](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/msink/32/3336_2.png) [@msink](https://discuss.kotlinlang.org/u/msink)\
**Post date:** [January 23, 2019, 1:22pm UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/6 "2019-01-23T13:22:47Z")

</div>

Yes, tried in eap-100 and Kotlin DSL.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 23, 2019, 1:24pm UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/7 "2019-01-23T13:24:49Z")

</div>

```auto
plugins {
    id("kotlin-multiplatform") version "1.3.20-eap-100"
    id("maven-publish")
}
repositories {
    maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
    mavenCentral()
}
...
kotlin {
    jvm()
    js()
    nativeCommon() // <-- error here!
}
...

```

Auto-completion does not suggest anything with the name correlated to `commonNative`

---

<div class="post-metadata">

**Author:** ![msink](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/msink/32/3336_2.png) [@msink](https://discuss.kotlinlang.org/u/msink)\
**Post date:** [January 23, 2019, 1:32pm UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/8 "2019-01-23T13:32:57Z")

</div>

It’s not target, it’s source set.  
Something like this:

```auto
kotlin {
    sourceSets.create("nativeCommon")
    sourceSets["nativeA"].apply {
        dependsOn(sourceSets["nativeCommon"])
    }
    sourceSets["nativeB"].apply {
        dependsOn(sourceSets["nativeCommon"])
    }
}

```

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 23, 2019, 1:44pm UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/9 "2019-01-23T13:44:07Z")

</div>

Wow it works like a breeze! Thank you so much!  
I assume that this code sharing based on `sourceSets` works on every Kotlin project (maybe Java as well).  
Would it be possible to set `nativeCommon` source sets to a Kotlin/Native module? Even by hand every rebuild. That would at least remove some of the red warnings in those sources.

---

<div class="post-metadata">

**Author:** ![msink](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/msink/32/3336_2.png) [@msink](https://discuss.kotlinlang.org/u/msink)\
**Post date:** [January 23, 2019, 3:29pm UTC](https://discuss.kotlinlang.org/t/multiplatform-multiple-native-targets-same-source-code/11276/10 "2019-01-23T15:29:38Z")

</div>

I do not know, and as it does not work in IDEA still use old trick:

```auto
        sourceSets["macosxMain"].apply {
            kotlin.srcDir("src/nativeMain/kotlin")
        }
        sourceSets["linuxMain"].apply {
            kotlin.srcDir("src/nativeMain/kotlin")
        }

```
