Call Kotlin inline function from Java

Viewed 6533

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 generateStyleNotCorrectException(key, value) }

It works in kotlin and the function is inlined.

But when used in Java code, It just cannot be inlined, and still a normal static method call (seen from the decompiled contents).

Something like this:

public static final void inJava(String key, String value) throws AOPException {
    throw ExceptionsKt.generateStyleNotCorrectException(key, value);
// when decompiled, it has the same contents as before , not the inlined contents.
}
2 Answers

Yes, u can do it

In Kotlin file:

    Builder.sendEvent { event ->
                    YandexMetrica.reportEvent(event)
                }
                .build();

In Java file:

    Builder.sendEvent(new Function1<String, Unit>() {
                    @Override
                    public Unit invoke(String event) {
                        Log.i("TEST", event);
                        return null;
                    }
                })
                .build();
Related