I have an old (java) Android app project that was created some years ago on an old version of Android Studio (3.1.2) - in fact I think it might have started life on Eclipse. It was (and is) full working and error free code.
However the project has to be moved to a new PC. For various reasons could not install 3.1.2 and get it to work, and anyway, it's old. So, installed the latest Android Studio on the new PC (2024.2.2 patch 1), transferred the project tree over, and tried to open and build it.
Without success.
Firstly it complained about gradle versions, so these were updated to those it recommended.
Next it said there was no namespace defined, so that was added to build.gradle:
...
android {
namespace "my.name.space"
...
Then in an activity that contained a switch statement and cases for menu entries, of the form
...
case R.id.mymenuitem:
...
it said "could not find R" on every case statement, even though they are all defined in menu XML files, eg:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="; > ...
...
<item android:id="@+id/mymenuitem"
android:title="my menu item text" />
...
</menu>
Not sure how I got rid of "could not find R" but a cache flush or clean must have done it.
But now every case statement says "constant expression required", and I haven't found any way of sorting it out - but they are defined as constants in those menu entry XML files.
Not sure what is going on, my knowledge of Android Studio development is pretty limited. As I say, on the old PC and Android Studio it built and ran without error. Can anyone advise?
I have an old (java) Android app project that was created some years ago on an old version of Android Studio (3.1.2) - in fact I think it might have started life on Eclipse. It was (and is) full working and error free code.
However the project has to be moved to a new PC. For various reasons could not install 3.1.2 and get it to work, and anyway, it's old. So, installed the latest Android Studio on the new PC (2024.2.2 patch 1), transferred the project tree over, and tried to open and build it.
Without success.
Firstly it complained about gradle versions, so these were updated to those it recommended.
Next it said there was no namespace defined, so that was added to build.gradle:
...
android {
namespace "my.name.space"
...
Then in an activity that contained a switch statement and cases for menu entries, of the form
...
case R.id.mymenuitem:
...
it said "could not find R" on every case statement, even though they are all defined in menu XML files, eg:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android/apk/res/android" > ...
...
<item android:id="@+id/mymenuitem"
android:title="my menu item text" />
...
</menu>
Not sure how I got rid of "could not find R" but a cache flush or clean must have done it.
But now every case statement says "constant expression required", and I haven't found any way of sorting it out - but they are defined as constants in those menu entry XML files.
Not sure what is going on, my knowledge of Android Studio development is pretty limited. As I say, on the old PC and Android Studio it built and ran without error. Can anyone advise?
Share Improve this question edited Feb 17 at 21:51 nmw01223 asked Feb 17 at 21:32 nmw01223nmw01223 1,7234 gold badges25 silver badges44 bronze badges 4 |1 Answer
Reset to default 0Solved:
It appears that for various reasons gradle 8 and later does not make 'constants' such as R.id.xyz
final any longer. So, won't work in a switch-case
statement - or anything that expects a constant.
So, one answer is to change the code to if-else if
etc.
The other - which is what I did because switch-case
seems more readable in this situation - is to alter gradle behaviour back to how it was. To do this, in gradle.properties
, set
android.nonFinalResIds=false
R
class hasn't been generated from the xml by the build / dev env. And the "constant expression required" errors will be due to that too. But once you have gotten past this, there may be other errors when you attempt to build for more recent Android versions. So you may need to uprate your Android knowledge ... – Stephen C Commented Feb 18 at 4:11