PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 27, 2022

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

 April 27, 2022     android, warnings     No comments   

Issue

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


Solution

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.



Answered By - PerracoLabs
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing