Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 587
hgoebl Avatar asked Jan 09 '16 11:01

hgoebl


2 Answers

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

like image 166
Denis Lukenich Avatar answered Nov 13 '22 09:11

Denis Lukenich


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.

like image 43
degill Avatar answered Nov 13 '22 09:11

degill