0

I was developing an KMM App, where I try to implement an MVVM patter using Kotlin Multiplatform plugin.

My problem comes, due to I implement kotlinx-serialization-json, library, where is expose and incorrect Json classes, whihc throws the following error:

Json class error:

None of the following functions can be called with the arguments supplied protected constructor Json(configuration: JsonConfiguration, serializersModule: SerializersModule) defined in kotlinx.serialization.json.Json public fun Json(from: Json = ..., builderAction: JsonBuilder.() -> Unit): Json defined in kotlinx.serialization.json 

and JsonConfiguration class error:

Cannot access '<init>': it is internal in 'JsonConfiguration'

When I see from which library it's importing I see that is from the following:

enter image description here

I don't know why I have that library in my proyect, in order have a diferent serialization version which I set in my configuration files:

build.gradle.proyect

buildscript {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.0.2")
        classpath(kotlin("gradle-plugin", version = Versions.kotlin))
        classpath(kotlin("serialization", version = Versions.kotlin))
        classpath("com.squareup.sqldelight:gradle-plugin:${Versions.sqldelight}")
        classpath ("androidx.navigation:navigation-safe-args-gradle-plugin:${Versions.navigation}")
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
}

plugins{
    kotlin("android") version "${Versions.kotlin}" apply false
}

build.gradle.shared

plugins {
    kotlin("multiplatform")
    id("kotlinx-serialization")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("com.squareup.sqldelight")
}
....
 sourceSets {
        all {
            languageSettings.apply {
                useExperimentalAnnotation("kotlinx.coroutines.ExperimentalCoroutinesApi")
            }
        }

        val commonMain by getting{
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation(Coroutines.Core.core)
                implementation(Ktor.Core.common)
                implementation(Ktor.Json.common)
                implementation(Ktor.Logging.common)
                implementation(Ktor.Serialization.common)
                implementation(SqlDelight.runtime)
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
                implementation(Ktor.Mock.common)
            }
        }
        val androidMain by getting{
            dependencies {
                implementation(kotlin("stdlib"))
                implementation(Coroutines.Core.core)
                implementation(Ktor.android)
                implementation(Ktor.Core.jvm)
                implementation(Ktor.Json.jvm)
                implementation(Ktor.Logging.jvm)
                implementation(Ktor.Logging.slf4j)
                implementation(Ktor.Mock.jvm)
                implementation(Ktor.Serialization.jvm)
                implementation(Serialization.core)
                implementation(SqlDelight.android)
            }
        }

        val androidAndroidTestRelease by getting
        val androidTest by getting {
            dependsOn(androidAndroidTestRelease)
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        //val iosX64Main by getting
        //val iosArm64Main by getting
        //val iosSimulatorArm64Main by getting
        val ios by creating {
            dependsOn(commonMain)
            //iosX64Main.dependsOn(this)
            //iosArm64Main.dependsOn(this)
            //iosSimulatorArm64Main.dependsOn(this)
            dependencies {
                implementation(SqlDelight.native)
            }
        }

    }

And the file where I keep my versions:

dependecies.kt

object Versions {
  const val navigation = "2.3.0"
  const val androidBuildTools = "30.0.2"
  const val appCompat = "1.3.1"
  const val compileSdk = 29
  const val constraintLayout = "1.1.3"
  const val coroutines = "1.5.2"
  const val kotlin = "1.5.31"
  const val ktor = "1.6.2"
  const val lifecycle = "2.2.0"
  const val material = "1.4.0"
  const val minSdk = 26
  const val picasso = "2.71828"
  const val glide = "4.11.0"
  const val recyclerView = "1.1.0"
  const val serialization = "1.3.1"
  const val slf4j = "1.7.30"
  const val sqldelight = "1.5.2"
  const val swipeToRefreshLayout = "1.0.0"
  const val targetSdk = 29
  const val timber = "4.7.1"
}

const val material = "com.google.android.material:material:${Versions.material}"
const val picasso = "com.squareup.picasso:picasso:${Versions.picasso}"
const val glide = "com.github.bumptech.glide:glide:${Versions.glide}"
const val timber = "com.jakewharton.timber:timber:${Versions.timber}"
const val databinding = "com.android.databinding:compiler:2.0.0-beta6"
const val firebase = "com.google.firebase:firebase-auth-ktx"
const val firebasePlataform = "com.google.firebase:firebase-bom:28.4.1"
object AndroidX {
  const val appCompat = "androidx.appcompat:appcompat:${Versions.appCompat}"
  const val constraintLayout = "androidx.constraintlayout:constraintlayout:${Versions.constraintLayout}"
  const val lifecycleExtensions = "androidx.lifecycle:lifecycle-extensions:${Versions.lifecycle}"
  const val lifecycleViewModelKtx = "androidx.lifecycle:lifecycle-viewmodel-ktx:${Versions.lifecycle}"
  const val recyclerView = "androidx.recyclerview:recyclerview:${Versions.recyclerView}"
  const val swipeToRefreshLayout = "androidx.swiperefreshlayout:swiperefreshlayout:${Versions.swipeToRefreshLayout}"
  const val navigation = "androidx.navigation:navigation-fragment-ktx:${Versions.navigation}"
  const val navigation_ui = "androidx.navigation:navigation-ui-ktx:${Versions.navigation}"
}

object Coroutines {
  const val android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:${Versions.coroutines}"

  object Core {
    const val core = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}"
    const val common = "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:${Versions.coroutines}"
    const val native = "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:${Versions.coroutines}"
  }
}

object Ktor {
  const val android = "io.ktor:ktor-client-android:${Versions.ktor}"
  const val ios = "io.ktor:ktor-client-ios:${Versions.ktor}"

  object Core {
    const val common = "io.ktor:ktor-client-core:${Versions.ktor}"
    const val jvm = "io.ktor:ktor-client-core-jvm:${Versions.ktor}"
    const val native = "io.ktor:ktor-client-core-native:${Versions.ktor}"
  }

  object Json {
    const val common = "io.ktor:ktor-client-json:${Versions.ktor}"
    const val jvm = "io.ktor:ktor-client-json-jvm:${Versions.ktor}"
    const val native = "io.ktor:ktor-client-json-native:${Versions.ktor}"
  }

  object Logging {
    const val common = "io.ktor:ktor-client-logging:${Versions.ktor}"
    const val jvm = "io.ktor:ktor-client-logging-jvm:${Versions.ktor}"
    const val slf4j = "org.slf4j:slf4j-simple:${Versions.slf4j}"
    const val native = "io.ktor:ktor-client-logging-native:${Versions.ktor}"
  }

  object Mock {
    const val common = "io.ktor:ktor-client-mock:${Versions.ktor}"
    const val jvm = "io.ktor:ktor-client-mock-jvm:${Versions.ktor}"
    const val native = "io.ktor:ktor-client-mock-native:${Versions.ktor}"
  }

  object Serialization {
    const val common = "io.ktor:ktor-client-serialization:${Versions.ktor}"
    const val jvm = "io.ktor:ktor-client-serialization-jvm:${Versions.ktor}"
    const val native = "io.ktor:ktor-client-serialization-native:${Versions.ktor}"
  }
}

object Serialization {
  const val runtime =
      "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.serialization}"
  const val core = "org.jetbrains.kotlinx:kotlinx-serialization-core:${Versions.serialization}"
  const val runtimeNative =
      "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:${Versions.serialization}"
}

object SqlDelight {
  const val runtime = "com.squareup.sqldelight:runtime:${Versions.sqldelight}"
  const val android = "com.squareup.sqldelight:android-driver:${Versions.sqldelight}"
  const val native = "com.squareup.sqldelight:native-driver:${Versions.sqldelight}"
}


I try to remove the cache file which is import the wrong library manually, but doesn't works, as removing the .idea and .build folders and make an Inavalidate cache and restart either works.

So I don't know if there some command or way to remove manually a dependecy, which it's use later, and start to use.

Thanks in advance for the help!

[UPTADE]

I try to exclude the specific dependecy, from the only place which can be added, and doesn't work either, beacuse it still added

enter image description here

[EDIT]

I try adding the following restriction in my build.gradle(:shared), but doesn't works either.

 configurations.all {
        resolutionStrategy.eachDependency {
            if ((this.requested.group == "kotlinx.serialization.json") && (!this.requested.name.contains(
                    "Json"
                ))
            ) {
                this.useVersion("1.3.1")
            }
        }
    }

Am I doing right? becasuse I don't guess so

Manuel Lucas
  • 636
  • 6
  • 17
  • you can run `/gradlew dependencies` to see which of dependencies is fetching this one, and update it. Also you can use resolution strategy as shown [here](https://stackoverflow.com/a/54395634/3585796) to force using a strict version for the needed dependency. – Phil Dukhov Feb 28 '22 at 12:26
  • I try adding to my build.gradle(:shared) the following to force don't use that version: configurations.all{ resolutionStrategy{ eachDependency { when(requested.module.toString()){ "kotlinx-serialization:json" -> useVersion("1.3.1") } } } } – Manuel Lucas Mar 03 '22 at 16:32
  • So? Did it worked out? – Phil Dukhov Mar 05 '22 at 15:52
  • Hi ! I still trying due to it's doesn't works – Manuel Lucas Mar 10 '22 at 11:25
  • 1
    You comparing a wrong group and name. Group should be `org.jetbrains.kotlin`. `"Json"` shouldn't be uppercased. These values are parts of the implementations name, so generally you have it like `implementation("group:name:version")`. You can add logs to see all requested items to understand it better. – Phil Dukhov Mar 10 '22 at 12:21

0 Answers0