118
 @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?

Chaitanya Karmarkar
  • 1,425
  • 2
  • 7
  • 18

1 Answers1

125

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.

vault
  • 3,930
  • 1
  • 35
  • 46
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
  • Yep it is good possible available solution for this warning, but is there any other better alternative solution available for this warning? – Chaitanya Karmarkar Oct 13 '20 at 12:58
  • 76
    I have over 20 cases in one switch statement. I only got this lint warning today after upgrading to Android Studio 4.1. Yet, this is apparently 8+ years old? – grolschie Oct 15 '20 at 04:32
  • Same thing happend with me, why new update gives this warning that should be the real question I think, because Android Studio 4.0 does not gives this warning and everything also works fine at the time of testing. Is anything rising from coffin which wants peace after 8+ years maybe :) ? – Chaitanya Karmarkar Oct 15 '20 at 14:01
  • 4
    @Chaitanya Karmarkar It is at the discretion of Google to define new lint warnings in each Android Studio version, even if in such a case it is an issue that has existed for years: https://issuetracker.google.com/issues/159878591#comment3 – PerracoLabs Oct 15 '20 at 14:16
  • I am agree with you. Just one more query, why everything works perfectly fine everytime at the time of testing even if we use switch statement in onClick()? Why not any problem/bug occurs? – Chaitanya Karmarkar Oct 15 '20 at 14:21
  • 4
    If this is an old error regarding ADT 14, why does the error mention Gradle Plugin 5? – user8127814 Oct 27 '20 at 18:24
  • 9
    From what I understand, ADT 14 stopped using final for identifiers, however, when Google switched to Android Studio, the gradle plugin went back to using final for identifiers, however in Gradle plugin 5 they plan to change it again to being non-final. This means that Java switch statements will break in Gradle 5, and that's the reason for the new lint warning. – user8127814 Nov 02 '20 at 16:04
  • 2
    Hi, guys, I'm new to Android development. I have a question. If it's no longer final, will it be okay to store the resource ID in, say a variable and then later at some point in the app lifetime, that variable is going to be used again to refer to the resource? – LuckMan Nov 13 '20 at 15:07
  • 31
    For a long time the Google persuades developers to use switch case instead of if! Now it suddenly recommends the vice versa. It is very interesting to me. – saeed khalafinejad Dec 16 '20 at 12:19
  • 1
    does it have any impact on kotlin code? can we still use when{..} for this – ansh sachdeva Apr 18 '21 at 15:53
  • generally this only affects library projects. As long as your module doesn't have apply plugin: 'com.android.library', the warning is just false and can be ignored – Robin Gawenda Apr 19 '21 at 12:48
  • 1
    The error content has changed to mention Android Gradle Plugin version 7.0, on Android Studio 4.2 – KenIchi Jun 18 '21 at 05:38
  • @RobinGawenda It used to only affect resources in Library projects. But now I'm seeing it in my application project. And only after updating Android Studio to 4.1.x. – MandisaW Jun 23 '21 at 17:27
  • 5
    To change them automatically you can place the caret on the switch keyword and press Alt + Enter on Windows (Option + Enter on Mac) and select Replace 'switch' with 'if' – DJTano Apr 07 '22 at 17:49