Why does the access of result annotated with `andr...
# android
a
Why does the access of result annotated with
android.annotations.Nullable
generate a mere warning instead of an error?
j
I’m new to android and Kotlin, but why isn’t it an optional? Is it to do with how it’s exposed from Java or something like that?
g
Because it is a platform call and the compiler reads it as a type
String!
which can be of type
String
or
String?
a
Yeah, so I’m asking, why is it a platform type
Compare it with this:
image.png
Test is defined as:
Copy code
import androidx.annotation.Nullable;

public class Test {

    @Nullable
    public String getAction() {
        throw new RuntimeException();
    }

}
Why does the compiler understand the
androidx.annotation.Nullable
annotation but not the
android.annotation.Nullable
(the warning is coming from the linter, why can’t the compiler do that?)
j
The android.jar doesn't use
android.annotation.Nullable
. It rewrites the bytecode to use
android.annotation.RecentlyNullable
which only triggers a warning. It will be an error in Q.
👍 17
a
Ah, ok thanks, now I see the announcement https://android-developers.googleblog.com/2018/08/android-pie-sdk-is-now-more-kotlin.html
Normally, nullability contract violations in Kotlin result in compilation errors. But to ensure the newly annotated APIs are compatible with your existing code, we are using an internal mechanism provided by the Kotlin compiler team to mark the APIs as recently annotated. […] The goal is to provide you with sufficient time to update your code.