最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Transferring project from old Android Studio - will not build - Stack Overflow

programmeradmin3浏览0评论

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
  • might be helpful or related Cannot find R.layout.activity_main – chickity china chinese chicken Commented Feb 18 at 0:15
  • The "could not find R" will happen if the 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
  • Hello nmw01223, create a new project, and paste in it the files of your classes (in their respective folders), in this way, you fet about all the original configurations, which are the ones that generate the errors. – Marce Puente Commented Feb 18 at 5:22
  • Thanks for the comments, creating a new project and pasting in the files is probably the sensible way, avoids all sorts of stuff. Actually, I DID find the answer to this particular one - see below - but if any more oddities show up, that's what I'll do. – nmw01223 Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Solved:

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
发布评论

评论列表(0)

  1. 暂无评论