Questions tagged [kotlin-interop]

Usage of Kotlin together with other languages, most notably Java. e.g. Calling Kotlin functions from Java, or using Java classes from Kotlin.

117 questions
8
votes
4 answers

Using functional interfaces with function types in Kotlin

When calling Java code from Kotlin, there is SAM conversion so that Java code like this: adapter.setOnClickListener(new OnClickListener() { @Override public void onClick(View view, int position) { // Do stuff here } }); Can look…
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
8
votes
1 answer

Converting Thread/Runnable implementation from Java to Kotlin

I have an existing Java class ThreadUtils with a method every that looks like: public class ThreadUtil { public static Thread every(int seconds, Runnable r) { Thread t = new Thread(() -> { while(true) { …
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
8
votes
2 answers

Can't have a return inside a Runnable SAM in Kotlin?

I am having the following error with this piece of code, which makes no sense to me: fun spawnWorker(): Runnable { return Runnable { LOG.info("I am a potato!") return } } My IDE says this to me: But the Runnable interface…
PedroD
  • 5,670
  • 12
  • 46
  • 84
7
votes
4 answers

Calling Kotlin from Java -- error: package demo does not exist

I don't understand the documentation: Package-Level Functions All the functions and properties declared in a file example.kt inside a package org.foo.bar, including extension functions, are compiled into static methods of a Java class named…
Thufir
  • 8,216
  • 28
  • 125
  • 273
7
votes
1 answer

Kotlin, targeting Java interop: Idiomatic type for lazy collection?

When targeting Java interop, what type should one use for a lazy collection? Sequence makes the most sense for Kotlin callers due to extension functions on it being lazy by default, but forces Java callers to deal with a Kotlin stdlib type and…
F. George
  • 4,970
  • 4
  • 20
  • 38
7
votes
3 answers

Null safety in legacy Java libraries used in Kotlin projects

Let's say I have particular code in old/legacy Java library: public class JavaClass { private String notNullString; private String nullableString; private String unannotatedString; public JavaClass(@NotNull String notNullString, …
Jan Slominski
  • 2,968
  • 4
  • 35
  • 61
7
votes
2 answers

Kotlin use Java callback interface

I have a WebView. I want to call public void evaluateJavascript(String script, ValueCallback resultCallback) this method. Here is the ValueCallback interface: public interface ValueCallback { /** * Invoked when the value is…
vihkat
  • 895
  • 3
  • 13
  • 28
7
votes
2 answers

Passing a listener object as a function parameter in kotlin

I'm trying to pass a listener from an action to a class (an adapter). In java (code from the Action): private void setListeners() { adapterRecyclerView.setListener( new View.OnClickListener() { @Override …
Kunta Kinte
  • 351
  • 1
  • 2
  • 15
7
votes
6 answers

Cant 'observeOn' main thread with RxKotlin

I'm trying to observe observable on main thread by using: // Kotlin Code Observable .observeOn(AndroidSchedulers.mainThread()) but I'm getting following error: Type Mismatch: Required: rx.Scheduler! Found:…
Nathan Horrigan
  • 763
  • 1
  • 9
  • 20
6
votes
1 answer

Kotlin function parameter with receiver, called from Groovy

Kotlin and Groovy both provide a way to write a high-order function where the function parameter has an implicit receiver. Kotlin Version class KotlinReceiver { fun hello() { println("Hello from Kotlin") } } class KotlinVersion…
Dan
  • 4,312
  • 16
  • 28
6
votes
1 answer

Usage of CompletableFuture's exceptionally method in Kotlin

I'm trying to handle CompletableFuture exceptions in Kotlin, but I'm not able to figure out how to supply the appropriate parameters. So, for example, I have: CompletableFuture.runAsync { "sr" } .exceptionally{e -> {}} but then the compiler…
Johnny
  • 7,073
  • 9
  • 46
  • 72
5
votes
2 answers

Why `Intrinsics.checkParameterIsNotNull` is not inlined?

In Kotlin, if we decompile some Kotlin bytecode to Java, we can often see this statement which does null checks: Intrinsics.checkParameterIsNotNull(foo, "foo") If we explore furthur, we can see the decompiled implementation of this method, it's…
ice1000
  • 6,406
  • 4
  • 39
  • 85
5
votes
1 answer

How to use SQLite in Kotlin/Native?

For a multi-platform project, I consider Kotlin/Native, with the hope to have a common code base at least for business logic and saving of data. For saving of data I would like to use a database like SQLite. However, according to another…
CryptUser
  • 608
  • 1
  • 8
  • 17
5
votes
3 answers

Kotlin files not able to use R.java

I created a new layout file in the appropriate resource folder. After syncing, when I try to reference the layout file, i.e. R.layout.activity_test, R is an "unresolved symbol". When I try to build in AS it fails. The interesting thing is that if I…
5
votes
1 answer

Kotlin Unresolved reference: println from gradle on the CLI

Putting a println statement before a kotlin function returns crashes. stacktrace: thufir@dur:~/NetBeansProjects/kotlin$ thufir@dur:~/NetBeansProjects/kotlin$ gradle clean build --stacktrace w: Classpath entry points to a non-existent location: e:…
Thufir
  • 8,216
  • 28
  • 125
  • 273