【问题标题】:Error to compile ios project with kotlin multiplatform使用 kotlin 多平台编译 ios 项目时出错
【发布时间】:2021-02-11 03:08:09
【问题描述】:

我已经在一个现有的 ios 项目中实现了 kotlin 多平台,但我遇到了这些问题

当我为模拟器编译应用程序时,构建阶段脚本中出现错误。

Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain
Command PhaseScriptExecution failed with a nonzero exit code

当我删除为模拟器编译的脚本时 ????。我可以运行模拟器

另一个错误是当我想归档项目时。说它是为 iOS 模拟器构建的????

我的 build.gradle.kts

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

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    kotlin("plugin.serialization")
    id("com.android.library")
    id("kotlin-android-extensions")
    id("com.squareup.sqldelight")
}

repositories {
    gradlePluginPortal()
    google()
    jcenter()
    mavenCentral()
    maven {
        url = uri("https://dl.bintray.com/kotlin/kotlin-eap")
    }
}

dependencies {
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.2.0")
}

configurations {
    create("compileClasspath")
}

android {
    compileSdkVersion(29)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
}

val libName = "shared"

kotlin {
    android()
    ios {
        binaries.framework(libName)
    }

    val coroutinesVersion = "1.4.1-native-mt"
    val serializationVersion = "1.0.0-RC"
    val ktorVersion = "1.4.0"
    val sqlDelightVersion = "1.4.3"
    val reactive_version = "1.1.18"

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib-common")

                // Coroutines
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.2")

                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")

                //  KTOR
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-json:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization:$ktorVersion")

                // SQLDELIGHT
                implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")

                // Reactive
                implementation("com.badoo.reaktive:reaktive:$reactive_version")
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib")

                implementation("androidx.core:core-ktx:1.3.2")
                implementation("io.ktor:ktor-client-android:$ktorVersion")
                implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")

                implementation("androidx.lifecycle:lifecycle-extensions:2.2.0")
            }
        }
        val iosMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.2")

                // HTTP
                implementation("io.ktor:ktor-client-ios:$ktorVersion")
                implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
            }
        }

        all {
            languageSettings.apply {
                progressiveMode = true
                useExperimentalAnnotation("kotlin.RequiresOptIn")
                useExperimentalAnnotation("kotlinx.coroutines.ExperimentalCoroutinesApi")
            }
        }
    }
}

val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)

sqldelight {
    database("SmileduDataBase") {
        packageName = "com.example.smiledu"
        schemaOutputDirectory = file("src/commonMain/db/databases")
    }
}

【问题讨论】:

    标签: xcode kotlin build.gradle kotlin-multiplatform kotlin-multiplatform-mobile


    【解决方案1】:
    Another error is when I want to archive the project. Says it was built for iOS simulator ?
    

    嗨。我遇到过这个问题几次,我唯一得到的解决方法是我们需要在为 iOS 创建存档之前在 android studio 终端中运行这个./gradlew clean,然后尝试创建存档,它应该可以工作。您可以在此处找到更多详细信息:- https://youtrack.jetbrains.com/issue/KT-40907

    When I compile the application for the simulator an error occurs in the build phases script.
    

    为此,您的包装器似乎已损坏,可能对此不太确定。 android 应用程序运行完美吗?你也可以在这里看一次:- How to resolve java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain error?

    【讨论】:

    • 嗨。有效。谢谢! android 应用程序确实可以工作,现在 ios 应用程序也可以。我不得不运行 $ gradle wrapper
    • 但是当我提交我的应用程序时,会出现此错误“无效的捆绑结构 - 二进制文件 'Smiledu_app.app/Frameworks/shared.framework/shared' 是不允许的。” ☹️
    • 如果你能分享一些关于上述问题的更多细节,我至少到现在还没有遇到过这个问题。
    猜你喜欢
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2021-04-28
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多