hgoebl
hgoebl

Reputation: 13007

How to disable Android Studio code style warning "... can be simplified to ..."

I'm using productFlavors and buildConfigField to enable/disable features in my Android app:

productFlavors {
    vanilla {
        buildConfigField "boolean", "FEATURE_SAR", "false"
    }
    edge {
        applicationIdSuffix ".edge"
        buildConfigField "boolean", "FEATURE_SAR", "true"
    }
}

Haven't found a way to disable Android Studio code style warning saying "'!BuildConfig.FEATURE_SAR' can be simplified to 'false'".

As you can see in my code, I've tried many ways but none of them works. And I cannot find the setting where I can disable this in Android Studio.

@Override
@SuppressWarnings("SimplifiableIfStatement") // no effect
@SuppressLint("SimplifiableIfStatement")     // no effect
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Set<Integer> suppressPositions = new HashSet<>();

    //noinspection SimplifiableIfStatement <-- no effect
    if (!BuildConfig.FEATURE_SAR) {
        suppressPositions.add(IDX_SAR);
    }

    SimpleAdapter adapter = ViewUtils.createSimpleAdapter(
        getContext(), MENU_RESOURCES, suppressPositions);
    setListAdapter(adapter);
}

Of course there are ways to get around this by using resources, but for some reasons I cannot use resources in all cases. So the question is only how to disable these warnings, not how to better handle config management in Android/Gradle.

Upvotes: 5

Views: 1947

Answers (2)

degill
degill

Reputation: 1325

Just in case some more people stumble upon this rather old question.

You can also insert a statement instead of the final value, this way the IDE should not be able to determine if there is a pointless boolean expression or not:

buildConfigField("boolean", "FEATURE_STAR", "Boolean.parseBoolean(\"true\")")

The result in your BuildConfig will look like this:

public static final boolean FEATURE_STAR = Boolean.parseBoolean("true");

Btw: This is the same mechanism by which BuildConfig.DEBUG will be generated.

Upvotes: 2

Denis Lukenich
Denis Lukenich

Reputation: 3164

In general once you put the cursor into the warning press ALT + ENTER. Then enlarge the context-menu of your warning. There should be a entry "Disable Inspection" displayed with a red X as shown here:

Example

Upvotes: 5

Related Questions