How to use .klib library in Kotlin/Multiplatform project - gradle

I want to use C code in my Kotlin/Multiplatform project (common level).
By this article:
https://theprogrammershangout.com/resources/kotlin/native/creating-c-bindings.md/
I got my .klib file and now I want to add it to my IntelliJ Kotlin/Multiplatform project.
I'm trying to add this to Gradle without success:
implementation files("/path/to/App.klib")
The project is built successfully but the importing App is not resolved.
What am I missing?
I already searched all over the internet and did not found anything helpful.
My build.gradle:
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
}
}
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.4.10'
id 'maven-publish'
}
apply plugin: 'com.android.library'
repositories {
mavenCentral()
google()
jcenter()
}
group 'com.texel.examples.kotlinnative'
version '0.0.1'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName '1.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
}
}
sourceSets {
main {
manifest.srcFile 'src/jvmMain/AndroidManifest.xml'
java.srcDirs = ['src/jvmMain/kotlin']
res.srcDirs = ['src/jvmMain/resources']
}
}
}
dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
kotlin {
// This is for iPhone emulator
// Switch here to iosArm64 (or iosArm32) to build library for iPhone device
// iosX64("ios") {
// binaries {
// framework()
// }
// }
macosX64("maccos") {
binaries {
framework()
}
}
android("android") {
// you can also publish both "release" and "debug"
publishLibraryVariants("release")
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common:1.4.10'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9-native-mt"
implementation files("/Users/user/Downloads/App.klib")
}
}
jvmMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
}
jsMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
}
}
macosMain {
dependencies {
}
}
iosMain {
dependencies {
}
}
}
}
configurations {
compileClasspath
}
Many thanks.

Here is the official documentation: C Interop and also an Using C Interop and libcurl for an App
(you may prefer to look at the https://github.com/JetBrains/kotlin-native/tree/master/samples/libcurl instead)
As I see the gradle setup is pretty different:
hostTarget.apply {
compilations["main"].cinterops {
val libcurl by creating {
when (preset) {
presets["macosX64"] -> includeDirs.headerFilterOnly("/opt/local/include", "/usr/local/include")
presets["linuxX64"] -> includeDirs.headerFilterOnly("/usr/include", "/usr/include/x86_64-linux-gnu")
presets["mingwX64"] -> includeDirs.headerFilterOnly(mingwPath.resolve("include"))
}
}
}

Related

Kotlin multiplatform publish with dokka and sources

I am struggling to publish a Kotlin multiplatform project properly to maven (for now mavenLocal). I can add the dependency to another multiplatform project and use the code but I don't get any documentation and I am not sure if I am doing something wrong or if that is simply not possible at the moment.
From what I understand you cannot use the normal javadoc because it is bound to Java which does not make sense in a multiplatform environment. I read somewhere that in that case you should use the html version of dokka. I can see that I get a "javadoc.jar" with content to my mavenLocal but still in the IDE in an example project where I add my KMP library as a dependency, I don't see any documentation.
Also, the code decompiling seems to be weird. I guess somehow the sources are also not properly resolved.
According to the documentation everything should automatically and perfectly work by simply adding the maven-publish and the dokka plugin. But it seems like this is not the case and actually nothing is working as I'd expect it :D
Does anyone know, how to properly set that up?
My gradle file looks like this:
plugins {
kotlin("multiplatform") version "1.6.21"
id("org.jetbrains.kotlinx.benchmark") version "0.4.2"
id("org.jetbrains.dokka") version "1.6.21"
`maven-publish`
signing
}
group = "io.github.quillraven.fleks"
version = "1.4-KMP-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
kotlin {
targets {
jvm {
compilations {
all {
kotlinOptions {
jvmTarget = "1.8"
}
}
val main by getting { }
// custom benchmark compilation
val benchmarks by compilations.creating {
defaultSourceSet {
dependencies {
// Compile against the main compilation's compile classpath and outputs:
implementation(main.compileDependencyFiles + main.output.classesDirs)
}
}
}
}
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
}
js(BOTH) {
browser { }
}
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"))
}
}
val jvmMain by getting
val jvmTest by getting
val jvmBenchmarks by getting {
dependsOn(commonMain)
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.2")
implementation("com.badlogicgames.ashley:ashley:1.7.4")
implementation("net.onedaybeard.artemis:artemis-odb:2.3.0")
}
}
val jsMain by getting
val jsTest by getting
val nativeMain by getting
val nativeTest by getting
}
}
benchmark {
targets {
register("jvmBenchmarks")
}
}
val javadocJar by tasks.registering(Jar::class) {
archiveClassifier.set("javadoc")
from(tasks.dokkaHtml)
}
publishing {
repositories {
maven {
url = if (project.version.toString().endsWith("SNAPSHOT")) {
uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
} else {
uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
}
credentials {
username = System.getenv("OSSRH_USERNAME")
password = System.getenv("OSSRH_TOKEN")
}
}
}
publications {
val kotlinMultiplatform by getting(MavenPublication::class) {
version = project.version.toString()
groupId = project.group.toString()
artifactId = "Fleks"
artifact(javadocJar)
pom {
name.set("Fleks")
description.set("A lightweight entity component system written in Kotlin.")
url.set("https://github.com/Quillraven/Fleks")
scm {
connection.set("scm:git:git#github.com:quillraven/fleks.git")
developerConnection.set("scm:git:git#github.com:quillraven/fleks.git")
url.set("https://github.com/quillraven/fleks/")
}
licenses {
license {
name.set("MIT License")
url.set("https://opensource.org/licenses/MIT")
}
}
developers {
developer {
id.set("Quillraven")
name.set("Simon Klausner")
email.set("quillraven#gmail.com")
}
}
}
}
signing {
useInMemoryPgpKeys(System.getenv("SIGNING_KEY"), System.getenv("SIGNING_PASSWORD"))
sign(kotlinMultiplatform)
}
}
}
// only sign if version is not a SNAPSHOT release.
// this makes it easier to publish to mavenLocal and test the packed version.
tasks.withType<Sign>().configureEach {
onlyIf { !project.version.toString().endsWith("SNAPSHOT") }
}
When I run the publishToMavenLocal gradle task then I get following directories in my .m2 folder:
When I then create an example project and add it as a dependency, then I don't see any quick documentation and also the decompiling is not working properly:
I was finally able to publish to mavenCentral. Not sure if this is the best way and correct way to do it, but it seems to work. Here is my build.gradle.kts. The important part is in the publications sections. For whatever reason it is not sufficient to have the "root" folder setup properly. I also had to adjust the pom, javadoc and signing of every single publication, too.
#file:Suppress("UNUSED_VARIABLE")
plugins {
kotlin("multiplatform") version "1.6.21"
id("org.jetbrains.kotlinx.benchmark") version "0.4.2"
id("org.jetbrains.dokka") version "1.6.21"
`maven-publish`
signing
}
group = "io.github.quillraven.fleks"
version = "1.4-KMP-RC1"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
kotlin {
targets {
jvm {
compilations {
all {
kotlinOptions {
jvmTarget = "1.8"
}
}
val main by getting { }
// custom benchmark compilation
val benchmarks by compilations.creating {
defaultSourceSet {
dependencies {
// Compile against the main compilation's compile classpath and outputs:
implementation(main.compileDependencyFiles + main.output.classesDirs)
}
}
}
}
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
}
js(BOTH) {
browser { }
}
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"))
}
}
val jvmMain by getting
val jvmTest by getting
val jvmBenchmarks by getting {
dependsOn(commonMain)
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.2")
implementation("com.badlogicgames.ashley:ashley:1.7.4")
implementation("net.onedaybeard.artemis:artemis-odb:2.3.0")
}
}
val jsMain by getting
val jsTest by getting
val nativeMain by getting
val nativeTest by getting
}
}
benchmark {
targets {
register("jvmBenchmarks")
}
}
val javadocJar by tasks.registering(Jar::class) {
archiveClassifier.set("javadoc")
from(tasks.dokkaHtml)
}
publishing {
repositories {
maven {
url = if (project.version.toString().endsWith("SNAPSHOT")) {
uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
} else {
uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
}
credentials {
username = System.getenv("OSSRH_USERNAME")
password = System.getenv("OSSRH_TOKEN")
}
}
}
publications {
val kotlinMultiplatform by getting(MavenPublication::class) {
// we need to keep this block up here because
// otherwise the different target folders like js/jvm/native are not created
version = project.version.toString()
groupId = project.group.toString()
artifactId = "Fleks"
}
}
publications.forEach {
if (it !is MavenPublication) {
return#forEach
}
// We need to add the javadocJar to every publication
// because otherwise maven is complaining.
// It is not sufficient to only have it in the "root" folder.
it.artifact(javadocJar)
// pom information needs to be specified per publication
// because otherwise maven will complain again that
// information like license, developer or url are missing.
it.pom {
name.set("Fleks")
description.set("A lightweight entity component system written in Kotlin.")
url.set("https://github.com/Quillraven/Fleks")
scm {
connection.set("scm:git:git#github.com:quillraven/fleks.git")
developerConnection.set("scm:git:git#github.com:quillraven/fleks.git")
url.set("https://github.com/quillraven/fleks/")
}
licenses {
license {
name.set("MIT License")
url.set("https://opensource.org/licenses/MIT")
}
}
developers {
developer {
id.set("Quillraven")
name.set("Simon Klausner")
email.set("quillraven#gmail.com")
}
}
}
signing {
useInMemoryPgpKeys(System.getenv("SIGNING_KEY"), System.getenv("SIGNING_PASSWORD"))
sign(it)
}
}
}
// only sign if version is not a SNAPSHOT release.
// this makes it easier to publish to mavenLocal and test the packed version.
tasks.withType<Sign>().configureEach {
onlyIf { !project.version.toString().endsWith("SNAPSHOT") }
}
Here is my example gradle file that publishes a kotlin multiplatform project with sources and javadocs to a maven repository.
The project source is here: https://github.com/danbrough/misc_demos/tree/master/kotlin_multiplatform_dokka
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("multiplatform")
id("org.jetbrains.dokka")
id("com.android.library")
`maven-publish`
}
group = "dokka.test"
version = "0.0.1"
buildscript {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
repositories {
mavenCentral()
google()
}
kotlin {
jvm()
android()
linuxX64()
macosX64()
sourceSets {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
}
val posixMain by sourceSets.creating {}
targets.withType<KotlinNativeTarget>() {
compilations["main"].defaultSourceSet.dependsOn(posixMain)
}
}
tasks.withType<AbstractTestTask>() {
testLogging {
events = setOf(
TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED
)
exceptionFormat = TestExceptionFormat.FULL
showStandardStreams = true
showStackTraces = true
}
outputs.upToDateWhen {
false
}
}
tasks.withType(KotlinCompile::class) {
kotlinOptions {
jvmTarget = "11"
}
}
tasks.dokkaHtml.configure {
outputDirectory.set(buildDir.resolve("dokka"))
}
val javadocJar by tasks.registering(Jar::class) {
archiveClassifier.set("javadoc")
from(tasks.dokkaHtml)
}
publishing {
repositories {
maven(project.buildDir.resolve("m2").toURI()) {
name = "m2"
}
}
publications.forEach {
if (it !is MavenPublication) {
return#forEach
}
it.artifact(javadocJar)
}
}
android {
compileSdk = 33
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
namespace = project.group.toString()
defaultConfig {
minSdk = 23
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
signingConfigs.register("release") {
storeFile = File(System.getProperty("user.home"), ".android/keystore")
keyAlias = "keyAlias"
storePassword = System.getenv("KEYSTORE_PASSWORD") ?: ""
keyPassword = System.getenv("KEYSTORE_PASSWORD") ?: ""
}
lint {
abortOnError = false
}
buildTypes {
getByName("debug") {
//debuggable(true)
}
getByName("release") {
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
signingConfig = signingConfigs.getByName("release")
}
}
}

Gradle Configuration for Kotlin Multiplatform Project in Java and Js

Seems all ok! But...
I have created a Kotlin Multiplatform Project in Js and Java. I worked on create the right tests for all target and the right configuration for the build. Seems all go right, i managed to create the build and set it in the right way. Today i have opened the project and it stop, the build complete successfull but test and java compilations don't be executed.
So how i can configure it?
With code and result all will be more clear
build.gradle.kts
import com.moowork.gradle.node.npm.NpmTask
import com.moowork.gradle.node.task.NodeTask
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50")
}
}
plugins {
kotlin("multiplatform") version "1.3.50"
id("com.moowork.node") version "1.3.1"
}
repositories {
mavenCentral()
jcenter()
}
group = "com.example"
version = "0.0.1"
kotlin {
jvm()
js()
jvm {
withJava()
}
js {
nodejs()
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
jvm {
compilations["main"].defaultSourceSet {
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
}
compilations["test"].defaultSourceSet {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
}
}
}
js {
sequenceOf("", "Test").forEach {
tasks.getByName<KotlinJsCompile>("compile${it}KotlinJs") {
kotlinOptions {
moduleKind = "umd"
noStdlib = true
metaInfo = true
}
}
}
compilations["main"].defaultSourceSet {
dependencies {
implementation(kotlin("stdlib-js"))
}
}
compilations["test"].defaultSourceSet {
dependencies {
implementation(kotlin("test-js"))
}
}
}
}
}
val compileKotlinJs = tasks.getByName("compileKotlinJs")
val compileTestKotlinJs = tasks.getByName("compileTestKotlinJs")
val libDir = "$buildDir/lib"
val compileOutput = compileKotlinJs.getOutputs().getFiles()
val testOutput = compileTestKotlinJs.getOutputs().getFiles()
val populateNodeModules = tasks.create<Copy>("populateNodeModules") {
afterEvaluate {
from(compileOutput)
from(testOutput)
configurations["testCompile"].forEach {
if (it.exists() && !it.isDirectory) {
from(zipTree(it.absolutePath).matching { include("*.js") })
}
}
for (sourceSet in kotlin.sourceSets) {
from(sourceSet.resources)
}
into("$buildDir/node_modules")
}
dependsOn("compileKotlinJs")
}
node {
download = true;
}
tasks.create<NpmTask> ("installJest") {
setArgs(setOf("install", "jest"))
}
tasks.create<NodeTask> ("runJest") {
setDependsOn(setOf("installJest", "populateNodeModules", "compileTestKotlinJs"))
setScript(file("node_modules/jest/bin/jest.js"))
setArgs(compileTestKotlinJs.outputs.files.toMutableList().map {projectDir.toURI().relativize(it.toURI())})
}
tasks.getByName("test").dependsOn("runJest")
Look how many task are skiped! and how build Dir is created
buid result
build dir
I use jest to test in js.
Thanks in advance for support me

Kotlin Gradle DSL JSONObject not mocked

Since I converted my Groovy to Kotlin DSL gradle, my unittests are not working anymore. I get the error:
java.lang.RuntimeException: Method get in org.json.JSONObject not mocked. See http://g.co/androidstudio/not-mocked for details.
So i followed the link and added testoptions to all my build.gradle.kts files.
But after this it still doesn't work.
My (builsSrc) build.gradle.kts file:
plugins {
`kotlin-dsl`
}
repositories {
google()
jcenter()
}
My (App) build.gradle.kts file:
plugins {
id("com.android.library")
kotlin("android")
kotlin("android.extensions")
}
android {
compileSdkVersion(Versions.Android.compileSdkVersion)
defaultConfig {
versionCode = Versions.Android.appVersionCode
versionName = Versions.Android.appVersionName
minSdkVersion(Versions.Android.minSdkVersion)
targetSdkVersion(Versions.Android.targetSdkVersion)
testInstrumentationRunner = Config.Test.instrumentationRunner
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
testOptions {
unitTests.setReturnDefaultValues(true)
}
}
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
implementation(Depends.Kotlin.reflect)
implementation(Depends.Kotlin.kotlinStdLib)
testImplementation(Depends.TestLibraries.json)
testImplementation(Depends.TestLibraries.jUnit)
androidTestImplementation(Depends.TestLibraries.jUnitRunner)
androidTestImplementation(Depends.TestLibraries.espressoCore)
}
It doens't look for me that I'm missing something.
Does anyone have an id on how to fix this?
The following works:
testOptions {
unitTests.apply {
isReturnDefaultValues = true
}
}
Note that you need to do a gradle sync to pick up the configuration
I finally managed to find a solution using:
apply(from = "../testOptions.gradle")
which contains:
android {
testOptions {
unitTests.returnDefaultValues = true
}
}

IntelliJ Kotlin multiplatform project Gradle sync very long

I've created a new Kotlin Multiplatform project for Andrid and iOS (Mobile shared Library). The project works fine but every time I run Gradle sync, it takes more than 5 minutes every time. It always get stuck on the same line:
Gradle: Build model 'org.jetbrains.kotlin.gradle.KotlinMPPGradleModel' for root project 'MyProject'
Why is it taking so long evey time?
I'm using Gradle version 5.1.
Here is my build.gradle file:
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.8.1"
}
}
plugins {
id 'kotlin-multiplatform' version '1.3.11'
}
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
repositories {
mavenCentral()
}
group 'com.example'
version '0.0.1'
apply plugin: "com.android.library"
apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName version
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions.incremental = false
}
kotlin {
targets {
fromPreset(presets.android, 'android')
// This preset is for iPhone emulator
// Switch here to presets.iosArm64 to build library for iPhone device
fromPreset(presets.iosX64, 'ios') {
compilations.main.outputKinds('FRAMEWORK')
}
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
androidMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
}
}
androidTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
}
iosMain {
}
iosTest {
}
}
}
configurations {
compileClasspath
}
Screenshot of the problem:
There's a known issue where Kotlin/Native dependencies are re-fetched every sync, which may be what you're seeing. You can see details and follow it here.
As posted to that issue, there's a workaround you may be able to try which essentially involves adding { content { excludeGroup("Kotlin/Native" } } to every item in your repositories block.
As #Brucelet pointed out this is a known issue. To supplement his answer, here is a full implementation of the workaround in Groovy:
repositories {
mavenCentral().content() {
excludeGroup "Kotlin/Native"
}
google().content() {
excludeGroup "Kotlin/Native"
}
jcenter() {
content {
excludeGroup("Kotlin/Native")
}
}
maven {
url 'https://jitpack.io'
content {
excludeGroup("Kotlin/Native")
}
}
}
and in Kotlin DSL:
repositories {
mavenLocal().apply {
content {
excludeGroup("Kotlin/Native")
}
}
maven {
url = uri("https://dl.bintray.com/soywiz/soywiz")
content {
includeGroup("com.soywiz")
excludeGroup("Kotlin/Native")
}
}
jcenter() {
content {
excludeGroup("Kotlin/Native")
}
}
google().apply {
content {
excludeGroup("Kotlin/Native")
}
}
}
Try to run the Gradle task from command lines with --parallel option.
Otherwise, fllow this guide to profile Gradle execution https://guides.gradle.org/performance/

Kotlin Multiplatform Library Project Upload Issue - POM Multiple Artifacts

We have a Kotlin multi-platform library project with a build script which builds for various platforms, i.e. common, JS & JVM, using the "old-style" sub-project structure and processes. Under this system, the artifacts produced can be successfully uploaded to our Sonatype Nexus repo (the root build.gradle is included below (1)).
We are now trying to convert to the "new-style" Kotlin multi-platform approach/structure (Building Multiplatform Projects with Gradle), but we are getting the following error when we try to upload the artifacts to the repo (the modified root build.gradle included below (2)):
BUILD FAILED in 0s
8 actionable tasks: 1 executed, 7 up-to-date
A POM cannot have multiple artifacts with the same type and classifier. Already have MavenArtifact objjson-metadata:jar.asc:asc:null, trying to add MavenArtifact objjson-metadata:jar.asc:asc:null.
10:36:52: Task execution finished 'uploadArchives'.
Does anyone know why we get this error?
The settings for each artifact created during the build seem to need to be updated in the script, but we are not sure how to do that.
Thanks in advance!
(1) Original ("old-style" multi-platform) root build.gradle:
buildscript {
ext.kotlinVersion = '1.2.61'
repositories {
jcenter()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath 'com.bmuschko:gradle-nexus-plugin:2.3.1'
}
}
plugins {
id 'io.codearte.nexus-staging' version '0.11.0'
}
allprojects {
group 'nz.salect.objJSON'
version '0.24-SNAPSHOT'
}
ext {
libraries = [
kotlin_stdlib : "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion",
kotlin_stdlib_common : "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion",
kotlin_stdlib_js : "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion",
kotlin_reflect_lib : "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion",
kotlin_test : "org.jetbrains.kotlin:kotlin-test:$kotlinVersion",
kotlin_test_common : "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion",
kotlin_test_js : "org.jetbrains.kotlin:kotlin-test-js:$kotlinVersion",
kotlin_test_annotations_common: "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion",
kotlin_test_junit : "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion",
]
}
subprojects {
repositories {
jcenter()
mavenCentral()
}
//Nexus/Maven upload settings
apply plugin: 'com.bmuschko.nexus'
modifyPom {
project {
name 'objJSON'
description 'A JSON serialization library fully implemented in Kotlin.'
url 'https://bitbucket.org/objdict/objjson'
inceptionYear '2016'
scm {
url 'https://bitbucket.org/objdict/objjson'
connection 'scm:https://bitbucket.org/objdict/objjson.git'
developerConnection 'scm:git://bitbucket.org/objdict/objjson.git'
}
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
developers {
developer {
id 'xxx'
name 'xxx'
email 'xxx'
}
}
}
}
extraArchive {
sources = true
tests = true
javadoc = true
}
nexus {
sign = true
repositoryUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
snapshotRepositoryUrl = 'https://oss.sonatype.org/content/repositories/snapshots/'
}
//End Nexus/Maven upload settings
}
nexusStaging {
packageGroup = "nz.salect"
}
(2) Updated ("new-style" multi-platform) root build.gradle:
buildscript {
ext.kotlinVersion = "1.3.11"
repositories {
jcenter()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
classpath("com.bmuschko:gradle-nexus-plugin:2.3.1")
}
}
plugins {
// from the multiplatform reference file
id "kotlin-multiplatform" version "1.3.11"
id "maven-publish"
id "io.codearte.nexus-staging" version "0.11.0"
//Nexus/Maven upload settings
//id "bmuschko.nexus"
}
//Nexus/Maven upload settings
apply plugin: "com.bmuschko.nexus"
//ext { - all moved to buildSrc
// libraries = [
// kotlin_stdlib: "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion",
// kotlin_stdlib_common: "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion",
// kotlin_stdlib_js: "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion",
//
// kotlin_reflect_lib: "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion",
//
// kotlin_test: "org.jetbrains.kotlin:kotlin-test:$kotlinVersion",
// kotlin_test_common: "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion",
// kotlin_test_js: "org.jetbrains.kotlin:kotlin-test-js:$kotlinVersion",
// kotlin_test_annotations_common: "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion",
//
// kotlin_test_junit: "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion",
// ]
//}
allprojects {
group "nz.salect.objJSON"
version Share.version
repositories {
jcenter()
mavenCentral()
}
modifyPom {
project {
name "objJSON"
description "A JSON serialization library fully implemented in Kotlin."
url "https://bitbucket.org/objdict/objjson"
inceptionYear "2016"
scm {
url "https://bitbucket.org/objdict/objjson"
connection "scm:https://bitbucket.org/objdict/objjson.git"
developerConnection "scm:git://bitbucket.org/objdict/objjson.git"
}
licenses {
license {
name "The Apache Software License, Version 2.0"
url "http://www.apache.org/licenses/LICENSE-2.0.txt"
distribution "repo"
}
}
developers {
developer {
id "xxx"
name "xxx"
email "xxx"
}
}
}
}
extraArchive {
sources = true
tests = true
javadoc = true
}
nexus {
sign = true
repositoryUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
snapshotRepositoryUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
}
//End Nexus/Maven upload settings
}
nexusStaging {
packageGroup = "nz.salect"
}
repositories {
mavenCentral()
}
kotlin {
targets {
fromPreset(presets.jvm, "jvm") {
mavenPublication {
artifactId = "objjson-jvm"
}
}
fromPreset(presets.js, "js") {
mavenPublication {
artifactId = "objjson-js"
}
}
// For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
// For Linux, preset should be changed to e.g. presets.linuxX64
// For MacOS, preset should be changed to e.g. presets.macosX64
//fromPreset(presets.mingwX64, "mingw")
}
sourceSets {
commonMain {
dependencies {
implementation("$Libraries.kotlin_stdlib_common")
implementation("$Libraries.kotlin_reflect_lib")
}
}
commonTest {
dependencies {
implementation("$Libraries.kotlin_test_common")
implementation("$Libraries.kotlin_test_annotations_common")
}
}
jvmMain {
dependencies {
implementation("$Libraries.kotlin_stdlib")
implementation("$Libraries.kotlin_reflect_lib")
}
}
jvmTest {
dependencies {
implementation("$Libraries.kotlin_test")
implementation("$Libraries.kotlin_test_junit")
}
}
jsMain {
dependencies {
implementation("$Libraries.kotlin_stdlib_js")
}
}
jsTest {
dependencies {
implementation("$Libraries.kotlin_test_js")
}
}
mingwMain {
}
mingwTest {
}
}
}

Resources