priyank
priyank

Reputation: 2691

Android library written in Kotlin, provide support for Java apps

I am writing an Android library in Kotlin. But when used in Android apps written in Java the support is very poor. For example default parameters in Kotlin are still required in Java. Another one is, non-null parameters does not accept null in Kotlin app (compile time error) but throws run time exception when null is passed in Java. I know about null checks but they make the code look dirty.

What is the proper way to support Android Java apps? Direction, suggestions, examples are welcome. Thanks.

Upvotes: 2

Views: 295

Answers (1)

Mibac
Mibac

Reputation: 9448

When a function has default arguments you can use the @JvmOverloads annotation to create separate methods for Java users. There are also more similar enhancements to Java support in Kotlin. Here is a list what you can do.

You can also use @Nullable and @NotNull annotations on local variables and fields to improve IDE support for nullable values (also more likely the compiler will fail at compile time rather than runtime).

IntelliJ Idea auto generates not null checks for you when you use the @NotNull annotation.

Upvotes: 1

Related Questions