https://kotlinlang.org logo
g

Guilherme Cordeiro

02/23/2021, 8:49 PM
Hello! Maybe I am doing something wrong without realizing, but I am struggling to share code between different KMM modules... If I have: 1. A base KMM module, with code both on
commonMain
and
commonTest
that I want to share (like frameworks, contracts, test utilities, etc) 2. Several other independently built modules, depending on the base module What is the proper way to add the base module on the dependencies of the child modules? Inside the
sourceSets
block, for both
commonMain
and
commonTest
? Can I access classes defined on the base module
commonTest
inside a child module
commonTest
?
I tried to add like this:
Copy code
sourceSets {
    val commonMain by getting {
        dependencies {
            api(project(":base"))
        }
    }
    val commonTest by getting {
        dependencies {
            api(project(":base"))
        }
    }
}
but although I can import classes from base
commonTest
on the child module
commonTest
, I get an
Unresolved reference
error when compiling 😕
Thanks in advance for any suggestions or corrections 🙏
e

emmanuelkehinde

02/23/2021, 10:39 PM
Willing to know more about how this goes 👌🏽
a

Arkadii Ivanov

02/24/2021, 12:20 AM
AFAIK modules can't depend on other modules test source sets.
t

Tijl

02/24/2021, 7:44 AM
just put your test utils in the main sources of a separate module, and then have test source sets depend on that module
g

Guilherme Cordeiro

02/24/2021, 12:26 PM
I suspected that, but want to make sure... I'l try the suggestion of the separate module for now. Thanks!! ❤️
r

russhwolf

02/24/2021, 1:32 PM
I've been using the separate module trick for a couple years now. It has occasionally led to tooling issues (I think it was around 1.3.40 I couldn't build due to some weird compiler/gradle plugin bugs) but for the most part it's worked well.
a

Arkadii Ivanov

02/24/2021, 3:55 PM
The only issue with separate module for test utils is that the code is treated as production, not as test. E.g. detekt may not ignore some relaxed checks, IDEA shows warnings about test function names, etc.
g

Guilherme Cordeiro

02/25/2021, 12:34 PM
I see... I agree it's not ideal, but acceptable for now as kmp is still in early stages of development... I'll bear the limitations on mind, thanks for all the orientations! 🙇
👍 1
t

Tijl

02/25/2021, 3:55 PM
this is not a kmp limitation, test sourcesets are just not exportable in gradle
1
🙇 1
today i learned 1
g

Guilherme Cordeiro

02/25/2021, 7:04 PM
Nice, I didn't know that! Still so much to learn, but mistake by mistake we put the pieces together 😄 thanks again ❤️