0

我正在开发一个 KMM 应用程序,我尝试使用 Kotlin Multiplatform 插件实现 MVVM 模式。

我的问题来了,由于我实现了 kotlinx-serialization-json, library,其中暴露和不正确的 Json 类,whihc 抛出以下错误:

Json 类错误:

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 

和 JsonConfiguration 类错误:

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

当我看到它从哪个库导入时,我看到它来自以下内容:

在此处输入图像描述

我不知道为什么我的项目中有那个库,以便在我的配置文件中设置一个不同的序列化版本:

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)
            }
        }

    }

还有我保存我的版本的文件:

依赖项.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}"
}


我尝试删除手动导入错误库的缓存文件,但不起作用,因为删除 .idea 和 .build 文件夹并制作 Inavalidate 缓存并重新启动都可以。

所以我不知道是否有一些命令或方法可以手动删除依赖项,稍后会使用它并开始使用。

在此先感谢您的帮助!

[更新]

我尝试从唯一可以添加的地方排除特定的依赖,也不起作用,因为它仍然添加

在此处输入图像描述

[编辑]

我尝试在我的 build.gradle(:shared) 中添加以下限制,但也不起作用。

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

我做得对吗?因为我不这么认为

4

0 回答 0