Usage of Kotlin together with other languages, most notably Java. e.g. Calling Kotlin functions from Java, or using Java classes from Kotlin.
Questions tagged [kotlin-interop]
117 questions
15
votes
2 answers
Call Kotlin inline function from Java
Exceptions.kt:
@Suppress("NOTHING_TO_INLINE")
inline fun generateStyleNotCorrectException(key: String, value: String) =
AOPException(key + " = " + value)
In kotlin:
fun inKotlin(key: String, value: String) {
throw…

aristotll
- 8,694
- 6
- 33
- 53
13
votes
1 answer
Implement (/inherit/~extend) annotation in Kotlin
In Java I have the possibility to "implement" annotations.
Sample Java annotation:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JavaClassAnno {
String[] value();
}
Sample Java "implementation":
class…

Roland
- 22,259
- 4
- 57
- 84
13
votes
5 answers
"Accidental override: The following declarations have the same JVM signature" when implementing Java interface
I faced the following error trying to extend RuntimeException and implement GraphQLError interface, defined in Java, from my Kotlin code. This is the error:
Accidental override: The following declarations have the same JVM signature…

Victor Dombrovsky
- 2,955
- 3
- 21
- 33
13
votes
2 answers
Is there an overhead for writing a library in Kotlin for Android?
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…

Marc O'Morain
- 3,699
- 5
- 28
- 34
12
votes
1 answer
Class is not abstract and does not implement abstract base class member
I'm confused by this Kotlin error associated with providing an implementation for an abstract class that has been imported from a maven package.
I have a maven library that is written in Kotlin and exposes an abstract class called…

Ken
- 741
- 2
- 9
- 17
12
votes
4 answers
Data classes in Kotlin
What is the difference between:
Definition 1
data class Person (var name:String, var age:Int)
Definition 2
class Person (var name:String, var age:Int)
Definition 3
class Person (){
var name:String = ""
var age:Int = 1
}
In the 3 cases…

josedlujan
- 5,357
- 2
- 27
- 49
11
votes
3 answers
kotlin: syntax for 2 lambda parameter
I'm new to kotlin. I have a java class with 2 overloaded methods. One accepts one function, the other one accepts two
mapToEntry(Function super T, ? extends V> valueMapper)
and
mapToEntry(Function super T, ? extends K> keyMapper,
…

piotrek
- 13,982
- 13
- 79
- 165
10
votes
1 answer
Why Can't Kotlin Infer The Type For Comparator
Reading the Java interop document about SAM Conversions, I expected the Kotlin function
Collections.sortWith(comparator: kotlin.Comparator /* = java.util.Comparator */)
to be able to take a lambda function without needing to explicitly…

matt freake
- 4,877
- 4
- 27
- 56
10
votes
2 answers
How to apply @IntRange() support annotation to Kotlin property setter
I have been trying to find out how to apply @IntRange(from = 1) to my Kotlin property. After several failed attempts I finally just created the class I wanted in Java and converted it to Kotlin inside Android Studio. Here is my Java class:
import…

Adam
- 2,167
- 5
- 19
- 33
10
votes
2 answers
Changing kotlin extension function receiver JVM name
This is a general question.
Let's say I have an extension function written in kotlin which converts DP to PX and return a NonNull Int
fun Int.toPx() { /** implementation */ }
The function in java will look something like this
public int toPx(int…

Gil Goldzweig
- 1,809
- 1
- 13
- 26
10
votes
2 answers
Kotlin: Can't use GenericTypeIndicator to call Firebase Database's getValue
When using Kotlin to work with Firebase database, I can't seem to retrieve a value of type List if I use a GenericTypeIndicator as follows:
snap.getValue(object : GenericTypeIndicator
- >() {})
It produces an exception from the…

Randy Sugianto 'Yuku'
- 71,383
- 57
- 178
- 228
9
votes
2 answers
Kotlin multiplatform/native interoperability with Objective-C framework
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…

oleg.semen
- 2,901
- 2
- 28
- 56
8
votes
1 answer
Kotlin native interop linker could not find framework
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…

oleg.semen
- 2,901
- 2
- 28
- 56
8
votes
2 answers
Inheritance from Java class with a public method accepting a protected class in Kotlin
I have this situation:
There are a Java class
public class A {
public void overrideMe(B param){
//TODO: override me in Kotlin!
}
protected static class B {
}
}
and a Kotlin class, which inherits from it and has to…

Nikolay Romanov
- 191
- 4
8
votes
3 answers
Kotlin - chaining of safe call operator. unnecessary operator calls
Take the following example which uses safe call operator (?.):
class Sample {
class A(
val sampleB: B? = B()
)
class B(
val sampleC: C = C()
)
class C(
val sampleInt: Int = 1
)
fun…

Nishanth
- 1,801
- 21
- 25