How to resolve "Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case statements" warning?

AndroidWarnings

Android Problem Overview


 @Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.legalInformationLL:
            startActivity(new Intent(AboutActivity.this, LegalInformation.class));
            break;
        case R.id.backIB:
            finish();
            break;
    }
}

for this code "Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case statements" warning shows up. What is the possible solution? If I change this code to:

 @Override
public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.legalInformationLL) {
        startActivity(new Intent(AboutActivity.this, LegalInformation.class));
    } else if (id == R.id.backIB) {
        finish();
    }
}

warning goes; but switch statement is better for performance as compared to if statement. So what is the possible solution that will work efficiently faster?

Android Solutions


Solution 1 - Android

The issue happens because since ADT 14 resource identifiers are no longer final.

See the next link, where Google states to use instead "if/else" conditions as alternative:

http://tools.android.com/tips/non-constant-fields

That being said. when it comes to performance, "switch" statements may perform better than "if/else" conditions.

However, in your case, you do not gain or lose performance or efficiency.

The type of performance that a "switch" statement may give, has to be taken into consideration for more specific edge cases which may require high efficiency, such as render loops, or algorithms with efficiency in focus.

For your use-case, using an "if/else" condition is a good solution without efficiency issues.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChaitanya KarmarkarView Question on Stackoverflow
Solution 1 - AndroidPerracoLabsView Answer on Stackoverflow