PHD
PHD

Reputation: 685

Is Build.VERSION.SDK_INT constant expression?

Given the boolean and a variable set based on it:

private static final boolean IS_ANDROID_Q = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
private static final int OPTION = IS_ANDROID_Q ? 0 : 1;

There is a switch-case statement:

public static void foo(int option) {
    switch (option) {
        case OPTION: break;
    }
}

The compiler complains about constant expression required error on OPTION variable. However, when I changed Build.VERSION.SDK_INT to other int values, it did not show any error message.

The official documentation states that Build.VERSION.SDK_INT is declared as static final int.

Are there any conditions required for constant expression other than specified in the oracle documentation?

Or did I miss something?

UPDATE 1:

The reason I did not think this post solved my problem is that

private static final boolean IS_ANDROID_Q = {with some int} >= Build.VERSION_CODES.Q;
private static final int OPTION = IS_ANDROID_Q ? 0 : 1;

this runs perfectly fine.

UPDATE 2:

There are mentions of Java switch statement: Constant expression required, but it IS constant possibly being a solution to this question, which I disagree with. If you take a look at the accepted answer for the question, he says that

... the Foo.BA* variables do not have initializers, and hence do not qualify as "constant variables". ...

However as you can see, the variables I declared, IS_ANDROID_Q and OPTIONS, do have initializers. I initially stated that the change of Build.VERSION.SDK_INT to some constant int value led to the successful build.

So the only remaining question is whether or not Build.VERSION.SDK_INT is a constant expression. To give a rationale, a constant expression can only be valid when it is initialized with a compile-time constant expression. You can find this in the question I linked above.

This is the whole reason why I named this question "Is Build.VERSION_SDK_INT constant expression?". It is not about whether my variables have been initialized.

Hence, I do not think this question is a duplicate one.

Upvotes: 2

Views: 1017

Answers (2)

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21778

Build.VERSION.SDK_INT is not a constant expression because it is a simple name of a non constant variable. A constant variable can only be initialized with the constant expression. A constant expression cannot include a function call as part of its initialization statement and here we see a call to SystemProperties.getInt(..).

Hence you cannot use Build.VERSION.SDK_INT as a label in a switch statement and other places where only constant expressions are acceptable.

Upvotes: 1

PHD
PHD

Reputation: 685

I searched for source code for Android SDK and confirmed that Build.VERSION.SDK_INT is not a constant expression.

Here is the definition of Build.VERSION.SDK_INT:

public static final int SDK_INT = SystemProperties.getInt(
        "ro.build.version.sdk", 0);

Upvotes: 0

Related Questions