kotlin-interop

Is there an overhead for writing a library in Kotlin for Android?

[亡魂溺海] 提交于 2019-12-09 16:55:28
问题 I'm considering porting a Java (Android) library to Kotlin. I really like Kotlin and the benefits over Java should reduce the number of bugs in the library. Since the library is targeting a resource constrained environment I'm worried that by porting the library to Kotlin there will be extra overhead. Does a Kotlin library (distributed as .class files) introduce any runtime or extra overhead versus a Java library? Will the resulting Android app be any larger or slower because of Kotlin? 回答1:

Kotlin multiplatform/native interoperability with Objective-C framework

百般思念 提交于 2019-12-09 05:59:58
问题 I'm trying to call Swift/Objective-C code from Kotlin in a multiplatform project. There are no problems with calls to platform code. But when I'm trying to call some library (or framework, not sure how it is properly called as I'm not an iOS dev) it fails. Docs states that it is possible to call Objective-C code and Swift if it is properly exported: Kotlin/Native provides bidirectional interoperability with Objective-C. Objective-C frameworks and libraries can be used in Kotlin code if

Kotlin native interop linker could not find framework

我怕爱的太早我们不能终老 提交于 2019-12-05 09:41:27
I'm trying to use cocoapods framework in Kotlin Multiplatform project. So I added framework to Pods file. ran pod install. created .def file added cinterop config in build.gradle ./gradlew cinteropFirebaseIos runs successfully. It generates .klib so I can see classes in kotlin code. But when I'm trying to run iOS app build fails with message: Showing Recent Messages > Task :app:linkDebugFrameworkIos ld: framework not found FirebaseDatabase /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors Here is my config in build.gradle

Calling kotlin functions which are keywords in java from java?

拈花ヽ惹草 提交于 2019-12-04 23:58:54
Since new is not a keyword in kotlin, i can have the following function in kotlin. fun new(): String { return "just returns some string" } But i am unable to call this function from java since new is a keyword in java. I would like to know if there is some alias for this function in java realm. I did not find any intellij suggestions that might be a possible alias to this function. Edit 1 : I have written the following code in kotlin: fun new(): String { return "just returns some string" } fun main(args:Array<String>){ new() } And I had a look at the java bytecode. It was as follows. // ======

Is there an overhead for writing a library in Kotlin for Android?

蓝咒 提交于 2019-12-04 04:25:15
I'm considering porting a Java (Android) library to Kotlin. I really like Kotlin and the benefits over Java should reduce the number of bugs in the library. Since the library is targeting a resource constrained environment I'm worried that by porting the library to Kotlin there will be extra overhead. Does a Kotlin library (distributed as .class files) introduce any runtime or extra overhead versus a Java library? Will the resulting Android app be any larger or slower because of Kotlin? Kotlin has many optimizations specifically that help Android. If you read through blog posts you can see how

Kotlin: how to pass array to Java annotation

你。 提交于 2019-12-03 22:46:54
I want to use @OneOf annotation from package io.dropwizard.validation; Java usage: @OneOf(value = {"m", "f"}) Kotlin usage: ??? I've tried this: @OneOf(value = arrayOf("m", "f")) and this: @OneOf(value = ["m", "f"]) All i get is : Type inference failed. Expected type mismatch: required: String found: Array<String> Kotlin version: 1.1.2-2 The value parameter is automatically converted to a vararg parameter in Kotlin, as described in http://kotlinlang.org/docs/reference/annotations.html#java-annotations . The correct syntax for this particular case is @OneOf("m", "f") In Kotlin 1.2, it supports

Kotlin dagger 2 Android ViewModel injection error

拥有回忆 提交于 2019-12-03 17:09:30
I'm trying to use dagger 2 on my Android application to inject the new ViewModel from arch android library. From what I see on this sample https://github.com/googlesamples/android-architecture-components/tree/e33782ba54ebe87f7e21e03542230695bc893818/GithubBrowserSample I need to use this: @MustBeDocumented @Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) @Retention(AnnotationRetention.RUNTIME) @MapKey internal annotation class ViewModelKey(val value: KClass<out ViewModel>) @Module abstract class ViewModelModule

Kotlin multiplatform/native interoperability with Objective-C framework

天大地大妈咪最大 提交于 2019-12-03 13:13:23
I'm trying to call Swift/Objective-C code from Kotlin in a multiplatform project. There are no problems with calls to platform code. But when I'm trying to call some library (or framework, not sure how it is properly called as I'm not an iOS dev) it fails. Docs states that it is possible to call Objective-C code and Swift if it is properly exported: Kotlin/Native provides bidirectional interoperability with Objective-C. Objective-C frameworks and libraries can be used in Kotlin code if properly imported to the build (system frameworks are imported by default). See e.g. "Using cinterop" in

Assignment not allowed in while expression?

匆匆过客 提交于 2019-11-30 04:50:53
In Java we can usually perform an assignment within the while condition. However Kotlin complains about it. So the following code does not compile: val br = BufferedReader(InputStreamReader( conn.inputStream)) var output: String println("Output from Server .... \n") while ((output = br.readLine()) != null) { // <--- error here: Assignments are not expressions, and only expressions are allowed in this context println(output) } According to this other thread , this seems the best solution: val reader = BufferedReader(reader) var line: String? = null; while ({ line = reader.readLine(); line }() !

Assignment not allowed in while expression?

百般思念 提交于 2019-11-29 02:18:29
问题 In Java we can usually perform an assignment within the while condition. However Kotlin complains about it. So the following code does not compile: val br = BufferedReader(InputStreamReader( conn.inputStream)) var output: String println("Output from Server .... \n") while ((output = br.readLine()) != null) { // <--- error here: Assignments are not expressions, and only expressions are allowed in this context println(output) } According to this other thread, this seems the best solution: val