21

Android Studio is now warning this breaking change is coming.

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

Is the only other option an if/else?

Brandon McAnsh
  • 992
  • 8
  • 18
  • 1
    Maybe they want you to use a Kotlin "when" statement? Dunno if the problem would still exist... https://kotlinlang.org/docs/reference/control-flow.html – fattire Aug 27 '20 at 06:44
  • 2
    so what is java solution? switch just support primitive types not views or etc. – Ali Zarei Oct 13 '20 at 08:32
  • I also have this issue with an old project that uses ButterKnife. The errro appears on the BindView line. @BindView(R.id.button_text) protected TextView buttonText; – teh_raab Oct 13 '20 at 08:51
  • 1
    still exists in a when with kotlin; in bytecode it is the same statement. – Brandon McAnsh Oct 13 '20 at 16:24
  • NO, we have to use if/else ladder statement, https://stackoverflow.com/questions/64335374/how-to-resolve-resource-ids-will-be-non-final-in-android-gradle-plugin-version – Lee White Oct 14 '20 at 05:22

1 Answers1

10

Have to use if/else per Google: http://tools.android.com/tips/non-constant-fields.

In a regular Android project, constants in the resource R class are declared like this: public static final int main=0x7f030004;

However, as of ADT 14, in a library project, they will be declared like this: public static int main=0x7f030004;

In other words, the constants are not final in a library project. The reason for this is simple: When multiple library projects are combined, the actual values of the fields (which must be unique) could collide. Before ADT 14, all fields were final, so as a result, all libraries had to have all their resources and associated Java code recompiled along with the main project whenever they were used. This was bad for performance, since it made builds very slow. It also prevented distributing library projects that didn't include the source code, limiting the usage scope of library projects.

The reason the fields are no longer final is that it means that the library jars can be compiled once and reused directly in other projects. As well as allowing distributing binary version of library projects (coming in r15), this makes for much faster builds.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Brandon McAnsh
  • 992
  • 8
  • 18
  • This shouldn't be the accepted answer, as that docs info only applies to Library projects, while the Lint error now shows up in application projects as well. – MandisaW Jun 23 '21 at 17:28
  • Its the same behavior in both regardless of what the mentioned doc says. The linked documentation is also 8 years old. – Brandon McAnsh Jun 24 '21 at 19:40