How can I publish a javadoc.jar file with my Kotlin multiplatform project? - kotlin

I am trying to publish my Kotlin multiplatform library to Maven Central via Sonatype. This repository requires me to include a javadoc.jar file with my artifacts. Unfortunately, the IntelliJ IDEA project wizard and the Kotlin multiplatform docs do not help me do that. When running the Gradle task dokkaJavadoc (for the official Kotlin documentation tool Dokka), I get the error "Dokka Javadoc plugin currently does not support generating documentation for multiplatform project."
I actually do not need genuine JavaDocs for publishing - an empty javadoc.jar or one with other docs generated by Dokka would suffice. Since I have been a longtime Maven user and these are my first steps with Gradle, I have no idea how to do that.
build.gradle.kts:
plugins {
kotlin("multiplatform") version "1.4.31"
id("org.jlleitschuh.gradle.ktlint") version "10.0.0"
id("io.gitlab.arturbosch.detekt") version "1.15.0"
id("org.jetbrains.dokka") version "1.4.20"
id("maven-publish")
signing
}
group = "com.marcoeckstein"
version = "0.0.3-SNAPSHOT"
publishing {
publications {
create<MavenPublication>("maven") {
pom {
val projectGitUrl = "https://github.com/marco-eckstein/kotlin-lib"
name.set(rootProject.name)
description.set(
"A general-purpose multiplatform library. " +
"Implemented in Kotlin, usable also from Java, JavaScript and more."
)
url.set(projectGitUrl)
inceptionYear.set("2021")
licenses {
license {
name.set("MIT")
url.set("https://opensource.org/licenses/MIT")
}
}
developers {
developer {
id.set("marcoeckstein.com")
name.set("Marco Eckstein")
email.set("marco.eckstein#gmx.de")
url.set("https://www.marcoeckstein.com")
}
}
issueManagement {
system.set("GitHub")
url.set("$projectGitUrl/issues")
}
scm {
connection.set("scm:git:$projectGitUrl")
developerConnection.set("scm:git:$projectGitUrl")
url.set(projectGitUrl)
}
}
}
}
repositories {
maven {
name = "sonatypeStaging"
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2")
credentials(PasswordCredentials::class)
}
}
}
signing {
useGpgCmd()
sign(publishing.publications["maven"])
}
repositories {
mavenCentral()
jcenter()
}
kotlin {
targets.all {
compilations.all {
kotlinOptions {
allWarningsAsErrors = true
}
}
}
jvm {
compilations.all {
kotlinOptions.jvmTarget = "11"
}
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
js(BOTH) {
browser {
testTask {
useKarma {
useChromeHeadless()
webpackConfig.cssSupport.enabled = true
}
}
}
}
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("io.kotest:kotest-assertions-core:4.4.1")
}
}
val jvmMain by getting
val jvmTest by getting {
dependencies {
implementation(kotlin("test-junit5"))
implementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
runtimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
}
}
val jsMain by getting
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
val nativeMain by getting
val nativeTest by getting
}
}
configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
enableExperimentalRules.set(true)
verbose.set(true)
// ktlint.disabled_rules:
// filename:
// Caught more precisely (with desired exceptions) with detekt.
// import-ordering:
// ktlint's order is not supported (yet) by IntelliJ.
// See:
// - https://github.com/pinterest/ktlint/issues/527
// - https://youtrack.jetbrains.com/issue/KT-10974
// no-wildcard-imports:
// Not desired. We want them for Java statics and Enum members.
// experimental:annotation:
// Not desired.
// experimental:multiline-if-else:
// Not desired.
disabledRules.set(
setOf(
"filename",
"import-ordering",
"no-wildcard-imports",
"experimental:annotation",
"experimental:multiline-if-else"
)
)
additionalEditorconfigFile.set(file("$projectDir/.editorconfig"))
}
detekt {
input = files("$projectDir/src/")
config = files("$projectDir/detekt-config.yml")
buildUponDefaultConfig = true
}
tasks {
withType<io.gitlab.arturbosch.detekt.Detekt> {
// Target version of the generated JVM bytecode. It is used for type resolution.
jvmTarget = "11"
}
}
Also posted at Kotlin Discussions.

This answer is a cross-post from Kotlin Discussions. Credit goes to Lamba92_v2 of the JetBrains Team, who linked his solution in his project kotlingram.
I noticed I had another issue related to publishing: Signatures and POM information where not applied to all modules. But given Lamba92_v2's code I could resolve all publishing-related issues:
plugins {
kotlin("multiplatform") version "1.4.31"
id("org.jlleitschuh.gradle.ktlint") version "10.0.0"
id("io.gitlab.arturbosch.detekt") version "1.15.0"
id("org.jetbrains.dokka") version "1.4.20"
id("maven-publish")
signing
}
group = "com.marcoeckstein"
version = "0.0.3"
val dokkaHtml by tasks.getting(org.jetbrains.dokka.gradle.DokkaTask::class)
val javadocJar: TaskProvider<Jar> by tasks.registering(Jar::class) {
dependsOn(dokkaHtml)
archiveClassifier.set("javadoc")
from(dokkaHtml.outputDirectory)
}
publishing {
publications.withType<MavenPublication> {
artifact(javadocJar)
pom {
val projectGitUrl = "https://github.com/marco-eckstein/kotlin-lib"
name.set(rootProject.name)
description.set(
"A general-purpose multiplatform library. " +
"Implemented in Kotlin, usable also from Java, JavaScript and more."
)
url.set(projectGitUrl)
inceptionYear.set("2021")
licenses {
license {
name.set("MIT")
url.set("https://opensource.org/licenses/MIT")
}
}
developers {
developer {
id.set("marcoeckstein.com")
name.set("Marco Eckstein")
email.set("marco.eckstein#gmx.de")
url.set("https://www.marcoeckstein.com")
}
}
issueManagement {
system.set("GitHub")
url.set("$projectGitUrl/issues")
}
scm {
connection.set("scm:git:$projectGitUrl")
developerConnection.set("scm:git:$projectGitUrl")
url.set(projectGitUrl)
}
}
the<SigningExtension>().sign(this)
}
repositories {
maven {
name = "sonatypeStaging"
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2")
credentials(PasswordCredentials::class)
}
}
}
signing {
useGpgCmd()
}
repositories {
mavenCentral()
jcenter()
}
kotlin {
targets.all {
compilations.all {
kotlinOptions {
allWarningsAsErrors = true
}
}
}
jvm {
compilations.all {
kotlinOptions.jvmTarget = "11"
}
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
js(BOTH) {
browser {
testTask {
useKarma {
useChromeHeadless()
webpackConfig.cssSupport.enabled = true
}
}
}
}
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("io.kotest:kotest-assertions-core:4.4.1")
}
}
val jvmMain by getting
val jvmTest by getting {
dependencies {
implementation(kotlin("test-junit5"))
implementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
runtimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
}
}
val jsMain by getting
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
val nativeMain by getting
val nativeTest by getting
}
}
configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
enableExperimentalRules.set(true)
verbose.set(true)
// ktlint.disabled_rules:
// filename:
// Caught more precisely (with desired exceptions) with detekt.
// import-ordering:
// ktlint's order is not supported (yet) by IntelliJ.
// See:
// - https://github.com/pinterest/ktlint/issues/527
// - https://youtrack.jetbrains.com/issue/KT-10974
// no-wildcard-imports:
// Not desired. We want them for Java statics and Enum members.
// experimental:annotation:
// Not desired.
// experimental:multiline-if-else:
// Not desired.
disabledRules.set(
setOf(
"filename",
"import-ordering",
"no-wildcard-imports",
"experimental:annotation",
"experimental:multiline-if-else"
)
)
additionalEditorconfigFile.set(file("$projectDir/.editorconfig"))
}
detekt {
input = files("$projectDir/src/")
config = files("$projectDir/detekt-config.yml")
buildUponDefaultConfig = true
}
tasks {
withType<io.gitlab.arturbosch.detekt.Detekt> {
// Target version of the generated JVM bytecode. It is used for type resolution.
jvmTarget = "11"
}
}

Related

Kotlin Multiplatform. Cannot access class SqlDriver.Schema. Check your module classpath for missing or conflicting dependencies

I am trying to build a KMP library targeting iOS, Android, JS(Browser), Mac, Windows and Linux. For now I am only using Ktor and SQLDelight as a dependency. But getting the following issue in nativeMain's actual implementation while creating driver for SQLDelight
While the same code doesn't give any issue for iOS main which is also using the same NativeSqliteDriver (I need them separately since Ktor client for iOS and desktop platforms are separate).
Following is my build.gradle.kts
plugins {
kotlin("multiplatform") version "1.5.31"
id("maven-publish")
id("com.android.library")
kotlin("plugin.serialization") version "1.5.31"
id("com.squareup.sqldelight") version "1.5.3"
}
group = "me.group"
version = "1.0-SNAPSHOT"
val xcFrameworkName = "AddressLib"
repositories {
google()
mavenCentral()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
testRuns["test"].executionTask.configure {
useJUnit()
}
}
js(LEGACY) {
browser {
commonWebpackConfig {
cssSupport.enabled = true
}
}
}
val xcFramework = XCFramework(xcFrameworkName)
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
when {
hostOs == "Mac OS X" -> macosX64("native") {
binaries.framework(xcFrameworkName) {
xcFramework.add(this)
}
}
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
android()
ios {
binaries.framework(xcFrameworkName) {
xcFramework.add(this)
}
}
val coroutinesVersion = "1.5.2-native-mt"
val serializationVersion = "1.3.1"
val ktorVersion = "1.6.5"
val sqlDelightVersion = "1.5.3"
val napierVersion = "2.2.0"
val koinVersion = "3.1.4"
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
implementation("io.insert-koin:koin-core:$koinVersion")
implementation("io.github.aakira:napier:$napierVersion")
}
}
val commonTest by getting
val jvmMain by getting {
dependencies {
implementation("io.ktor:ktor-client-java:$ktorVersion")
implementation("com.squareup.sqldelight:sqlite-driver:$sqlDelightVersion")
}
}
val jvmTest by getting
val jsMain by getting {
dependencies {
implementation("io.ktor:ktor-client-js:$ktorVersion")
implementation("com.squareup.sqldelight:sqljs-driver:$sqlDelightVersion")
}
}
val jsTest by getting
val nativeMain by getting {
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val nativeTest by getting
val androidMain by getting {
dependencies {
implementation("io.ktor:ktor-client-android:$ktorVersion")
implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:$ktorVersion")
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val iosTest by getting
}
sqldelight {
database("AddressDatabase") {
packageName = "com.library.address.database"
}
}
}
android {
compileSdkVersion(31)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(31)
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
publishing {
repositories {
maven {
credentials {
username = "<username>"
password = "<pwd>"
}
url = URI("https://mymavenrepo.com")
}
}
}
So it seems the issue was somewhat due to same dependency being added to the build gradle twice and it's corresponding code being added twice as well. To solve the same I had to make a separate source set like the following
val sqlDriverNativeMain by creating {
dependsOn(commonMain)
dependencies {
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val iosMain by getting {
dependsOn(sqlDriverNativeMain)
dependencies {
implementation("io.ktor:ktor-client-ios:$ktorVersion")
}
}
val nativeMain by getting {
dependsOn(sqlDriverNativeMain)
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
}
}
and after that move the driver creation code inside the sourceSet directory named sqlDriverNativeMain. This resolved the issue.

KMM dependency issue in Shared Module

We are creating an app using KMM. But we are facing an issue, kSoap2 Library works fine in the android app but when this library used in Shared Directory of Android Studio, it doesn't work and gives error. The Same thing applies to Calendar. It is also not working, when writing Calendar.getInstance() the error says
Cannot access 'Kotlin.Cloneable'' which is a supertype of
'java.util.Calendar'. Check your module classpath for missing or
conflicting dependencies
build.gradle.kts(for Shared)
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
repositories {
google()
mavenCentral()
}
kotlin {
android()
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iosTarget("ios") {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
// implementation("com.facebook.android:facebook-android-sdk:[5,6)")
// implementation("com.google.android.material:material:1.3.0")
// implementation("com.google.firebase:firebase-messaging:10.2.1")
// implementation("com.google.android.gms:play-services:10.2.1")
// implementation("com.google.android.gms:play-services-ads:10.2.1")
// implementation("com.google.firebase:firebase-core:10.2.1")
// implementation("com.google.firebase:firebase-auth:10.2.1")
// implementation("com.github.siyamed:android-shape-imageview:0.9.+#AndreiA")
// implementation("com.github.barteksc:android-pdf-viewer:3.1.0-beta.1")
// implementation("org.apache.commons:commons-io:1.3.2")
implementation("com.google.android.material:material:1.3.0")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("androidx.legacy:legacy-support-v13:1.0.0")
implementation("androidx.core:core:1.0.0")
implementation("androidx.legacy:legacy-support-v4:1.0.0")
implementation("androidx.constraintlayout:constraintlayout:1.1.3")
implementation("com.android.support.constraint:constraint-layout:")
implementation("com.google.firebase:firebase-messaging:10.2.1")
implementation("com.google.android.gms:play-services:10.2.1")
implementation("com.google.android.gms:play-services-ads:10.2.1")
implementation("com.google.firebase:firebase-core:10.2.1")
implementation("com.google.firebase:firebase-auth:10.2.1")
implementation("com.github.siyamed:android-shape-imageview:0.9.+#AndreiA")
implementation("com.facebook.android:facebook-android-sdk:[5,6)")
implementation("androidx.mediarouter:mediarouter:1.0.0")
implementation("androidx.vectordrawable:vectordrawable:1.0.0")
implementation("com.github.barteksc:android-pdf-viewer:3.1.0-beta.1")
implementation("org.apache.commons:commons-io:1.3.2")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.30")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting
val iosTest by getting
}
}
apply { plugin("com.google.gms.google-services") }
android {
compileSdkVersion(30)
sourceSets["main"].manifest.srcFile("src//androidMain//AndroidManifest.xml")
defaultConfig {
minSdkVersion(17)
targetSdkVersion(30)
}
}
val packForXcode by tasks.creating(Sync::class) {
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)
val targetDir = File(buildDir, "xcode-frameworks")
group = "build"
dependsOn(framework.linkTask)
inputs.property("mode", mode)
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
dependencies {
implementation(files("/Users/mbp/AndroidStudioProjects/PashtoLibrary/androidApp/libs/ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar"))
implementation(files("/Users/mbp/AndroidStudioProjects/PashtoLibrary/androidApp/libs/org.apache.commons.net_2.0.0.v200905272248.jar"))
}
build.gradle.kts(for AndroidApp):
import org.jetbrains.kotlin.gradle.targets.js.npm.includedRange
plugins {
id("com.android.application")
kotlin("android")
}
repositories {
google()
jcenter()
}
dependencies {
/*implementation(project(":shared"))
implementation("com.google.android.material:material:1.3.0")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("androidx.constraintlayout:constraintlayout:2.0.4")*/
implementation(project(":shared"))
implementation("androidx.multidex:multidex:2.0.0")
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(files("libs/ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar"))
implementation(files("libs/pinchzoom.jar"))
implementation(files("libs/org.apache.commons.net_2.0.0.v200905272248.jar"))
implementation(files("libs/StartAppInApp-3.4.2.jar"))
implementation(files("libs/picasso-2.5.2.jar"))
// implementation("com.google.android.material:material:1.3.0")
// implementation("androidx.appcompat:appcompat:1.2.0")
//
// implementation("androidx.appcompat:appcompat:1.0.0")
// implementation("androidx.legacy:legacy-support-v13:1.0.0")
// implementation("androidx.core:core:1.0.0")
// implementation("androidx.legacy:legacy-support-v4:1.0.0")
//
//
// implementation("androidx.constraintlayout:constraintlayout:1.1.3")
// implementation("com.android.support.constraint:constraint-layout:")
// implementation("com.google.firebase:firebase-messaging:10.2.1")
// implementation("com.google.android.gms:play-services:10.2.1")
// implementation("com.google.android.gms:play-services-ads:10.2.1")
// implementation("com.google.firebase:firebase-core:10.2.1")
// implementation("com.google.firebase:firebase-auth:10.2.1")
// implementation("com.github.siyamed:android-shape-imageview:0.9.+#AndreiA")
// implementation("com.facebook.android:facebook-android-sdk:[5,6)")
// implementation("androidx.mediarouter:mediarouter:1.0.0")
// implementation("androidx.vectordrawable:vectordrawable:1.0.0")
// implementation("com.github.barteksc:android-pdf-viewer:3.1.0-beta.1")
// implementation("org.apache.commons:commons-io:1.3.2")
//
//
// implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.30")
}
apply { plugin("com.google.gms.google-services") }
android {
compileSdkVersion(30)
defaultConfig {
applicationId = "co.beyondsolutions.libraryapp"
minSdkVersion(17)
targetSdkVersion(30)
versionCode = 1
versionName = "1.0"
multiDexEnabled = true
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
}
All your android dependencies should be moved to the androidMain sourceSet from commonMain.
As a general rule in common you can only have dependencies that can compile to all your targets. Since your android dependencies are under the common source set it doesn't really know how to compile those to iOS
Here's an example of using the Android Arch viewmodel: https://github.com/halcyonmobile/MultiplatformPlayground/blob/master/common/src/androidMain/kotlin/com/halcyonmobile/multiplatformplayground/shared/CoroutineViewModel.kt

Kotlin/Native can't import io.ktor.network.selector.ActorSelectorManager

I want to use TCP Sockets in Kotlin/Native using KTOR and I followed the according tutorial. But somehow I can't import ActorSelectorManager from io.ktor.network.selector and get Unresolved reference: ActorSelectorManager
My build.gradle.kts looks like this:
plugins {
kotlin("multiplatform") version "1.4.32"
kotlin("plugin.serialization") version "1.4.32"
}
repositories {
mavenCentral()
jcenter()
maven { url = uri("https://plugins.gradle.org/m2/") }
maven { url = uri("http://dl.bintray.com/kotlin/kotlin-dev") }
maven { url = uri("https://kotlin.bintray.com/kotlinx") }
}
kotlin {
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val target = when {
hostOs == "Mac OS X" -> macosX64("SocketStuff")
hostOs == "Linux" -> linuxX64("SocketStuff")
isMingwX64 -> mingwX64("SocketStuff")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
target.apply {
binaries {
executable {
entryPoint = "main"
}
}
}
sourceSets {
val Main by getting
val Test by getting
commonMain {
sourceSets["commonMain"].dependencies {
implementation("io.ktor:ktor-network:1.5.3")
}
}
}
}
Does anyone know if I forgot to add a dependency or something?
There are the ActorSelectorManager on the JVM and the WorkerSelectorManager on the native target. You can use constructor function SelectorManager() to instantiate appropriate selector manager instance:
import io.ktor.network.selector.*
fun main() {
val manager = SelectorManager()
}

Publish kotlin multiplatform library to Maven Central (InvalidMavenPublicationException multiple artifacts with the identical ...)

As Jcenter will be shutdown soon I’m trying to migrate my libs to Maven Central. I have searched a lot to find any working script but with no luck. There is official docs, but it is like a joke, there just told to put maven-publish plugin to the gradle script and voila that’s it.
Currently I'm getting error:
Caused by: org.gradle.api.publish.maven.InvalidMavenPublicationException: Invalid publication 'js': multiple artifacts with the identical extension and classifier ('jar', 'sources').
My script looks like this:
plugins {
id("kotlin-multiplatform")
id("org.jetbrains.dokka") version "1.4.0-rc"
`maven-publish`
signing
}
kotlin {
sourceSets {
jvm()
js() {
nodejs()
browser()
}
linuxX64()
linuxArm64()
mingwX64()
macosX64()
iosArm64()
iosX64()
val commonMain by getting {
dependencies {
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jsMain by getting {
dependencies {
}
}
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
val jvmMain by getting {
dependencies {
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
}
}
val nativeMain by creating {
dependsOn(commonMain)
dependencies {
}
}
val linuxX64Main by getting {
dependsOn(nativeMain)
}
val linuxArm64Main by getting {
dependsOn(nativeMain)
}
val mingwX64Main by getting {
dependsOn(nativeMain)
}
val macosX64Main by getting {
dependsOn(nativeMain)
}
val iosArm64Main by getting {
dependsOn(nativeMain)
}
val iosX64Main by getting {
dependsOn(nativeMain)
}
}
}
tasks {
create<Jar>("javadocJar") {
dependsOn(dokkaJavadoc)
archiveClassifier.set("javadoc")
from(dokkaJavadoc.get().outputDirectory)
}
dokkaJavadoc {
println("Dokka !")
dokkaSourceSets {
create("commonMain") {
displayName = "common"
platform = "common"
}
}
}
}
// Publishing
val fis = FileInputStream("local.properties")
val properties = Properties().apply {
load(fis)
}
val ossUser = properties.getProperty("oss.user")
val ossPassword = properties.getProperty("oss.password")
extra["signing.keyId"] = properties.getProperty("signing.keyId")
extra["signing.password"] = properties.getProperty("signing.password")
extra["signing.secretKeyRingFile"] = properties.getProperty("signing.secretKeyRingFile")
val libraryVersion: String by project
val publishedGroupId: String by project
val artifactName: String by project
val libraryName: String by project
val libraryDescription: String by project
val siteUrl: String by project
val gitUrl: String by project
val licenseName: String by project
val licenseUrl: String by project
val developerOrg: String by project
val developerName: String by project
val developerEmail: String by project
val developerId: String by project
project.group = publishedGroupId
project.version = libraryVersion
signing {
sign(publishing.publications)
}
publishing {
publications.withType(MavenPublication::class) {
groupId = publishedGroupId
artifactId = artifactName
version = libraryVersion
artifact(tasks["javadocJar"])
artifact(tasks["sourcesJar"])
pom {
name.set(libraryName)
description.set(libraryDescription)
url.set(siteUrl)
licenses {
license {
name.set(licenseName)
url.set(licenseUrl)
}
}
developers {
developer {
id.set(developerId)
name.set(developerName)
email.set(developerEmail)
}
}
organization {
name.set(developerOrg)
}
scm {
connection.set(gitUrl)
developerConnection.set(gitUrl)
url.set(siteUrl)
}
}
}
repositories {
maven("https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
name = "sonatype"
credentials {
username = ossUser
password = ossPassword
}
}
}
}
I also find this reddit topic with no solution, this article that doesn't work, and lot of others. There are tons of materials how to publish to bintray, but they are irrelevant now
It seems the issue was in this line artifact(tasks["sourcesJar"]) as this task already included.
Here I want to put my working script for uploading kotlin multiplatform library to Maven Central.
First of all we need to register Sonatype account, validate our domain, etc, here is a fresh article with detailed steps.
Then your project script build.gradle.kts may look like this:
import java.io.FileInputStream
import java.util.Properties
import org.gradle.api.publish.PublishingExtension
plugins {
id("kotlin-multiplatform")
id("org.jetbrains.dokka") version "1.4.0-rc"
id("io.codearte.nexus-staging") version "0.22.0"
`maven-publish`
signing
}
enum class OS {
LINUX, WINDOWS, MAC
}
fun getHostOsName(): OS =
System.getProperty("os.name").let { osName ->
when {
osName == "Linux" -> OS.LINUX
osName.startsWith("Windows") -> OS.WINDOWS
osName.startsWith("Mac") -> OS.MAC
else -> throw GradleException("Unknown OS: $osName")
}
}
kotlin {
sourceSets {
jvm()
js() {
browser()
nodejs()
}
when (getHostOsName()) {
OS.LINUX -> {
linuxX64()
linuxArm64()
}
OS.WINDOWS -> {
mingwX64()
}
OS.MAC -> {
macosX64()
iosArm64()
iosX64()
}
}
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
implementation(Libs.olekdia.common)
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jvmMain by getting {
dependencies {
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
}
}
val jsMain by getting {
dependencies {
}
}
val nativeMain by creating {
dependsOn(commonMain)
}
when (getHostOsName()) {
OS.LINUX -> {
val linuxX64Main by getting {
dependsOn(nativeMain)
}
val linuxArm64Main by getting {
dependsOn(nativeMain)
}
}
OS.WINDOWS -> {
val mingwX64Main by getting {
dependsOn(nativeMain)
}
}
OS.MAC -> {
val macosX64Main by getting {
dependsOn(nativeMain)
}
val iosArm64Main by getting {
dependsOn(nativeMain)
}
val iosX64Main by getting {
dependsOn(nativeMain)
}
}
}
}
}
tasks {
create<Jar>("javadocJar") {
dependsOn(dokkaJavadoc)
archiveClassifier.set("javadoc")
from(dokkaJavadoc.get().outputDirectory)
}
dokkaJavadoc {
dokkaSourceSets {
create("commonMain") {
displayName = "common"
platform = "common"
}
}
}
}
//--------------------------------------------------------------------------------------------------
// Publishing
//--------------------------------------------------------------------------------------------------
val fis = FileInputStream("local.properties")
val properties = Properties().apply {
load(fis)
}
val ossUser = properties.getProperty("oss.user")
val ossPassword = properties.getProperty("oss.password")
extra["signing.keyId"] = properties.getProperty("signing.keyId")
extra["signing.password"] = properties.getProperty("signing.password")
extra["signing.secretKeyRingFile"] = properties.getProperty("signing.secretKeyRingFile")
val libraryVersion: String by project
val publishedGroupId: String by project
val artifactName: String by project
val libraryName: String by project
val libraryDescription: String by project
val siteUrl: String by project
val gitUrl: String by project
val licenseName: String by project
val licenseUrl: String by project
val developerOrg: String by project
val developerName: String by project
val developerEmail: String by project
val developerId: String by project
project.group = publishedGroupId
project.version = libraryVersion
signing {
sign(publishing.publications)
}
afterEvaluate {
configure<PublishingExtension> {
publications.all {
val mavenPublication = this as? MavenPublication
mavenPublication?.artifactId =
"${project.name}${"-$name".takeUnless { "kotlinMultiplatform" in name }.orEmpty()}"
}
}
}
publishing {
publications.withType(MavenPublication::class) {
groupId = publishedGroupId
artifactId = artifactName
version = libraryVersion
artifact(tasks["javadocJar"])
pom {
name.set(libraryName)
description.set(libraryDescription)
url.set(siteUrl)
licenses {
license {
name.set(licenseName)
url.set(licenseUrl)
}
}
developers {
developer {
id.set(developerId)
name.set(developerName)
email.set(developerEmail)
}
}
organization {
name.set(developerOrg)
}
scm {
connection.set(gitUrl)
developerConnection.set(gitUrl)
url.set(siteUrl)
}
}
}
repositories {
maven("https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
name = "sonatype"
credentials {
username = ossUser
password = ossPassword
}
}
}
}
nexusStaging {
username = ossUser
password = ossPassword
packageGroup = publishedGroupId
}
Provide needed library details in gradle.properties:
libraryVersion = 0.1.1
libraryName = Your library name
libraryDescription = Your library description
publishedGroupId = com.yourdomain
artifactName = your-cool-librayr
siteUrl = https://gitlab.com/yourlibrayr
gitUrl = https://gitlab.com/yourlibrayr.git
developerId = ...
developerOrg = ...
developerName = Your Name
developerEmail = yourmail#mail.com
licenseName = The Apache Software License, Version 2.0
licenseUrl = http://www.apache.org/licenses/LICENSE-2.0.txt
allLicenses = ["Apache-2.0"]
kotlin.mpp.enableGranularSourceSetsMetadata = true
gnsp.disableApplyOnlyOnRootProjectEnforcement = true
Here gnsp.disableApplyOnlyOnRootProjectEnforcement = true property needed for declaring nexusStaging in subprojects.
And finally put your credits to local.properties:
oss.user=your_user_name
oss.password=your_pass
signing.keyId=last_8_numbers_of_key
signing.password=your_pass
signing.secretKeyRingFile=/path/to/keystorage.gpg
Now for publishing open terminal in project directory:
./gradlew build
./gradlew publish
./gradlew closeAndReleaseRepository
You could skip last command, and close and release staging packages from Nexus repository manager. That nexus-staging plugin is only needed to do it from command line.
I have tried to move publishing part of script to separate file, and include it with apply(from = "publish.gradle.kts"), but it didn't work, as it loses context in separate file
I use older version of dokka library (1.4.0-rc), as newer version could not generate javadocs for all platforms. And this javadocs is required by repository for publishing. As authors mentioned we could generate empty javadoc.jar file for that purpose.

Intellij autocomplete failing on multiplatform library dependencies

I've created a multiplatform library that I consume in a different multiplatform project. After including the dependency I am able to use the libraries in commonMain without issue and all targets build without errors. However, intellij autocomplete does not know about my new types. I have invalidated caches and restarted the application many times, but it just cannot find this type. Anybody encountered this?
just in case, here is my build.gradle.kts:
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
buildscript {
repositories {
jcenter()
}
}
plugins {
kotlin("multiplatform") version "1.3.61"
}
repositories {
jcenter()
maven( "https://dl.bintray.com/kotlin/ktor" )
mavenCentral()
mavenLocal()
}
val ktor_version = "1.1.3"
val logback_version = "1.2.3"
kotlin {
js {
browser { }
}
jvm {
compilations.named("main") {
tasks.getByName<Copy>(processResourcesTaskName) {
dependsOn("jsBrowserWebpack")
tasks.named<KotlinWebpack>("jsBrowserWebpack") {
from(entry.name, destinationDirectory)
}
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
api("github.fatalcatharsis:constraint:1.0-SNAPSHOT")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jvmMain by getting {
dependencies {
implementation( kotlin("stdlib-jdk8"))
implementation( "io.ktor:ktor-server-netty:$ktor_version")
implementation( "io.ktor:ktor-html-builder:$ktor_version")
implementation( "ch.qos.logback:logback-classic:$logback_version")
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-testng"))
}
}
val jsMain by getting {
dependencies {
implementation( kotlin("stdlib-js"))
}
}
val jsTest by getting {
dependencies {
implementation( kotlin("test-js"))
}
}
}
}
tasks.register<JavaExec>("run") {
dependsOn("jvmJar")
group = "application"
main = "sample.SampleJvmKt"
val t = tasks.named<Jar>("jvmJar")
classpath(configurations.named("jvmRuntimeClasspath"), t.get() )
}