1096

I just downloaded and installed the new Android SDK. I wanted to create a simple application to test drive it.

The wizard created this code:

package eu.mauriziopz.gps;

import android.app.Activity;
import android.os.Bundle;

public class ggps extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

but Eclipse gives me the error

R cannot be resolved

on line

setContentView(R.layout.main);

Why?

PS: I do have an XML file named main.xml under res/layout/.

Hitesh Sahu
  • 31,496
  • 11
  • 150
  • 116
Maurizio Pozzobon
  • 2,804
  • 7
  • 32
  • 42
  • For those who still experience some problem with eclipse not being able to update/generate R.java. A possible solution is by creating a dummy xml and just delete that xml. In my case this is the solution – denil Jun 25 '12 at 10:59
  • Honestly, this sounds so stupid (and it is), but modifying the AndroidManifest.xml, then undoing that edit, then saving the file worked for me. If I cleaned the project after that, it broke again. And all this time I thought Xcode was the only IDE that had these kinds of mysterious errors. – iwasrobbed Dec 22 '12 at 18:12
  • 3
    Did you try right click on the project -> Android Tools -> Fix Project Settings? – Serg Feb 01 '14 at 10:59
  • 6
    Please check your XMl layout file, if any error in xml you can't generate R.java, and the error comes after bulid and clean also. – swiftBoy Feb 20 '14 at 11:47
  • 1
    Just a tip: when I switched IDE from Eclipse to Android Studio this stopped happening to me. – mata Jul 17 '14 at 12:13

108 Answers108

844

After tracking down this problem as well, I found this note in the Android documentation:

http://source.android.com/source/using-eclipse.html

*Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.*

While going through the Android sample tutorials, I would often use the Ctrl + Shift + O command to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.java class that is automatically generated when you build.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Michael Levy
  • 12,767
  • 15
  • 60
  • 98
  • Indeed annoying and this is the correct answer. If you are having this trouble with a library project, you can discover this is happening if the library project will compile when you fully namespace the R reference to the library project. – Timothy Lee Russell Nov 05 '11 at 19:04
  • For what its worth, the referenced page seems to have been updated. The original reference can still be found in Google cache - http://webcache.googleusercontent.com/search?q=cache:efQa13VcFc8J:source.android.com/source/using-eclipse.html+&cd=5&hl=en&ct=clnk&gl=us – Michael Levy Nov 07 '11 at 00:31
  • If it still isn't working for you try importing R directly by import [PROJECT_NAME].R; – Gabriel Fair Feb 08 '12 at 11:49
  • 32
    This happens as well, but a bunged up layout file can also cause it. – GHz May 20 '12 at 02:40
  • 114
    This classic bug has been in Eclipse for YEARS and never gets fixed. 1000s of people face this same problem. – Carol Jun 15 '12 at 05:58
  • I recently upgraded Eclipse and ALL my projects had this problem. One was fixed by simply deleting the import statement as suggested above. I fixed the rest by forcing a recompile (e.g. adding a comment and removing it) - I assume a Project > Clean would also do the job. – Snowcrash Jul 31 '12 at 06:53
  • 53
    Eclipse developers, please remove this $£%&"$&/ bug! Google developers, please give me a £$&$"%& Android suite similar to Xcode for the iPhone! – Abramodj Oct 28 '12 at 18:30
  • @Abramodj This is not a bug in Eclipse. When you create a project for first time, build it, and it will generate `R.java` file which will resolve this error. – GoodSp33d Dec 08 '12 at 06:57
  • 18
    @2-Stroker : It is a bug. It happened to me when I created a branch new andorid project via the eclipse WIZARD and it complained about 'R cannot be resolved'. I had to comment out the setContentView & getMenuInflater lines and then eclipse generated the R.java after which I could uncomment setContentView & getMenuInflator. Abramodj is right, the environment has serious problem if even your 'hello world' example does not compile. Fix it google! – CF_Maintainer Mar 17 '13 at 17:28
  • 5
    How does Google not grant eclipse.org funds to get items like this fixed? – Cris Jun 30 '13 at 23:32
  • @CF_Maintainer: Best and simplest solution for this bug. – latestVersion Feb 06 '14 at 00:51
  • 1
    it could be anything, in my project, R stopped resolving because I had images with uppercase letters in my res/drawable folder. I was not aware of this, renamed them with all lowercase and R suddenly worked/resolved – Lars Anundskås Feb 26 '14 at 00:27
  • I have imported a project that compiled just fine on another machine. Eclipse and the Android SDK had been installed on that machine the exact same way I have installed them on my new computer, though it may be a different version. Now the project does not compile on the new box and issues the sh***y "import com.xxx.xxx.android.R cannot be resolved" error. The imports were already there on each and every file on the old project. So what's the workaround? – matteo Mar 22 '14 at 19:01
  • I notice that there is no R.java file inside gen/.../... and I suspect this is related to the fact I'm also getting a bogus "Project has no project.properties file!" error, while the project does have a project.properties file. I guess this is why the R.java file doesn't get generated which in turn causes the other error, right? Does anybody know what's the workaround to solve the project.properties error? – matteo Mar 22 '14 at 19:05
  • this happens whenever I clean a project, and can cost days or weeks. i succumbed to temptation and cleaned my project today as a graphics file wasn't being recognized and now i wonder how many days i'll waste trying to fix it this time. cleaning is never worth the risk. – Androidcoder Apr 03 '14 at 00:43
  • Build project just Solve this issue for me.i go to Project-> Build All – LittleGirl Dec 17 '14 at 18:44
393

Each time I had a problem with R not been generated, or even disappeared, this was due to some problem in the XML layout file that prevented the application from being built.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Luc
  • 15,030
  • 32
  • 113
  • 175
  • 21
    +1 for this. I had an older styles.xml file with parent="android:WindowTitleBackground" and it was breaking my build and preventing anything generated to "gen". The error wasn't actually reported except in the Console window right when I first opened the project. – Scott Persinger Mar 08 '12 at 00:56
  • 12
    exactly, if you copied layouts from another project, and are getting this, looks for missing drawables - that will fix it instantly! – Gabor May 22 '12 at 11:45
  • I got it when I clicked "Run" with layout file (activity_main.xml) open, and it generated the empty `activity_main.out.xml`, and then found an error in that. I had to delete the `...out.xml` and click Run on the project. All fixed! See http://stackoverflow.com/questions/11254618/no-xml-content-please-add-a-root-view-or-layout-to-your-document – Stephen Hosking Aug 25 '12 at 07:29
  • 4
    I thinh I have the same problem, is there any way to know witch one of the layouts? – Sergey Kucher Oct 13 '12 at 08:57
  • 3
    Yes, I was missing a drawable file and that prevented the R file from even being generated. The weird thing is that I had a `android:icon="@drawable/XXX"` declaration on an `` in a menu resource, but there was no red squiggle signalling that the drawable resource XXX wasn't found. In other instances, `"@drawable/YYY"` _was_ flagged when YYY was not found, but not in this case. For some reason. – taylor Apr 12 '13 at 18:08
  • 5
    Yes, excellent solve. When one gets this error.. many times all of the layout files are marked as having errors.. This is because they are all looking for R. One of them however, likely has an error unrelated that aborts the build. In my case, a missing drawable. – Todd Painton Apr 18 '13 at 19:46
  • For me, it was an incorrect link to a @android:drawable location. Hope that helps someone. – proudgeekdad Feb 21 '14 at 04:23
  • Where I work, the XML file that often seems to be the root cause for an unresolved `R` and lots of broken XML files is the AndroidManifest.xml file, and specifically, the `@integer/google_play_services_version` element. The correct solution for this problem is explained here: http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject (via http://stackoverflow.com/a/20068489/1450294) – Michael Scheper Feb 25 '14 at 22:48
  • Usually when I have error in **XML files** this error appeared!! I think this answer is right answer of the problem!! @Michael Levy – Emad Aghayi Apr 19 '15 at 09:46
  • it is true that issues are mostly caused by XML errors. in the case of Android Studio, i think this post can help if people are trying to get over compilation issues and wishes to identify the problem: http://stackoverflow.com/questions/27407855/android-studio-failed-to-complete-gradle-execution-cause-is-empty/31447763#31447763 – Mingsheng Aug 24 '15 at 11:49
202

Whenever you get

R cannot be resolved

then check for the /res directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.

If you have any additional, even unused (!) or unreferenced (!) images in a folder like res/drawables-mdpi which do not comply to the file naming conventions (may contain only [a-z0-9_.]), the R.java class might not generate, causing the chain of events all the other posts referred to. Hope it helps!

Fluffeh
  • 31,925
  • 16
  • 62
  • 77
Vasu
  • 4,802
  • 6
  • 39
  • 48
  • I had this problem with the sample ApiDemos. Some missing resource in the strings.xml file. XML error are listed in the Eclipse's console. – lemotdit Jul 31 '10 at 03:34
  • thank you! this was my issue, i had a layout that was erroring out but there was no message about it. –  Nov 22 '10 at 13:58
  • 29
    How can you check for errors? I get nothing in my console when building/running – Vincent Apr 11 '11 at 09:33
  • If it still isn't working for you try importing R directly by `import [PROJECT_NAME].R;` – Gabriel Fair Feb 08 '12 at 11:48
  • This was my problem. Stuck a pdf in there instead of assets. – Gaʀʀʏ Dec 05 '12 at 20:54
  • I had a folder with a custom name in the `/res` folder, preventing the R file from being build. Custom folder names in the `/res` folder seems not to be supported according to http://stackoverflow.com/a/16749758/1560056. – CleanUp Aug 10 '13 at 13:58
  • 4
    How to find out WHICH xml file causes the error? Eclipse does not mark them! – Yar Apr 18 '14 at 11:35
  • I have find an error in drawable. one of my images name is incorrect. It is normally due to drawable with wrong names. – AndroidGeek Jul 07 '14 at 10:45
  • Mine was caused by incorrectly naming a raw file: It gave this Error message: "Invalid file name: must contain only [a-z0-9_.]" – wizurd Jul 19 '14 at 03:46
  • 1
    I had 2 images with same name in hdpi folder (1 with png and 1 with jpg format) deleting 1 of them resolved the issue – Haseeb Jadoon Nov 02 '14 at 06:42
  • Two images with same name, image.png and image.xcf, left over from inherited legacy code, in res/drawable/_raw. No flag indicators on any of these directories/files in latest Eclipse, Apr '16. – DragonLord Apr 15 '16 at 01:20
  • While this is one road to this error it is not the only one. – GregD Apr 20 '16 at 01:29
134

my project have include a r.java.at the beginning ,R.layout.main work good.But,after adding some code it doesn't work,and the error is R.layout.main can't resolved.what's the problem?

Look at your imports. Chances are that the line:

import android.R;

will be there. If that's the case, remove it, so that your project will resolve R not with the default Android Resources class, but with the one auto-generated from your /res/ folder.

Sam
  • 84,460
  • 18
  • 172
  • 171
Dimitar Dimitrov
  • 15,154
  • 4
  • 48
  • 51
  • 5
    Thanks @Dimitar,that fixed my issue! Eclipse and the auto-import feature seem to have gotten me by adding that import line automatically. – Steven Jun 08 '10 at 03:12
  • 7
    I've also had similar issues when I named a layout using capital letters – Travis Oct 24 '10 at 20:46
  • This is one of the top 5 reasons people get "R not found". I've compiled a problem/solution page with images that show the main 5 at link below. Truly hope the list helps people since this is a common problem. http://spragucm.wordpress.com/2014/05/13/android-r-cannot-be-resolved-to-a-variable/ – Chris Sprague May 13 '14 at 19:17
  • after removing R, at some places R is used as class & is giving error now, How to resolve that? – Narendra Jaggi May 24 '15 at 17:36
96

And another thing which may cause this problem:

I installed the new ADT (v. 22). It stopped creating gen folder which includes R.java. The solution was to also install new Android SDK Build Tools from Android SDK Manager.

Solution found here

Trying Tobemyself
  • 3,580
  • 3
  • 26
  • 44
Michał Klimczak
  • 11,170
  • 7
  • 55
  • 92
  • Myself also. And when I selected the SDK manager, lo and behold, the build tools shows up auto-checked for installing, with a note that it *wasn't* installed! What gives??? – David at HotspotOffice Jun 03 '13 at 20:46
  • I had the same issue. I'm so glad you shared this. I looked error from my own code two days. I'm very diappointed to Google, because this error message has been very difficult to solve, but it has been caused by own error, allthough tools dis not report properly what causes it. I has done same time changes to my project so I thinked it was my fault again. – Reijo Korhonen Jun 23 '13 at 10:51
  • 5
    Worked for me, thanks! It just reinforces my mantra: "never update anything while developing". – Rodrigo Dias Jul 16 '13 at 22:11
  • i was hanging around for looking for the answer but nothing worked for me. i think it was a eclipse or sdk problem. so as you are saying, i just delete prev eclipse bundle and reinstall it. and then it is working. your answer was helped – beginners Jul 24 '13 at 00:42
  • This was the problem in my case! Wasted one day trying to fix the R error, adding/removing references, and trying other options. Mine was a fresh new setup of ADT, and a new android project creation, and was still getting the error. It turned out finally, I had to install all available updates of Android SDK Tools, Android SDK Platform Tools and SDK Build Tools :p – Saurabh Kumar Aug 04 '13 at 03:19
  • I worked till this point and it didn't resolve my issue. I then realize my Android installation directory had a eclipse folder. I used that eclipse instead and the project created was smooth as butter. Earlier I used my normal eclipse that had Android plugin set up done by me. – Mustafa Aug 11 '13 at 02:13
  • this is on the Window menu if anyone can't find it – Rob Sedgwick Dec 14 '13 at 23:29
87

R.java is a file that the Android Eclipse plugins creates while building your application. R.java is created under the "gen" directory. This file is generated from the information in the "res" directory. If you run select "Project" -> "Clean..." on the Eclipse menu, it will remove and then regenerate the R.java file.

The problem "R cannot be resolved" happens when you change your package name in the AndroidManifest.xml file. It uses your Android package name to create a subdirectory under the "gen" directory where it stores the R.java file.

Eclipse may have problems executing clean, because it is confused about where the R.java file is when you have changed the Android package name. You can either rename the subdirectory under gen to match your new package name, or you can change your package name back to the old name. Do the clean and then change the package name to the new name you want. This works best if you stop Eclipse from trying to build while you are changing the package name. Under the "Project" menu uncheck the option to "Build Automatically" and also when the "Clean..." dialog asks if it should "Start a build immediately" uncheck the box so it doesn't try to build while you are changing the package name. After you have changed the name you can turn "Build Automatically" back on again.

Note that if your AndroidManifest.xml file package name does not match your Java package name, Eclipse will end up automatically adding an "import <your Android package name>.R;" line in all your .java files that have any references to R. If you change your AndroidManifest.xml package name, sometimes Eclipse does not update all of these added imports. If that happens, use the Eclipse refactoring (ALT + Shift + R) to change the import statement in one of your Java files to your new AndroidManifest.xml package name. It is best to do this while you have disabled "Build Automatically".

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • 21
    This looks like a cut and paste without reference – Casebash Apr 28 '10 at 00:06
  • 1
    Multiple Copy/Paste of files and name refactoring lead to the same issue, despite all file names and contents were Ok. Cleaning the project, deleting R.java manually and all the import headers, then rebuilding these import headers (still with CTRL+ SHIFT + O) worked fine. – hornetbzz May 18 '12 at 23:07
  • This worked for me. I had changed the package name in the manifest. I had to use ctrl-shift-O in the classes that referenced the generated R class values in order to add the correct import. – Chuck Krutsinger Jul 05 '12 at 17:07
  • This fixed it for me. Also have seen the import issue occur in the past, but this time the issue was the package name in the manifest was not matching the package name in the src folder. – Jim Range Apr 03 '14 at 18:31
  • I had two activities in my application. One had a different package name to the one that R was generated against. I had to import R manually using something like `import com.example.main.package.name.R;` – Oliver Moran May 16 '14 at 14:16
  • Thanks, this solved my problem! I received this error after refactoring my package name. I checked my res files, updated my manifest, etc but completely overlooked that the gen folder was still generating the R files under my original package name! – Chris L Nov 04 '14 at 18:34
  • I solved it by deleting the Build folder inside the module. Then rebuilding. – James Andrews Jul 07 '17 at 07:51
85

What Will said was right

R is an automatically generated class that holds the constants used to identify your >resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in >Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to >Project > Build all (and selecting "Build Automatically" while there as recommended by >Josef). If that doesn't work than try making a new project, if the problem is recreated than >post here again and we'll go into more detail.

but I've found out that there was another problem that was causing the first one. The tools in the SDK directory didn't have the permissions to be executed, so it was like the didn't exist for Eclipse, thus it didn't build the R.java file.

So modifying the permission and selecting "Build Automatically" solved the problem.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Maurizio Pozzobon
  • 2,804
  • 7
  • 32
  • 42
  • @Jignesh - `cd $ANDROID_SDK_ROOT/build-tools/`. Then `chmod a+x` on files like `aapt`, `aidl`, `dexdump`, `dx`, etc. `ANDROID_SDK_ROOT` should be set in your environment, and it will point to your SDK installation. For example, `/opt/android-sdk`. Its important to set both `ANDROID_SDK_ROOT` and `ANDROID_NDK_ROOT` because the Android tools use them internally. See [Recommended NDK Directory?](https://groups.google.com/d/msg/android-ndk/qZjhOaynHXc/2ux2ZZdxy2MJ) on the NDK user's list. – jww Feb 14 '14 at 05:54
  • @Lewis.. I already select the "Build Automatically" but the error is still there. – d_unknown Apr 26 '15 at 02:05
39

R is an automatically generated class that holds the constants used to identify your resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to Project > Build all (and selecting "Build Automatically" while there as recommended by Josef). If that doesn't work than try making a new project, if the problem is recreated than post here again and we'll go into more detail.

Will
  • 17,799
  • 10
  • 39
  • 40
27

Close all files, clean project, restart Eclipse.

Cosmin
  • 2,192
  • 2
  • 20
  • 21
sharika
  • 1
  • 2
  • 3
24

It is worth checking in AndroidManifest.xml. The attribute package has the correct value.

That is:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="your.correct.package.name"
   ...

After you change that, the R.java will be re-generated.

Garg's
  • 2,526
  • 1
  • 31
  • 44
green
  • 87
  • 1
  • 5
  • 13
  • My original package name was com.xxxxxdroid.router. I created a new version of my app com.xxxxxdroid2.router. Once I had done this I had to update the package name in AndroidManifest.xml, but only much later after I had started to copy and paste old classes across. Thanks for the pointer! – Eugene van der Merwe Jan 28 '12 at 23:41
  • this fixed my issue, I was working in an xml file, glad I remembered which one, I've got loads of them... thanks... – GHz May 20 '12 at 02:39
19

Along with the great suggestions in the previous answers, make sure your Android target is set:

  1. Right-click on your project
  2. Choose Properties
  3. Choose Android in the left menu
  4. Tick a box next to the appropriate Project Build Target.
  5. Click Apply and OK

Edit: A year later I found another cause. I had a .jpg image in my drawable folder with the same name as a .png image. Referencing this image in my code must have confused the program and it gave the "R cannot be resolved" error.

satyrFrost
  • 363
  • 7
  • 16
19

This error can also be caused by adding an activity to a namespace that is different to the root namespace for your package.

For example, if com.example.myapp is the root namespace for your package, you can then add an activity to the com.example.myapp.activities namespace.

This will produce the "R cannot be resolved" error.

To fix the import the R in the default namespace in your activity should be:

import com.example.myapp.R;
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Rob
  • 4,593
  • 3
  • 29
  • 29
  • This helped me, I had moved my activities into a package below the root, adding the root package as an import (import com.example.myapp.R;) solved the problem. – eageranalyst Feb 09 '13 at 12:45
17

Make sure you installed the Android build tool form sdk manager

project right click properties-> Java BuildPath select Library and add android-support.jar the follow these step.

Go to Project->Properties->Java Build Path than select Order and export tab. Set android-support .jar library checked and up it into top of the list. And clean and rebuild..It works for most of the cases

enter image description here

Sunil Kumar
  • 7,027
  • 4
  • 30
  • 48
15

I just had this problem for the millionth time and realized what was causing it: I created an XML file with uppercase letters in the name. All your XML filenames in /res must match [a-z0-9\\._].

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • also, i just noticed stack overflow requires us to escape all backslashes. –  Jul 01 '10 at 02:52
14

Simplest solution - Sometimes you just need to save the XML file you were working on to get the autogenerator to kick in.

Save the file (e.g. main.xml) then delete the R.java file and see if the regenerated R.java resolves the R resolve problem.

akim
  • 301
  • 3
  • 5
  • True. Sometimes, if it says there is a problem in your XML where there isn't (red squiggly line or yellow exclamation point), delete the offending line, save the XML file, paste the line back in, and save it again. If you are not building automatically, build again after that, and it just might work. Eclipse is stupid sometimes. :) – hotshot309 Jan 21 '12 at 04:58
  • Despite the other answers listed above, this one turned out to be the correct one for me. I initially got the "cannot find APK" message when I went to compile some sample code, then did a Clean which generated a ton of "R cannot be resolved" errors, and then just opening the .xml file and manually saving it the errors were cleared and I could compile. – sXe Jun 01 '12 at 02:15
  • So true: After cleaning the projects the R.java was gone - editing and saving the strings.xml solved the problem. – BurninLeo Jun 16 '12 at 05:34
12

Check the XML file names. Be sure that they're all in lowercase.

Also make sure that any image resource names are also all in LOWER CASE. I had a capital letter in the name of my jpg file, and it caused the R unresolved error right across my project.

Nick Wright
  • 1,103
  • 8
  • 18
TonyS
  • 1
  • 1
  • 2
  • +1 for this was my issue. You must name your images like so: eicon.png not eIcon.png. I found the error by looking at the Consol. It was returning an error on the file with Invalid File Name:... – jasonflaherty Nov 01 '12 at 05:53
12

R is a generated class. If you are using the Android Development Tools (ADT) it is generated whenever the project is built. You may have 'Build Automatically' turned off.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Josef Pfleger
  • 72,585
  • 15
  • 95
  • 99
10

You may need to update/install SDK tools. Relaunch Android SDK Manager again and install a new item: Android SDK Build-tools.one by one delete,fix which one work for you.enter image description here

Nazmul Hasan
  • 8,494
  • 5
  • 44
  • 64
10

This error cropped up on my x64 Linux Mint installation. It turned out that the result was a failure in the ADB binary, because the ia32-libs package was not installed. Simply running apt-get install ia32-libs and relaunching Eclipse fixed the error.

If your x64 distro does not have ia32-libs, you'll have to go Multiarch.

Check #4 and #5 on this post: http://crunchbang.org/forums/viewtopic.php?pid=277883#p277883

Hope this helps someone.

André Onuki
  • 214
  • 1
  • 11
kpax
  • 339
  • 3
  • 11
  • It's worth noting that if you have a 64-bit Linux installation you WILL need ia32-libs. If you don't have it, attempting to execute the adb command will result in a strange message saying it can't find the file or directory. – rs_atl Jun 21 '12 at 18:52
9

You may need to update SDK tools. Relaunch Android SDK Manager again and install a new item: Android SDK Build-tools.

enter image description here

Eduardo Dennis
  • 12,511
  • 11
  • 72
  • 102
8

I had this problem as well. It turned out that I had inadvertently deleted the "app_name" string resource from the strings.xml file, which was causing a silent error. Once I added it back, the R class was generated successfully and everything was back up and running.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Guy Starbuck
  • 20,727
  • 6
  • 51
  • 63
6

Yet another reason R.java might not get autogenerated is if you have directories like res/drawable-hdpi, res/drawable-mdpi, or res/drawable-ldpi.

1.6+ seems to be OK with these directories, but 1.5 doesn't want them. When I removed those directories, R.java started autogenerating for me again.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
beppu
  • 106
  • 1
  • 3
6

Try to make your new XML layout file name lower case. For example, use my_file.xml instead of myFile.xml.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Malcom
  • 1
  • 1
  • 1
6

First check is there any error in any xml layout or not, if then resolve it first.

Otherwise remove junit dependency from project and rebuild the project.

enter image description here

Jaimin Prajapati
  • 331
  • 4
  • 13
5

Often times this is because of the MinSDK version number you supplied when creating the project. Example:

If you want 2.1 to be the minimum, Android 2.1 is actually API Level 7.

You can see what I am talking about when you browse the SDK you downloaded and installed. Navigate to the place you installed the SDK to (C:\android-sdk-windows for example) and open the folder named "platforms". You will see something like "android-7" listed as a folder, and if you open that there is a source.properties file that, when opened with a text editor, will show you the corresponding platform version.

When you create a project, and you must select a "Build Target" API, the last column in that list named "API Level" shows the number you are looking for when populating the MinSDK setting.

This is probably one of the most common mistakes that results in the R.java file not being created under Project > gen > packagename > R.java.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
5

Remove main.out.xml. I'm new to this and don't yet know what this file is used for, but removing it cleared the problem.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user389061
  • 51
  • 1
  • 2
5

Just go to Android Top menu list. click on Build Menu, in under Build click on Rebuild Project.

enter image description here

Gundu Bandgar
  • 2,383
  • 1
  • 15
  • 20
4

Unfortunately none of the existing questions helped. Since my problem seems platform specific, this may only work if you are on Ubuntu 64 Bit (I am currently using Ubuntu 12). At first I did not see the "Problems" Window, which already hinted a solution, that on 64 Bit Environment you need the 32 Bit libs, which may be missing:

sudo apt-get install ia32-libs

I installed the package and rebuild my project, which fixed the issue for me.

Bernhard Kircher
  • 3,892
  • 3
  • 29
  • 38
4

restart your computer.
Nothing helped me except this way

dvrm
  • 3,781
  • 4
  • 32
  • 44
4

In case anyone is interested (I might be saving your life here), I had the error, R.xml cannot be resolved, slightly different on a GLS project. Hmmmm. After looking in R.java, I found an auto-generated class, XML.java, (I think) was not there.

Solution? It needed a new folder in res: res\xml and a file called default_values.xml in there. Then all was OK.

Just in case you have not got that file, it's:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
phil
  • 1
  • 1
4

Every time I come to this error and always been landed on SO. and end of the day the reason is same.

So Please Keep in mind always this small note

I found most of the time reason is - error in XML layout

If your XML isn't correct, then the R file is not generated after a build.

swiftBoy
  • 33,793
  • 26
  • 129
  • 124
4

In my case, R.java was not generating because there was a problem in AndroidManifest.xml file wherein I had wrongly specified the name of an activity!

user3024292
  • 97
  • 2
  • 10
  • Correct. My Manifest had a wrong theme `android:theme="@style/Theme.AppCompat.Light"`. I have removed this property from application tag, gone to Project->Clean menu and R.java was regenerated correctly. – Junior Mayhé Jun 14 '14 at 19:50
  • xml file with error like layout or manifest will prevent eclipse from auto generating R res – macio.Jun Jul 18 '14 at 19:52
4

Install all Android SDK Build-tools and remove import android.R in your Activity class. After click Project -> Clean

import android.R;

enter image description here

ethemsulan
  • 2,113
  • 24
  • 19
4

I had the examples of Android 8 and was trying to use Android 7 SDK. When I closed the project and reopened the application folder and chose to use Android 8 SDK, it was able to find the R file. Hope this helps.

vkris
  • 1,837
  • 7
  • 20
  • 28
4

In my case, I had an error in my AndroidManifest.xml. Others have said that your XML files must be free from errors, but I was only looking in the res/ folder. Find and fix as many possible errors and the problem may well resolve itself.

Brian Lacy
  • 17,820
  • 9
  • 50
  • 73
  • This was the culprit in my case. ANY error in the manifest.xml file can cause this problem if R.java hasn't been built yet. – SMBiggs Sep 11 '12 at 21:58
3

Check with the class files imports above and make sure that there is no import of android.R, if it has then remove it and then press Ctrl + Shift + o and select the packagename.R import not the other one.

Agilanbu
  • 2,308
  • 1
  • 26
  • 29
Mayank Saini
  • 2,957
  • 22
  • 25
3

just clean and rebuilt your project it sometimes help

Make sure that there is no error in your res/drawable folder in android.While naming images in this folder don't give numbers and capital letters.

XYZ_deve
  • 477
  • 5
  • 6
3

I changed android:versionName in file AndroidManifest.xml from 1.0 to 1.1. That regenerated my R.java file, and I'm a happy camper. I hope this might help somebody.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
StrefanA
  • 9
  • 1
3

So I have run into this problem multiple times when switching build targets. Usually doing a Project >> Clean worked for me. This last time, however, it did not. Finally I tried to open my default.properties file, located under the root project folder. I received an error message stating that it was out of sync with the file system. I actually deleted it and copied a coworkers version which allowed eclipse to rebuild my R file. I will paste what it looks like below. It is named 'default.properties'.

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
# 
# This file must be checked in Version Control Systems.
# 
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-3
3

There are several reasons of R can't be resolved.

  1. Most of time this error occur for XML file fault. This error can be solved by Clean and rebuild your project, also you can restart your android studio.

  2. Sometime If you codeing some wrong thingin AndroidManifest, this error can be occur so Please check this.

  3. If your resources contain the same name or they have invalid characters, this error can be occur.

If you already check those and yet you are facing with same problem then go ahead:


This error can be occurs when Android Studio fails to generate the R.java file correctly, so you have to follow those steps:

Step 1: Safely delete the build folder from app directory.
Step 2: In Android Studio, File -> Invalidate Caches / Restart
Step 3: Build -> Clean Project Then Build -> Rebuild Project.

so build folder will be regenerated and hope so error is gone.


If Error still found then maybe you have performed refactoring the package name previously. This case you can press ALT + ENTER together for import the R.

Hasib Akter
  • 6,859
  • 2
  • 22
  • 34
3

Change build target from 1.5 or 1.6 to 2.2 (API version 8), and check if you have the % character in string.xml. If yes, replace with %%.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
BigUser
  • 1,847
  • 2
  • 14
  • 10
3

My problem was that I left out the = mark from the strings in strings.xml e.g. <string name"dialog_grl... It did not appear to me even when the graphical layout of my .xml file could not show the strings. After a project clean all files got messed up.

erdomester
  • 11,491
  • 31
  • 126
  • 226
2

For me, I removed "import android.R;" and it fixed all my problems. What a nightmare!!!

Siddharth
  • 8,842
  • 13
  • 79
  • 133
2

If cleaning the project doesn't work and if there is no problem with xml files(If it is a new project). Then go to Androidmanifest.xml,edit it and add some spaces between the lines, save it. Now clean the project, error will vanish.

This happens because of stale R.java file. When we edit android manifest file,R.java is created again.

Ryhot
  • 315
  • 3
  • 12
2

To resolve this issue please go to java Build path->order and Export and select all checkbox and select ok and then clean and build project and import your package name R file(Ex com.demo.R). It works.

Dutts
  • 5,097
  • 3
  • 34
  • 53
Brajendra Pandey
  • 2,160
  • 1
  • 14
  • 22
  • Could you please clarify this? I've fixed the spelling errors, but am still not 100% it makes sense. – Dutts Feb 18 '13 at 10:28
2

I solved it for myself with Window->Preferences->Android and SDK Location: path to adt\sdk e.g. C:\adt\sdk

kolaval
  • 151
  • 3
  • 9
2

I think the problem is in layout.xml file or in import statement.

Be sure that you have not imported R.android., instead of import R.YOUR PACKAGE NAME..

It will work fine.

Pratik Dasa
  • 7,103
  • 4
  • 26
  • 43
2

For me it was due to using 'private' android drawables:

[2015-11-26 18:59:48 - MyAndroidApp] 
/Users/myname/git/MyAndroidApp/res/menu/drawer_view.xml:24: 
error: Error: Resource is not public. (at 'icon' with value 
'@android:drawable/ic_menu_home').

The interesting thing is that this errors didn't show up in the Console until I removed the reference to the file using the icons (ugh!).

android:entries="@menu/drawer_view"

Now the solution was quite obvious, copying the icons into my project (res/drawable-XXXX/) and switching to local references.

@drawable/ic_menu_home

Hope this helps.

DevCat
  • 61
  • 2
  • 9
2

Recently, I faced a similar situation with R that could not be resolved.

  1. Project -> Clean (it usually works, but this time it didn't).
  2. I tried to remove the gen/ folder (which automatically gets built) - nope.
  3. Dummy modifying a file. Nope again. By now I was going grey!
  4. I checked in the AndroidManifest.xml file if appName was present. It was - checked!
  5. Looked again at my res/ folder...

And this is where it gets weird... It gives an error, in some but not all cases..

I added the formatted="false" flag:

<resources>
    <string name="blablah" formatted="false">
    </string>
</resources>

Bingo!

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
2

Wow. I resolved my R issue by tracking down all possible errors in all XML files and related resources. I know that is a handful of layers, but simply you must:

  • Make sure any referenced resource image is actually there
  • Everybody already mentioned so much that this is just echo... Remove all Eclipse injected R.* imports if need be.
  • I like to restart Eclipse when I do the above.
  • Make sure all XML files are properly structured. Like, just do your stuff. Hahaha
  • Go through as many lines as you can. Get in the habit of hitting Ctrl + Shift + S often to get compiler results quickly changed.

Hahah. Sure it is nice to get the R Cannot be resolved fixed though...

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Lenn Dolling
  • 1,232
  • 13
  • 22
1

My solution (on Eclipse Juno) was to click on "Fix Project Properities" by:

  1. Click right mouse button on project
  2. Choose Android Tools menu
  3. Click on Fix Project Properities

ps. First check if R library is not implemented and if all files in res/ folder are ok.

deadfish
  • 11,064
  • 11
  • 79
  • 125
1

Yet another reason this might happen: Tonight, I met the dreaded error, and eventually discovered an errant mouse drag apparently moved one of my own packages' classes into the hierarchy of another. There is truly no end to the number of ways you can screw yourself in the Eclipse environment.

Mike Fahy
  • 4,380
  • 3
  • 23
  • 26
1

From the many answers that were offered here, what helped me is to change the order at the "Order and Export" at the java build path.

1.click on you project -> project properties

2.Java Build Path

3.Order and Export

Change the order of the Build class path to be

1.(your-package-name)/src

2.Android (version number)

3.Android Dependencies

4.(your-package-name)/gen

oznus
  • 2,318
  • 2
  • 23
  • 17
1

just clean and rebuilt your project it sometimes help

Pramod Yadav
  • 2,166
  • 1
  • 23
  • 31
1

Sometimes it helps when you manually create R.java file just simple empty R.java.

Dawid Drozd
  • 9,709
  • 7
  • 50
  • 66
1

In my case this error was because i imported a image to the folder /res, and the image had a invalid name. Can't believe that i spend almost a hour with this problem until i realized that the image had a invalid name :/

Afaria
  • 403
  • 5
  • 13
1

I had the same issue, but couldn't find out what xml file caused the error.

Thanks to this post I installed build tools 18.1.1 instead of 19 and it showed me the defective file. It seems that version 19 doesn't show the actual error.

Hope it helps someone :-)

Community
  • 1
  • 1
amuttsch
  • 1,204
  • 11
  • 24
1

I had this problem many times and I found that some times "R wont resolve" accrue because:

  1. some bad named Resource have exist like: an image with name My imge.png
  2. or some error in Resources like error in xml files or Error in defining Resource in your forms that won't exist.

in these case you even can't import your own package R to your project. so Go to your resource folder and search for 2 above points.

this will work

Mahdi
  • 5,031
  • 5
  • 46
  • 96
1

Same thing happen to me , and i passed a day to fix this. really this is a stupid error. i am just shearing my experience, may be it would helpful to you . First look carefully when you type this setContentView(R.layout.activity_to_do_list); hear your starting point R.l then eclipse should open a suggestion for you look like thisenter image description here

you import the second one not layout-android.R . the second one is created in your project hear com.example.gridtest is the project package name . then focus your imported section in your code enter image description here

ok look this import com.example.gridtest.R; its important . If you already import this android.R then remove it. Thank you hope it works . ( you don't need to do this always but if you face this kinda problem then do this, hope it would help )

Tanim reja
  • 1,991
  • 1
  • 15
  • 22
1

Some answers suggest restarting the computer. I have found that restarting the adb server is enough -- this is particularly true if you've just updated the Android SDK.

Restart adb via a linux shell:

$ cd $ANDROID_SDK/platform-tools
$ ./adb kill-server
$ ./adb start-server

You may need to restart Eclipse so it can reconnect to the restarted adb server. You may need to do a clean/build if Eclipse doesn't compile projects automatically.

pestrella
  • 9,268
  • 3
  • 37
  • 44
1
  1. Close all files, clean project, restart Eclipse.

or

  1. R is an automatically generated class that holds the constants used to identify your resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to Project > Build all (and selecting "Build Automatically" while there as recommended by Josef). If that doesn't work than try making a new project
Vaishali Sutariya
  • 4,834
  • 28
  • 32
1

I solved my issue with this by putting the resource files directly in the directory specified by Android. I had mistakenly believed that R.raw could contain the entire file directory of my resources. All files must have R.raw as their direct parent directory.

cowlinator
  • 4,270
  • 4
  • 27
  • 41
1

Make sure that all your resources files doesn’t have upper case extension. I found out that I saved some images with upper case extension .PNG. I changed it to .png and everything got back to normal.

Mansour Fahad
  • 663
  • 2
  • 9
  • 21
1

It worked for me with Right Click>Android Tools>Fix project properties and some time aftr restarting Eclipse.

Praveen
  • 267
  • 1
  • 9
1

a better and more promising solution

  1. close Eclipse and open your Workspace directory.
  2. look for a folder named ".metadata" and delete that folder.
  3. Open Eclipse.

the Error should go away :)

Gueorgui Obregon
  • 4,688
  • 2
  • 29
  • 54
01wifi01
  • 385
  • 3
  • 15
1

If you are on a Mac, check if a .DS_Store has been added to a directory in /res (ls -al on the Terminal command line). Remove it and refresh/rebuild.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
mac user
  • 1
  • 1
1

When you have just opened your project, it takes a while for android studio to load things up. It happens a lot with me. I just sit back and watch a youtube video for 5 minutes while it is done loading.

By then the cant resolve symbol R is gone! :-)

If you have checked and re-checked everything mentioned above, made sure that the project has completed building, I'd suggest you to patiently wait... If not clean and rebuild the project.

Sayan Sil
  • 4,236
  • 1
  • 11
  • 26
1

Just Restart Your Project

File>>invalidate caches /Restart

Note:

  1. Android automatically creates a .R file not need to import
  2. Import android.R.* << R.java file auto created by android Studio
  3. Android studio has lots of caches hold and that's why some time it's occurred
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
Ashif
  • 178
  • 1
  • 9
1

1) First of all close all the file.
2) Go to the Build -> Clean Project.
3) Go to the Build -> Rebuild Project.

This works for me.

SecretAgentMan
  • 2,690
  • 6
  • 16
  • 35
MEGHA DOBARIYA
  • 1,130
  • 7
  • 4
  • I test your solution; but For me, Go to the Build -> Build Module "Module Name" did the trick and showed me the error in my Manifest file – Shamshirsaz.Navid Nov 30 '20 at 17:00
1

The real question is how to prevent the import android.R; from returning:

Window -> Prefs -> Java -> Editor -> Save Actions

Uncheck "Organize Imports."

That will do it.

Noah
  • 14,450
  • 11
  • 98
  • 147
0

Make sure if you're looking for R.drawable.* that the file exists in your drawable* folders.

Graham P Heath
  • 6,161
  • 3
  • 26
  • 44
0

Solution:

Project clean after Eclipse restart. They are auto generated, otherwise you are XML inner any component (like button) after Ctrl + Shift + O. It autogenerated the R file and then the error was solved.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Sanket
  • 469
  • 6
  • 8
0

One shoudn't try to import android.R. Try to fix all errors that appear in XML files, including the manifest file. When all errors are cleared, R.java will reappear.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
laph
  • 2,785
  • 2
  • 16
  • 18
0

In my case I was trying to convert my project into Maven. After while(and thousands of random errors which were saying NOTHING) I tried to undo all of operations. What I didn't notice .project file was changed and it wasn't visible inside Eclipse.

Only reverting .project file to before-maven version helped me fixing this error.

Piotr
  • 1,668
  • 1
  • 22
  • 38
0

For me it was because of wrong java compiler version. Project Properities -> Java Compiler, enable "Enable project specific settings" and choose your "compiler compliance level"

chek
  • 23
  • 2
  • 5
0

I was facing the same problem, I googled and tried suggestions I found but none of them worked for me. I changed my workspace and it worked, but before changing your workspace try other workarounds you have. Any one of them might work for you.

Chilledrat
  • 2,608
  • 3
  • 27
  • 37
surhidamatya
  • 1,603
  • 28
  • 53
0

This worked for me.

I had no 'import android.R' in the brand new project the eclipse wizard created. the default xml layout file was lower case as well.

Problem was eclipse was not generating R.java. No amount of cleaning/restarting was having any effect.

I commented the setContentView and getMenuInflator calls and the make succeeded after which Eclipse generated R.java.

Once R.java was generated, I was able to uncomment setContentView and getMenuInflator.

CF_Maintainer
  • 973
  • 2
  • 12
  • 30
0

i tried most method above , but not work my case

finally i found out the adb lost

for mac os , go to android sdk directory

type ./adb start-server

chings228
  • 1,789
  • 22
  • 23
  • i spent hours on that , so i tried to install android sdk manager. when i try to install package , i was warned my adb lost . so i connect adb again. after that the r.java problem disappear immediately – chings228 May 18 '13 at 04:02
  • It may have been a coincidence that it worked after that, but the underlying cause was not solved by restarting `adb`. This is something that deals with the way the project is built. It was more likely that you restarted Eclipse, cleaned the project, or did something else along the way that caused the R.java file to be regenerated. – jprofitt May 18 '13 at 04:04
  • i did all the thing u mentioned , restart , clean , rebuild , but none works for my case i just want to share my experience if ppl try all but not work – chings228 May 18 '13 at 04:05
0

Check that the library projects that you've imported into the app are all open.

And for some reason, if you haven't added anything to the activity, like onCreate(), it won't understand what R is.

Muz
  • 5,296
  • 3
  • 42
  • 57
0

I have got this same problem today. I've tried to perform all sdk update using SDK manager . After finishing and restarting IDE problem has been resolved.

0

There are two ways that R will an error

1) Problem with your layout file ie xml file

2) If you update adt then the R problem will occure. - you have to check properties for the project - goto->Properties ->Java Build Path -> Order and Export and check for all the lib .

It will solve the problem.

nilesh
  • 167
  • 1
  • 9
0

first of all look at your gen file if it contains your package name and R.java or no. if it was empty maybe you don't have permission to create new files so exit eclipse than right click, run as administrator and clean your project and everything will be correct.

otherwise you should have an error in your XML file maybe or check the other answers.

Chris Sim
  • 3,584
  • 3
  • 26
  • 32
0

I tried most of the options on this thread (and many others) and nothing worked for me. Finally this one worked so I thought to share it with everyone...

I have multiple versions of Eclipse IDE on my machine (because I also work on non Android related projects). Accidentally I created my Android project using a workspace that was being used by another (non-Android) version of Eclipse. Due to this I guess something weird was going on and my R.java was never getting generated!

To fix this, all I did is created a new workspace and magically R.java started getting generated and everything went smoothly...

So while you are trying all other options, give this one a shot too. And hope it'll work for you...

Gautam Tandon
  • 244
  • 2
  • 2
0

This works.

Step 1:

Download https://www.dropbox.com/s/7nwlrg05ke05tat/GalleryImageview.zip

Step 2:

Load eclipse and load the project > Run and Test will work on the fly

Step 3:

Update here if anything is wrong.

Enjoy. Happy coding.

0

I followed the advice to install the platform build tools. Then The message

<package>/gen already exists but is not a source folder. Convert to a source folder or rename it.

appeared in the console. So I followed the advice and set "gen" as a source folder. This fixed the issue.

Wolfgang Fahl
  • 12,097
  • 9
  • 75
  • 150
0

I would say "R can not be resolved" is something too general and can be misleading at times. I also had the same problem many times ago even when I first create a new project (which mean I did not mess with the code yet). Most of the time I just need to update my sdk manager (as some people have mentioned). But in my recent case, I found out that the path to my project file is too long (something like this E:\R&D\ANDROID\ANDROID BASIC\Folder 1\ Folder 2\..... \Folder n\MyProject). The folder names also contain spaces character. I thought it could be the case and indeed it's true, when I created a new project in another folder with shortest path possible, and none of the folders' names containing space character, it worked as normal.

Great Question
  • 2,084
  • 6
  • 29
  • 42
0

What worked for me is installing "Android SDK Build-tools". Open the Android SDK Manager and install the Android SDK build-tools. It also helped to update the Andriod Development Platform in Eclipse: Help > check for updates

Oke
  • 113
  • 8
Piyush Mattoo
  • 14,524
  • 6
  • 49
  • 58
0

Right click on your project and click Refresh Or hit F5

Mina Gabriel
  • 17,138
  • 23
  • 89
  • 118
0

Import R class with your package name. Do not import android R class.

Neha Agarwal
  • 630
  • 7
  • 23
0

reason R.java might not get auto generated is if you have directories like

res/drawable-hdpi, 
res/drawable-mdpi, 
res/drawable-ldpi.

image name should be in a proper way

Check the Image Name and check the and layout/any.xml and string.xml

dhiraj kakran
  • 459
  • 1
  • 6
  • 19
0

This is a very old thread, I had a new finding today. I have created an activity, I put it in another package(by mistake). In the newly created class, eclipse was not able to resolve R.

I tried all the ways mentioned in many places but I failed to notice that I put it in a different package. After struggling for few minutes I noticed that I put it in wrong package.

If you are not keeping your Activity in the activity package, you will get this error.

NewUser
  • 3,471
  • 7
  • 48
  • 74
0

If you work with eclipse: Sometimes eclipse supresses the error messages in the ressource files. Solution: Save a temporary ressource that has no errors in the layout folder. Build the project new. Then eclipse shows the correct error in the ressource file. Delete the temporary ressource.

SJX
  • 615
  • 8
  • 13
0

Also, if you have extra files like "desktop.ini" in your res folders that are added by the OS but are totally unnecessary, try deleting them. This apparently was one of the things blocking the generation of R.java in an imported project of mine.

Aaron Campbell
  • 500
  • 8
  • 18
0

Each time I had a problem with R not been generated, or even disappeared, this was due to some problem in the XML layout file that prevented the application from being built.

R is a generated class. If you are using the Android Development Tools (ADT) it is generated whenever the project is built. You may have 'Build Automatically' turned off.

Akshay Paliwal
  • 3,050
  • 2
  • 35
  • 42
0

R is auto generated resource in android project some times it happens don't exactly know why but have you try build automatically option from project

=>remove other import of R file of other project or android.r =>always remember if you always face problem with auto generate id or auto deleting R file you can generate static id like string file and use that with @id instead of @+id

Ajay Pandya
  • 2,319
  • 4
  • 28
  • 64
0

Oh God I am Linux (ubuntu) this is how I resolved it:

First thing the problem is permission problem I was not seeing any perission realted error but that was the problem. The tools were not have executable permission.

So just a a quick fix(If you know the exact file that need executable perission apply the ff command specifically to them.)

change to the dir you put your sdk, then:

sudo chmod +x -R .

After that goto project->clean->select your project->clean

That worked for me!.

dsharew
  • 9,287
  • 5
  • 40
  • 66
0

When running x64 Linux you may be missing some x32 build libs. Ex for Debian make sure you have libc6-i386 & zlib1g packages installed.

ArteQ
  • 79
  • 2
  • 4
0

I had the same problem. In my case had to do with the layout XML file names. I had some of the file names with uppercase letters in them:

screenQS.xml - didn't work!

screenqs.xml - worked!

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Arturski
  • 1,042
  • 3
  • 13
  • 24
0

Another way this can occur is if you start a new project from one of the samples. When you later decide to change the package name from com.example.android.foo to your own domain, you will need to modify several values in the manifest and in individual .java files.

If you're in Eclipse, find the package statement for the .java file and choose QuickFix. There may be several choices there, but the one you want is the one that indicates to "Move 'foo.java' to package 'com.youdomain.android.yourapp'. Save the file and it may autobuild or do as others have suggested and try "Project->Clean".

Eric Cloninger
  • 2,230
  • 2
  • 21
  • 26
0

My R file disappeared and wouldn't generate - the console reported this:

[2010-09-22 16:19:40 - myproject] res\drawable-ldpi\downloadNow.png: Invalid file name: must contain only [a-z0-9_.]

So it turned out that resource names cannot have capital letters - confirm that they are all small letters and contain . and _ only

sami
  • 700
  • 2
  • 8
  • 21
0

I faced the same problem after putting some graphics in the drawable folder. There was some error and when I tried to clean, the R.java file was not there.

So I carefully checked and found that there was some files with uppercase file names and some files are having the same name but different file extensions. I renamed those files and had a unique name in lowercase for every file. Remember Android cares about the name of the file, not the extension.

Maybe this can help to you or other readers.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
noob
  • 17,131
  • 18
  • 103
  • 168
0

Of the many possible problems that could cause this, this is what fixed it for me.

Right click on project -> properties -> Java Build path-> source.

Then add the gen and src folders. For some reason, Android wanted me to only have the overall folder.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
RasTheDestroyer
  • 1,622
  • 15
  • 21
0

I had tried all of the above with no prevail. It turned out PhoneGap was causing a conflict with the Android SDK. After uninstalling PhoneGap, the Resources file started regenerating again. Another project, needed PhoneGap so I re-installed, and once again, the Android project (a different project) stopped auto-generating the R file - Build Automatically was checked, did the Clean, and restarted Eclipse - no dice. I removed PhoneGap and it was working once again. This was PhoneGap v 1.5 with the MDS 1.1 plugin for Eclipse.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
-1

This could be an issue caused by manually renaming the package in the AndroidManifest.xml

An alternative is to use the built in tool to rename the package, this will take care of most of the problems avoiding the R not found issue.

Right Click on the project
Click on Android Tools
Click on Rename Application Project
Enter the new package name

Then a pop-up will appear asking for changes to be applied, including the R workaround.

Source: Rename package name in android

Community
  • 1
  • 1
warmth
  • 457
  • 6
  • 10
-1

To resolve R

  1. Make your all XML file correct

and then Clean Project in Build menu

This is the simplest way to do it

Hope it hepls.

Rajat Mittal
  • 342
  • 3
  • 18
-2

I'm guessing you just copy and pasted the codes, or not. Either way check your AndroidManifest.xml file, see if the package name is correct.

Chilledrat
  • 2,608
  • 3
  • 27
  • 37
Lendl Leyba
  • 2,214
  • 3
  • 29
  • 47
-3

Check the content of your XML file. If layout_width or layout_height contain the match_parent element, then change it to fill_parent. It solved the problem for me.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
neeraj t
  • 4,354
  • 2
  • 24
  • 29
-4

Press Alt+enter and Import

or

Ctrl+F9

and solved.

Paul Roub
  • 35,100
  • 27
  • 72
  • 83
Shubham
  • 1,105
  • 1
  • 11
  • 15