There seems to be no easy way to suppress annotation processing with NULL by the Kotlin compiler.
As a workaround, you can create an intermediate derived class with @Nullable annotation in Java: when the Kotlin compiler sees both @Nullable and @NonNull on the same code element, it behaves as if annotations with null values ββare missing. Then just subclass it in Kotlin. Example:
Consider a class with the @NonNull parameter in Java:
abstract class Base { public abstract void f(@NonNull String str);
Kotlin understands the annotation: f(str: String) , the type is not null.
Now we extend Base in Java, redefine the method and add the @Nullable annotation to the parameter in Intermediate :
abstract class Intermediate extends Base { @Override public abstract void f(@Nullable String str); }
For Intermediate , Kotlin sees f(str: String!) , The parameter is of the platform type , i.e. its uncertainty is unknown.
After that, you can declare the NULL parameter in a subclass of Kotlin Intermediate :
class Derived(): Intermediate() { override fun f(url: String?) { ... } }
source share