正如标题所示,runBlocking我刚刚在 build.gradle 中添加的协程库中缺少协程构建器。有趣的是,所有其他东西似乎都可用,GlobalScope全部CoroutineScope.launch CoroutineScope.async存在。runBlocking不是。我究竟做错了什么?
这是我的build.gradle
buildscript {
ext {
ktor_version = "1.1.1"
kotlin_version = "1.3.20-eap-52"
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.44"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}
plugins {
id 'kotlin-multiplatform' version '1.3.20-eap-100'
}
repositories {
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
maven { url 'https://dl.bintray.com/kotlin/kotlin-js-wrappers' }
maven { url 'https://dl.bintray.com/kotlinx/kotlinx' }
maven { url "https://kotlin.bintray.com/kotlinx" }
jcenter()
mavenCentral()
}
group 'books'
version '0.0.0'
apply plugin: 'maven-publish'
apply plugin: "org.jetbrains.kotlin.frontend"
kotlin {
jvm() {
compilations.all …Run Code Online (Sandbox Code Playgroud) gradle kotlin build.gradle kotlinx.coroutines kotlin-multiplatform
我是 kotlin 新手,想要构建一个多平台应用程序。对于公共部分,我想使用包含平台特定功能的数据类。
是否可以在特定于平台的声明中使用 kotlin 数据类?
就像是
expect data class Foo(val bar: String)
Run Code Online (Sandbox Code Playgroud)
此致
我试图在 Kotlin/MPP(多平台)项目和 JVM 目标功能中使用ktor 客户端,基本身份验证似乎没有效果。
import io.ktor.client.HttpClient
import io.ktor.client.features.ResponseException
import io.ktor.client.features.auth.Auth
import io.ktor.client.features.auth.providers.basic
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.json.serializer.KotlinxSerializer
import io.ktor.client.features.logging.DEFAULT
import io.ktor.client.features.logging.LogLevel
import io.ktor.client.features.logging.Logger
import io.ktor.client.features.logging.Logging
import io.ktor.client.request.get
import io.ktor.client.request.header
import kotlinx.coroutines.runBlocking
import java.util.*
fun main() = runBlocking {
val client = HttpClient {
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.HEADERS
}
install(JsonFeature) {
serializer = KotlinxSerializer()
}
install(Auth) {
basic {
username = "user"
password = "pass"
}
}
}
val url = "https://en.wikipedia.org/wiki/Main_Page" …Run Code Online (Sandbox Code Playgroud) 我使用 Kotlin Multiplatform 为 iOS 和 Android 创建了一个共享库,并且一切正常,直到我没有将 Xcode 更新到 12.0
当我将 Xcode 更新到 12.0 时,该框架停止在真实设备(iphone)上工作,但在模拟器上工作
我的摇篮
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
id("kotlin-android-extensions")
}
group = "com.example.multiplatform_android_ios"
version = "1.0-SNAPSHOT"
repositories {
gradlePluginPortal()
google()
jcenter()
mavenCentral()
}
kotlin {
android()
ios {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.0")
}
} …Run Code Online (Sandbox Code Playgroud) 我花了一整天的时间试图解决这个问题,尝试从 github 打开几个项目,尝试了 Android studio 4.0、4.2 canary、IntellyJ Idea,但仍然没有在模块中看到 androidMain
我应该尝试什么?

我想在将我的业务逻辑代码从 JAVA 迁移到 Kotlin/Multiplatform 之前实现一些用例。其中之一是使用 C++ 代码。是否可以选择在 commonMain 级别的 Kotlin/Multiplatform 项目中使用 C++ 代码?或者我需要为每个平台编写一个包装层(如 JNI)?我可以得到一些示例代码吗?
非常感谢!
我需要为 iOS 和 Android 构建一个应用程序。
重新启动 Android Studio 后出现此错误。
Kotlin Multiplatform Mobile 插件问题
Kotlin Multiplatform Mobile 插件应该只适用于 macOS
我正在使用 Windows 机器。
请告诉我,如果有任何解决此问题的方法。
我是 KMM 新手,我正在尝试将 KMM 项目作为 pod 集成到我的示例 Xcode 项目中。我可以将 KMM Pod 链接到我的 Xcode 项目。但是,如果我尝试调用其中一个函数,则会产生以下错误。如果我不调用任何 KMM 函数,它可以在我的模拟器中运行该应用程序。
ViewController
import UIKit
import Multiplatform
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let a = Greeting()
a.greeting()
}
}
Run Code Online (Sandbox Code Playgroud)
ld: warning: ignoring file /Users/avjiang/Developments/Multiplatform/SharedCode/build/cocoapods/framework/Multiplatform.framework/Multiplatform, building for iOS Simulator-arm64 but attempting to link with file built for iOS Simulator-x86_64
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_MultiplatformGreeting", referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found …Run Code Online (Sandbox Code Playgroud) 我已经在 Android Studio 中构建了一个 KMM 项目,但 ios 应用程序似乎看不到共享模块中的类。最初它工作得很好,但现在我修改了共享模块中的一些类,出现了错误。
正如您所看到的,Platform 类中存在一些编译错误,但 Android 应用程序工作正常。该错误表示““多平台项目”功能是实验性的,应明确启用”。
iOS 应用程序如下所示:
稍后编辑:
显然,当我在公共源集中的shared.build.gradle中添加此依赖项时,我发现遇到了此错误:
val commonMain by getting {
dependencies {
// implementation("io.ktor:ktor-client-core:$ktorVersion")
// implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
// implementation("io.ktor:ktor-client-json:$ktorVersion")
// implementation("io.ktor:ktor-client-serialization-jvm:$ktorVersion")
}
}
Run Code Online (Sandbox Code Playgroud)
我需要这些依赖项来向我的 ktor 服务器发出请求。
更新:所以问题是我在通用集中添加了一些库,而这些库没有多平台支持。
我想将 Retrofit 添加到 :shared 模块 (commonMain) 中。
sourceSets["commonMain"].dependencies {
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
}
Run Code Online (Sandbox Code Playgroud)
但在commonMain我看不到 Retrofit 类。在androidMain我可以看到它。那是一种魔法。我没有将 Retrofit 添加到androidMain.
请帮我。提前致谢。
kotlin ×5
android ×3
ios ×3
kmm ×2
build.gradle ×1
gradle ×1
kotlin-multiplatform-mobile ×1
ktor ×1
ktor-client ×1
windows ×1