标签: kotlin-multiplatform

SerializationException:找不到无参数序列化器

我正在创建一个Kotlin Multiplatform库; 实际上我有3个模块(common,jvmjs),

在我获得的类路径中: classpath "org.jetbrains.kotlin:kotlin-serialization:${versions.kotlin}"

在我的模块中,我得到了:

  • 共同: "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:${versions.kotlinSerialization}"
  • JDK: "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${versions.kotlinSerialization}"
  • JS: "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:${versions.kotlinSerialization}"

apply plugin: 'kotlinx-serialization'ofc.

但是当我运行这个简单的测试时:

@Serializable
data class ASimpleClass( val a: Int )
Run Code Online (Sandbox Code Playgroud)

.

@Test  
fun testingMultiplatformCode_canSerialize() {
    JSON.stringify( ASimpleClass( 1 ) )
}
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

kotlinx.serialization.SerializationException: Can't locate argument-less serializer for class studio.forface.ktmdb.ASimpleClass. For generic classes, such as lists, please provide serializer explicitly.

    at kotlinx.serialization.PlatformUtilsKt.serializer(PlatformUtils.kt:28)
    at studio.forface.ktmdb.ProjectTests.testingMultiplatformCode_canSerialize(ProjectTests.kt:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at …
Run Code Online (Sandbox Code Playgroud)

serialization multiplatform kotlin kotlin-multiplatform kotlinx.serialisation

6
推荐指数
2
解决办法
1060
查看次数

多平台项目的ios模块中未解决ktor依赖项

我有一个针对iOS和Android的kotlin-multiplatform项目。Ktor http客户端在通用模块中使用。

一切都适用于Android应用程序。但是当使用iOS lib构建项目时,我收到以下异常:

> Task :app:compileKotlinIos FAILED
src/commonMain/kotlin/com/ottamotta/mozoli/api/MozoliApiKtor.kt:4:8: error: unresolved reference: io
import io.ktor.client.HttpClient
       ^
src/commonMain/kotlin/com/ottamotta/mozoli/api/MozoliApiKtor.kt:5:8: error: unresolved reference: io
import io.ktor.client.features.feature
Run Code Online (Sandbox Code Playgroud)

...以及其他原因,说没有ktor依赖关系已解决。

这是build.gradle:

plugins {
    id 'kotlin-multiplatform' version '1.3.10'
}
repositories {
    google()
    jcenter()
    mavenCentral()
    maven { url "https://kotlin.bintray.com/kotlinx" }
}

ext {
    support_lib_version = '28.0.0'
    ktor_version = '1.0.0'
}

def keystorePropertiesFile = rootProject.file("./app/secret.properties");
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "com.ottamotta.mozoli"
        minSdkVersion …
Run Code Online (Sandbox Code Playgroud)

android ios ktor kotlin-multiplatform

6
推荐指数
1
解决办法
1885
查看次数

适用于iOS的Kotlin Multiplatform库,带有bitcode

我们使用Kotlin在Android和iOS之间共享库.

我们设置了一切,但在iOS上我需要启用Bitcode.经过研究,我找到了解决方案:

kotlin {
targets {
    fromPreset(presets.jvm, 'jvm') {
        mavenPublication {
            artifactId = 'my-lib-name'
        }
    }
    // Switch here to presets.iosArm64 to build library for iPhone device || iosX64 for emulator
    fromPreset(presets.iosArm64, 'iOS') {
        compilations.main.outputKinds('FRAMEWORK')
        compilations.main.extraOpts '-Xembed-bitcode' // for release binaries
        compilations.main.extraOpts '-Xembed-bitcode-marker'//  for debug binaries
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但问题是现在,我有,如果是,我如何在发布和调试二进制文件和特定标志之间分开?我可以简单地添加两个标志没有任何缺点吗?

也许有人可以启发我谢谢

ios ios-frameworks kotlin bitcode kotlin-multiplatform

6
推荐指数
2
解决办法
362
查看次数

有没有办法在 Kotlin Multiplatform 中使用 Firebase(身份验证)?

我有一个在 Android 中使用 Firebase 身份验证的项目。它运行良好,我想使用 Kotlin Multiplatform 的代码共享将同一个项目移植到 iOS 应用程序。

我最初认为我可以简单地创建一个

expect class FirebaseAuth
Run Code Online (Sandbox Code Playgroud)

//AndroidMain
actual class FirebaseAuth

//iOSMain
actual class FirebaseAuth
Run Code Online (Sandbox Code Playgroud)

但我真的不知道如何在 iOSMain 中使用 iOS 版本的 FirebaseAuth?有人可以在这里指导我吗?

kotlin firebase firebase-authentication kotlin-multiplatform

6
推荐指数
2
解决办法
1714
查看次数

Kotlin 多平台注释处理

kapt当一个人有一个 jvm 目标时,可以在 Kotlin Multiplatform 中完成注释处理。

但是,如果没有 jvm 目标,如何处理注释?

具体来说,我想在处理来自commonMain. 但我无法弄清楚如何处理这些。

我的注释处理器此刻只记录:

@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes("ch.hippmann.annotation.Register")
@SupportedOptions(RegisterAnnotationProcessor.KAPT_KOTLIN_GENERATED_OPTION_NAME)
class RegisterAnnotationProcessor : AbstractProcessor(){

    companion object {
        const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
    }

    override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment?): Boolean {
        processingEnv.messager.printMessage(Diagnostic.Kind.WARNING, "Processing")
        return true
    }


}
Run Code Online (Sandbox Code Playgroud)

注释位于一个单独的多平台模块中,位于commonMain

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Register
Run Code Online (Sandbox Code Playgroud)

并在项目中使用commonMain
部分build.gradle

kotlin {
    jvm()
    // For ARM, should be changed to iosArm32 or iosArm64
    // For Linux, should be changed …
Run Code Online (Sandbox Code Playgroud)

annotations annotation-processing kotlin kapt kotlin-multiplatform

6
推荐指数
1
解决办法
616
查看次数

无法从仅限 jvm 的模块访问 commonMain 多平台类

我有一个具有以下结构的项目:

  • multiplatformmodule - 包含一个 kotlin 类 SomeMultiPlatformClass
  • jvmmodule - 一个纯 jvm 模块,它依赖于 multiplatformmodule

问题是在 jvmmodule 中,我无法multiplatformmodule在运行测试时访问任何类。我明白了java.lang.NoClassDefFoundError: com/example/multiplatform/multiplatformmodule/SomeMultiPlatformClass。IDE (Anroid Studio) 也将此类标记为未解析。然而,当multplatformmodule从另一个多平台模块访问类时,它会构建。

在示例项目https://github.com/micHar/kmm-dependency-issues/tree/master 中可以看到更多内容。

unit-testing jvm intellij-idea kotlin kotlin-multiplatform

6
推荐指数
1
解决办法
255
查看次数

找不到 Kotlin Multiplatrom Mobile 库的匹配变体

我有一个发布到 Maven Central的 Kotlin Multiplatform Mobile。我还尝试在非 KMM Android 应用程序中使用此库。当我在 android 应用程序中声明依赖项时,我收到此错误

Execution failed for task ':app:checkDebugAarMetadata'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Could not resolve io.github.tyczj:tweedle-android:0.3.0.
     Required by:
         project :app
      > No matching variant of io.github.tyczj:tweedle-android:0.3.0 was found. The consumer was configured to find a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
          - Variant 'commonMainMetadataElements-published' capability io.github.tyczj:tweedle-android:0.3.0:
              - Incompatible because this component declares …
Run Code Online (Sandbox Code Playgroud)

android maven kotlin kotlin-multiplatform kotlin-multiplatform-mobile

6
推荐指数
1
解决办法
616
查看次数

Swift 中的 KMM 泛型:强制 KMM Any 为 Swift Any 而不是 AnyObject

我目前正在开发一个用 Swift 实现的 KMM 库。所以我对泛型有一些不太理解的地方。

我有我的 KMM 通用类:

class Foo<T> {
 ...
}
Run Code Online (Sandbox Code Playgroud)

然后,我可以在 KMM 中毫无问题地使用它,例如像这样

var bar = Foo<List<SomeClass>>()
Run Code Online (Sandbox Code Playgroud)

原来的问题:

当我在 swift 中使用它时,这有点不同,因为当我将 KMM 库编译为 Swift 框架时,我的类现在以这种方式声明:

public class Foo<T> : KotlinBase where T : AnyObject {
 ...
}
Run Code Online (Sandbox Code Playgroud)

在这里,我无法像在 KMM 中那样将它与数组一起使用,因为 Swift 数组不是 Any,而是 AnyObject。例如:

var bar = Foo<[SomeClass]>()
Run Code Online (Sandbox Code Playgroud)

然后我收到错误:

“Foo”要求“[AnyObject]”是类类型

问题:

是否可以强制执行某些操作,以便 KMM Any 在 Swift 中被解释为 Any 而不是 AnyObject,或者这个问题还有其他解决方案吗?

编辑:

正如亚历山大所说:

可能没有办法解决这个问题。从根本上说,除了少数硬编码到平台中的原语之外,JVM 缺乏一种表达就地分配的值类型(如 Swift 的结构或枚举)的方法。

所以我尝试在 KMM 中重载 Foo,如下所示:

class ListFoo<T>() : Foo<T>() where …
Run Code Online (Sandbox Code Playgroud)

generics ios swift kotlin-multiplatform

6
推荐指数
0
解决办法
962
查看次数

如何添加/删除/编辑 kotlinx.serialization.json.JsonObject 的值

我目前正在努力寻找向现有资产添加财产的最佳方法jsonObject(kotlinx.serialization.json.JsonObject)

甚至我发现 JsonObject 内部有私有的不可变映射

private val content: Map<String, JsonElement>
Run Code Online (Sandbox Code Playgroud)

场景:假设我已经有一个jsonObject包含 50 个属性的对象,并且考虑到任何键上的 jsonObject 都可以具有任意数量的深度。

针对这种情况,我们如何高效地执行以下操作:

  1. 添加原始属性
  2. 更新原始属性
  3. 删除原始属性

仅供参考:即使我发现有一个名为JsonObjectBuilder的类,但它并不需要现有的 JsonElement / JsonObject / JsonArray 事实上,它会新创建另一个 jsonObject。

感谢您抽出时间阅读到这里

https://github.com/Kotlin/kotlinx.serialization/issues/1593

jetbrains-ide kotlin kotlin-multiplatform kotlin-multiplatform-mobile

6
推荐指数
1
解决办法
3381
查看次数

KMM:如何解决 abi 版本不兼容的问题。当前默认值为“1.4.2”,找到“1.5.0”。1.5.20编译器生成的库?

我正在制作一个KMM application,我已经完成了Android部分,没有任何问题,一切都很完美。

然而,当我启动iOS版本时,通过打开iosMyApp.xcworkspace文件并启动编译,会出现此错误。

The following Kotlin source sets were configured but not added to any Kotlin compilation:
 * androidAndroidTestRelease
 * androidTestFixtures
 * androidTestFixturesDebug
 * androidTestFixturesRelease
You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'.
See https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#connecting-source-sets
w: skipping /Users/jeremieguillot/.gradle/caches/modules-2/files-2.1/io.ktor/ktor-client-serialization-iosx64/1.6.3/1912839739c3051183691e878ef7e4d17366f4f4/ktor-client-serialization.klib. **Incompatible abi version. The current default is '1.4.2', found '1.5.0'. The library produced by 1.5.20 compiler**
e: Could not find "/Users/jeremieguillot/.gradle/caches/modules-2/files-2.1/io.ktor/ktor-client-serialization-iosx64/1.6.3/1912839739c3051183691e878ef7e4d17366f4f4/ktor-client-serialization.klib" in [/Users/jeremieguillot/AndroidStudioProjects/SpotABivouac/iosSpotApp/Pods, /Users/jeremieguillot/.konan/klib, /Users/jeremieguillot/.konan/kotlin-native-prebuilt-macos-1.5.10/klib/common, …
Run Code Online (Sandbox Code Playgroud)

android gradle ktor kotlin-multiplatform kotlin-multiplatform-mobile

6
推荐指数
1
解决办法
2405
查看次数