Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in switch case statements

12.3k Views Asked by At

I have a warning in Android Studio about my navigation drawer resources. Warning is:

Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in switch case statements.

I tried to use the method if to update my code but I won't 'converted right'. I found on the internet this article to help me to convert my code but it seem not work for me. I want to know if I miss something.

Down below is before and after to make an idea and here is my full activity because I saw lot of people have this after used public void onClick... and I don't use it. I have navigationView.setNavigationItemSelectedListener(menuItem -> {.

Before

navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (menuItem.getItemId())
            {
                case R.id.nav_drawer_settings:
                    Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_whitelist:
                    intent = new Intent (MainActivity.this, WhitelistActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_clipboard_cleaner:
                    intent = new Intent (MainActivity.this, ClipboardActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_invalid_media_cleaner:
                    intent = new Intent (MainActivity.this, InvalidActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_about:
                    intent = new Intent (MainActivity.this, AboutActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_support:
                    Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
                    startActivity(newIntent);
                    break;
                case  R.id.nav_drawer_share:{
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                    sharingIntent.setType("text/plain");
                    String shareBody =  "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
                    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                    startActivity(Intent.createChooser(sharingIntent, "Share using..."));
                }
                break;
            }
            return false;
        });
    }

After

navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (view.getId()) {
                if (item.getItemId() == R.id.nav_drawer_settings) {
                    Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_whitelist) {
                    Intent intent = new Intent (MainActivity.this, WhitelistActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_clipboard_cleaner) {
                    Intent intent = new Intent (MainActivity.this, ClipboardActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_invalid_media_cleaner) {
                    Intent intent = new Intent (MainActivity.this, InvalidActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_about) {
                    Intent intent = new Intent (MainActivity.this, AboutActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_support) {
                    Intent openURL = new Intent(Intent.ACTION_VIEW);
                    openURL.setData(Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
                    startActivity(openURL);
                }
                if (item.getItemId() == R.id.nav_drawer_share) {
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                    sharingIntent.setType("text/plain");
                    String shareBody =  "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
                    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                    startActivity(Intent.createChooser(sharingIntent, "Share using..."));
                }
            }
            return false;
        });
    }

Thanks in advance! :D

4

There are 4 best solutions below

2
Sergio Pardo On BEST ANSWER

On the after code that you posted, there is no need anymore for the switch.

It should work only by writing the following:

            if (item.getItemId() == R.id.nav_drawer_settings) {
                Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
                startActivity(intent);
            }
            if (item.getItemId() == R.id.nav_drawer_whitelist) {
                Intent intent = new Intent (MainActivity.this, WhitelistActivity.class);
                startActivity(intent);
            }
            if (item.getItemId() == R.id.nav_drawer_clipboard_cleaner) {
                Intent intent = new Intent (MainActivity.this, ClipboardActivity.class);
                startActivity(intent);
            }
            if (item.getItemId() == R.id.nav_drawer_invalid_media_cleaner) {
                Intent intent = new Intent (MainActivity.this, InvalidActivity.class);
                startActivity(intent);
            }
            if (item.getItemId() == R.id.nav_drawer_about) {
                Intent intent = new Intent (MainActivity.this, AboutActivity.class);
                startActivity(intent);
            }
            if (item.getItemId() == R.id.nav_drawer_support) {
                Intent openURL = new Intent(Intent.ACTION_VIEW);
                openURL.setData(Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
                startActivity(openURL);
            }
            if (item.getItemId() == R.id.nav_drawer_share) {
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String shareBody =  "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(sharingIntent, "Share using..."));
            }
0
Sunil Bhagwat On

even if you put case R.id.nav_drawer_settings: AS case (R.id.nav_drawer_settings): in bracket it will work

1
Baccata On

If one just wants to suppress this warning for the whole application, add this to ones build.gradle file:

android {
  lintOptions {
    disable 'NonConstantResourceId'
  }
}
2
Adil Hussain On

You can override the new default behaviour for resource IDs in your project by adding the following property to your project's gradle.properties file:

android.nonFinalResIds=false

See Android Gradle plugin 8.0.0 (April 2023) > Breaking changes: build option default values for the full list of build options which have new default values as of Android Gradle Plugin 8.0.0.