33

We've got a multi-project app that we're moving to gradle. The build results in Java compilation errors like:

AFragment.java:159: constant expression required
        case R.id.aBtn:

We've confirmed that the constants reported in errors are in the generated R.java.

One clue is that errors are only for switch values. For example there's no error using findViewById(R.id.aBtn).

also note that the constants are from the main project, not one of the library projects.

for anyone looking to get rid of the errors laalto's suggestion will solve it.

the link he provided, together with the fact that eclipse doesn't show the errors that occur when building with gradle gave me another clue. the R.java generated by eclipse defines the main project constants as 'final' but the gradle generated values are not 'final'. the real solution must be in correcting the gradle build files.

SOLUTION (2014-01-09)

our build.gradle for the app was applying the android-library plugin instead of the android plugin. it was this:

apply plugin: 'android-library'

changing it to this:

apply plugin: 'android'

fixed the problem.

user3174822
  • 331
  • 1
  • 3
  • 4

2 Answers2

51

Library project resource identifiers are not constant static final ints, just static ints.

Convert the code that needs to switch on library resource ids to if-else structures.

Further reading: http://tools.android.com/tips/non-constant-fields

laalto
  • 150,114
  • 66
  • 286
  • 303
  • it must be something else because the constants reported are in fact public static final int, because they are part of the main project, not the library project. – user3174822 Jan 08 '14 at 20:11
  • If this does not solve the problem, consider adding additional details of your build setup in the question. – laalto Jan 08 '14 at 20:28
  • It's not something else. It's a function of the version of dev environment and it doesn't matter if it's the main project or a library project .. all the R resource identifiers are not declared final. You just need to move the switch to ann if-else structure – dangVarmit Jan 10 '14 at 01:31
  • This solved the project for me, I'm not sure what I did to make my project a library project though? I guess it's because I'm using a gradle build file that looks [like this](https://github.com/Goddchen/Android-Gradle-Examples/blob/master/Gradle%20Eclipse%20Compatible/build.gradle) (not entirely, but that one was my starting point). – Simon Forsberg Apr 27 '14 at 21:19
  • Nevermind, as the OP states in the question, changing the plugin included in the gradle file fixed that... – Simon Forsberg Apr 27 '14 at 21:41
  • to convert `switch` to `if-else` in Android Studio hit alt-enter see-> http://stackoverflow.com/a/28259041/1815624 – CrandellWS Apr 22 '16 at 18:06
  • 1
    It solves the problem, but ... *why* is it `static final int` int applications and `static int` in libraries? – Krøllebølle Sep 20 '16 at 11:45
10

This happens if you use Resources from a Library project. In that case, the the ids in the R class are not really constants and thus cannot be used in a switch statement.

Ridcully
  • 23,362
  • 7
  • 71
  • 86