gpt4 book ai didi

gradle 同步成功时,Kotlin 多平台无法解析引用

转载 作者:行者123 更新时间:2023-12-03 05:04:19 24 4
gpt4 key购买 nike

当我编译我的 kotlin 多平台项目时,我得到 Unresolved reference关于我在公共(public)模块中使用的东西。但是 gradle 同步工作正常。你可以找到我的项目here

详细说明:

  • 我按照 Jetbrain to build a mobile multiplatform project 的教程进行操作.一切正常。
  • 然后我在公共(public)模块中实现了一个 ktor 客户端来请求一个 api(jsonplaceholder)。我添加了所有必需的依赖项,一切似乎都很好。所有引用都已解决,我没有错误。
  • 但是当我用 make Project 编译我的项目时那么所有添加到获取 ktor 等的依赖项都无法解析。

  • 我想我的项目配置有问题,当我运行 ./gradlew androidDependenciesSharedCode 好像有问题因为它有时在命令的输出中被标记为 \--- :SharedCode .
    build.gradle该项目是:
    buildscript {
    ext.kotlin_version = '1.3.71'
    repositories {
    google()
    jcenter()

    }
    dependencies {
    classpath 'com.android.tools.build:gradle:4.0.0-rc01'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
    }

    allprojects {
    repositories {
    google()
    jcenter()

    }
    }

    task clean(type: Delete) {
    delete rootProject.buildDir
    }
    build.gradle.kts常见的 SharedCode模块是:

    import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

    plugins {
    kotlin("multiplatform")
    kotlin("plugin.serialization")
    }

    kotlin {
    //select iOS target platform depending on the Xcode environment variables
    val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
    if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
    ::iosArm64
    else
    ::iosX64

    iOSTarget("ios") {
    binaries {
    framework {
    baseName = "SharedCode"
    }
    }
    }

    jvm("android")

    val serializationVersion = "0.20.0"
    val ktorVersion = "1.3.2"

    sourceSets["commonMain"].dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion")
    implementation("io.ktor:ktor-client-core:$ktorVersion")
    implementation("io.ktor:ktor-client-json:$ktorVersion")
    implementation("io.ktor:ktor-client-serialization:$ktorVersion")//Kotlinxserializer
    }

    sourceSets["androidMain"].dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
    }
    }

    val packForXcode by tasks.creating(Sync::class) {
    val targetDir = File(buildDir, "xcode-frameworks")

    /// selecting the right configuration for the iOS
    /// framework depending on the environment
    /// variables set by Xcode build
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets
    .getByName<KotlinNativeTarget>("ios")
    .binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)

    from({ framework.outputDirectory })
    into(targetDir)

    /// generate a helpful ./gradlew wrapper with embedded Java path
    doLast {
    val gradlew = File(targetDir, "gradlew")
    gradlew.writeText("#!/bin/bash\n"
    + "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
    + "cd '${rootProject.rootDir}'\n"
    + "./gradlew \$@\n")
    gradlew.setExecutable(true)
    }
    }

    tasks.getByName("build").dependsOn(packForXcode)

    build.gradle对于 android 应用程序是:
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'

    android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
    applicationId "com.mpp.mpptest"
    minSdkVersion 16
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    }

    }

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation project(':SharedCode')
    }

    你知道我做错了什么吗?

    我读了 Building Multiplatform Projects with Gradle但它没有帮助。

    我发现的唯一工作项目来自 KaMPKit .我设置了我的 build.gradle(.kts)文件与 KaMPKit 中的文件一样(参见分支 Set_build_gradle_as_KaMPKit),但“ Unresolved 引用”问题仍然存在。

    最佳答案

    我解决了我的问题。错误是因为我没有声明插件android.lybrary .

    即使没有必要解决问题,我也决定切换我的build.gradle文件到 kotlin DLS,我重写了所有 build.gradle我对共享模块进行了以下更改:

  • 我创建了一个 AndroidManifest.xml
  • 我添加了 com.android.library插件
  • 我添加了一个 block android

  • 最后是 build.gradle.kts共享模块如下所示:

    import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

    plugins {
    kotlin("multiplatform")
    kotlin("plugin.serialization")
    id("kotlinx-serialization")
    id("com.android.library")
    }

    android {
    compileSdkVersion(29)
    defaultConfig {
    minSdkVersion(Versions.min_sdk)
    targetSdkVersion(Versions.target_sdk)
    versionCode = 1
    versionName = "1.0"
    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    }

    kotlin {
    //select iOS target platform depending on the Xcode environment variables
    val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
    if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
    ::iosArm64
    else
    ::iosX64

    iOSTarget("ios") {
    binaries {
    framework {
    baseName = "SharedCode"
    }
    }
    }

    jvm("android")

    sourceSets["commonMain"].dependencies {
    implementation(kotlin("stdlib-common", Versions.kotlin))
    implementation(Deps.Ktor.commonCore)
    implementation(Deps.Ktor.commonJson)
    implementation(Deps.Coroutines.common)
    implementation(Deps.Ktor.commonSerialization)
    }

    sourceSets["androidMain"].dependencies {
    implementation(kotlin("stdlib", Versions.kotlin))
    implementation(Deps.Coroutines.jdk)
    implementation(Deps.Ktor.androidSerialization)
    }

    sourceSets["iosMain"].dependencies {
    implementation(Deps.Ktor.iosSerialization)
    }
    }

    val packForXcode by tasks.creating(Sync::class) {
    val targetDir = File(buildDir, "xcode-frameworks")

    /// selecting the right configuration for the iOS
    /// framework depending on the environment
    /// variables set by Xcode build
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets
    .getByName<KotlinNativeTarget>("ios")
    .binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)

    from({ framework.outputDirectory })
    into(targetDir)

    /// generate a helpful ./gradlew wrapper with embedded Java path
    doLast {
    val gradlew = File(targetDir, "gradlew")
    gradlew.writeText("#!/bin/bash\n"
    + "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
    + "cd '${rootProject.rootDir}'\n"
    + "./gradlew \$@\n")
    gradlew.setExecutable(true)
    }
    }

    tasks.getByName("build").dependsOn(packForXcode)

    所有依赖版本号都来自 Dependencies.kt文件(见 KaMPKit)

    现在所有依赖项都已解决。

    关于gradle 同步成功时,Kotlin 多平台无法解析引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62068133/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com