How to get static int variables from Java?

For example, when I try to get Toast.LENGTH_LONG variable, using:

using (AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast")) {
    int length = toastClass.GetStatic<int>("LENGTH_LONG");
}

I get an error: InvalidCastException.
Funnily, there is no InvalidCastException when using uint instead of int, but then I get a zero, instead of correct LENGTH_LONG value (which is 1).
Everything works ok if I try to get a non-static int variable from Java, or when using a method (even a static one) to retrieve an int value, but GetStatic just doesn’t work.

I ran into the same problem when trying to get the android.os.Build.VERSION.SDK_INT value. Here’s what I did:

private static function GetSDKLevel() : int {
    var clazz = AndroidJNI.FindClass("android.os.Build$VERSION");
    var fieldID = AndroidJNI.GetStaticFieldID(clazz, "SDK_INT", "I");
    var sdkLevel = AndroidJNI.GetStaticIntField(clazz, fieldID);
    return sdkLevel;
}

Storing it as a variable is necessary because it comes back as a long, not an int, and there’s not a convenient way to cast otherwise.

I have no idea why using the AndroidJNIClass doesn’t work as expected.