简体   繁体   English

如何禁用Android Studio代码样式警告“...可以简化为......”

[英]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: 我正在使用productFlavorsbuildConfigField来启用/禁用我的Android应用中的功能:

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'". 还没有找到一种方法来禁用Android Studio代码样式警告说“'!BuildConfig.FEATURE_SAR'可以简化为'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. 我无法在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. 所以问题只是如何禁用这些警告,而不是如何更好地处理Android / Gradle中的配置管理。

In general once you put the cursor into the warning press ALT + ENTER. 通常,一旦将光标置于警告中,请按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: 应该有一个条目“禁用检查”显示为红色X,如下所示:

例

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: 您还可以插入语句而不是最终值,这样IDE就无法确定是否存在无意义的布尔表达式:

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

The result in your BuildConfig will look like this: BuildConfig的结果如下所示:

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

Btw: This is the same mechanism by which BuildConfig.DEBUG will be generated. 顺便说一句:这与生成BuildConfig.DEBUG机制相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM