https://kotlinlang.org logo
r

russhwolf

02/11/2019, 4:26 AM
Can I always assume sources named
commonMain
and
commonTest
will be present in
project.kotlin.sourceSets
? If not, is there a way to dynamically detect the common sources? I was looking at the
metadata
target but it only has a
main
compilation and no
test
.
The context here is a gradle plugin that will involve code generation. I want to understand what assumptions I can and can’t make about where to put common sources.
j

josephivie

02/11/2019, 7:11 AM
Close enough, I'd say. Every project I've seen use commonMain and commonTest, and if I saw correctly in my tests, such source sets will always exist, even if unused. Out of curiosity, you interested in sharing what you are building? Sounds interesting
g

GarouDan

02/11/2019, 10:31 AM
Well, as far as I know we will always have a commonMain and a commonTest source set. Reference: https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html
Copy code
These are the default source set names for the production and test sources for the targets configured above. The source sets commonMain and commonTest are included into production and test compilations, respectively, of all targets. Note that the dependencies for common source sets commonMain and commonTest are the common artifacts, and the platform libraries go to the source sets of the specific targets.
Also you can do something like:
Copy code
js().compilations.main.defaultSourceSet  { /* ... */ }
js().compilations.test.defaultSourceSet { /* ... */ }
I don’t know what about using project.kotlin.sourceSets, but I think it should work.
One other way to access the compilations, is:
kotlin.targets."$target".compilations.main
where I got this from:
Copy code
task copyFramework {
    def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
    def target = project.findProperty('kotlin.target') ?: 'ios'
    dependsOn kotlin.targets."$target".compilations.main.linkTaskName('FRAMEWORK', buildType)

    doLast {
        def srcFile = kotlin.targets."$target".compilations.main.getBinary('FRAMEWORK', buildType)
        def targetDir = getProperty('configuration.build.dir')
        copy {
            from srcFile.parent
            into targetDir
            include 'app.framework/**'
            include 'app.framework.dSYM'
        }
    }
}
h

h0tk3y

02/11/2019, 12:25 PM
Yes,
commonMain
and
commonTest
source sets are always created by the
kotlin-multiplatform
plugin, you can expect them to be there if that plugin is applied. However, other plugins that create the
kotlin
extension don't create these source sets, so you should check if they are present unless you are targeting MPP only.
There's currently no other API to access them both. If you feel such an API would be appropriate, please file a feature request.
r

russhwolf

02/11/2019, 2:25 PM
@GarouDan Yes I've been doing things like you describe to detect platform sources in a dynamic way. But I wasn't sure if I should also be detecting common sources dynamically @h0tk3y Thanks, this gives me the confirmation I need. I don't think I need a new API to dynamically detect them for my current use-case, as long as I know that they're always there.
11 Views