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

4

14

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

Olds answered 9/9, 2021 at 15:19 Comment(1)
That helpful article seems to be offline now. Thankfully here's an archived version: web.archive.org/web/20230205001121/https://sites.google.com/a/…Henryk
E
10

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..."));
            }
Enterprise answered 9/9, 2021 at 15:28 Comment(2)
Cool! Worked but now the problem is when I want to use the navigation drawer button app instantly crashing. I just added before first value if (item.getItemId/... value MenuItem item = null; assert false; XDOlds
I´m a little confused haha, if you placed the item = null , and then you call item.getItemId, you would get a null pointer exception, but I see no point in doing that. Maybe you are trying to create a variable to access it , so you should do something like: Int itemId= item.getIgemId(); if(itemId == ....Enterprise
A
10

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.

Anemometry answered 28/4, 2023 at 10:11 Comment(2)
Where in the gradle file?Proserpina
In the gradle.properties file that's at the root level of your project. If you have no such file, add it.Anemometry
M
3

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

Michelmichelangelo answered 28/7, 2022 at 16:1 Comment(0)
G
3

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

android {
  lintOptions {
    disable 'NonConstantResourceId'
  }
}
Groyne answered 17/10, 2022 at 13:23 Comment(1)
I guess you should not disable the lint warnings for this. Either handle it or disable android.nonFinalResIds in gradle.Bawdy

© 2022 - 2024 — McMap. All rights reserved.