27

I import a project to Android Studio , but the R.java is always empty.

public final class R {
}

I have tried:

  1. Make project,make module, remake, etc.
  2. Project Setting-modules-sources, then add src and gen as my source folder.

But they are not take effect.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Marshal Chen
  • 1,985
  • 4
  • 24
  • 35

24 Answers24

16

Goto File -> Settings -> Compiler now check use external build

then rebuild project

Kuluval
  • 262
  • 2
  • 11
  • I solved this issue doing this as well. I had too many classes with this error. After disabling external build and removing the wrong R imports I had (apparently the cause of my problem), the correct R class was then generated, and I could finally import it in my classes. After everything was fine, I re-checked external builds. Since many people are having this problem, I think I should provide specific details about how that solution worked for me. – davidcesarino Jul 23 '13 at 15:47
  • 19
    I don't have **use external build** option. My Android Studio version is 0.8.9 – elvenbyte Oct 23 '14 at 18:33
  • 9
    Seems answer outdated. There is no such option nor in 13.1.4 nor in 14 – ruX Nov 11 '14 at 22:59
  • 3
    Just use the 'Rebuild Project'from the menu item 'Build' – Jashan PJ Mar 23 '15 at 20:08
  • Rebuild Project from menu item Build solved my problem –  May 08 '15 at 12:22
  • Only rebuilding didn't work for me. I had to sync the build dir first then rebuild and it worked. – Kalem Sep 29 '15 at 11:48
9

Best Solution for android studio Users :

Goto : Menu tab - > Build - > Rebuild Project

after rebuild Proj. your R.Java file problem will be solve automatically ..

Vijay Patil
  • 115
  • 1
  • 2
5

I managed to regenerate R: File->Settings->Compiler

then UNCHECK "Use in-process build"

Rebuild Project

Carlos Gómez
  • 357
  • 4
  • 9
4

For me the problem with the R class has been related to automatically organized imports.

As AndroidStudio has the option to allow the automatic imports, it took the wrong R class and thats why the ids have not been found and Gradle was throwing errors!

So check if the import to the R class is referencing to the one you need.

stan
  • 229
  • 3
  • 10
  • How do you do that? Thanks! – Ricardo May 13 '15 at 15:43
  • 1
    @Ricardo take a look at import statements at the top of the class you get error from. If you see import .R and path is something you don't recognize, chances are you are importing wrong R – vanomart Feb 24 '16 at 10:25
4

When I was trying to add another flavour I face same situation.

I found in one of post and I gave successful try and it work. Following I did: File -> Invalidate Cached/Restart.

CoDe
  • 11,056
  • 14
  • 90
  • 197
3

As for me, the problem was in this superfluous line:

import android.R;

Cfr
  • 5,092
  • 1
  • 33
  • 50
3

Had the same problem and solved it by:

  1. running / building the project (it was deployed to the device, in my case)
  2. right-clicking on the Project/Module/build folder
  3. choose: Synchronize 'build' - then i found

Project/Module/build/source/r/debug/package/R.java

Probably it was even there before the project was build, but I didn't test that.

I hope this was helpful, even though the answer comes a bit late and by now the bug with the

Settings->Compiler->[ ] Use external build

should be fixed afaik ;-)

Christian R.
  • 1,528
  • 9
  • 16
2

So, this is the latest solution if anyone get's stuck like I did today especially, for R.Java file.

If you have lost the count of:

  • Clean Project
  • Rebuild Project
  • Invalidate Caches / Restart
  • deleted .gradle folder
  • deleted .idea folder
  • deleted app/build/generated folder
  • checked your xml files
  • checked your drawables and strings

then try this:

check your classpath dependency in your Project Gradle Scripts and if it's, this:

classpath 'com.android.tools.build:gradle:3.3.2'

then downgrade it to, this:

classpath 'com.android.tools.build:gradle:3.2.1'
Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37
  • 1
    but why? i mean it works, but whats the reason that a newer version essentially breaks. – Emile May 15 '19 at 15:37
  • Not quite sure @Emile but more stabler version may be one of the reason to answer this, also sometimes while updating dependencies such things might have been overlooked. – Rohit Sharma May 16 '19 at 17:02
1

If you are facing this problem in a specific module in your project, you could try opening just that module as a project and then build it. This worked for me. It was failing to generate the R file for the module when I was trying to re-build the entire project.

ben_joseph
  • 1,660
  • 1
  • 19
  • 24
1

Check your project package! I had brought some classes over from another project and did not notice the subtle change in the package name. See top of your AndroidManifest.xml

tom
  • 2,190
  • 1
  • 23
  • 27
1
File->Invalide Cahces / Restart
Kusan
  • 260
  • 1
  • 11
0

Did you follow steps here? First update your Eclipse ADT plugin, then export project and the import in Android Studio.

http://developer.android.com/sdk/installing/migrate.html

fasteque
  • 4,309
  • 8
  • 38
  • 50
0

I had a lot of problems exporting a project from Eclipse (assuming that is where you are exporting from). It was much easier for me to create a new project in Android Studio, then copy over the java files, xml files, layout directories, etc, and the AndroidManifest, into the appropriate location in the new project.

If you already have an R.java file for your project, you could try just copying it over to the new project. In Android Studio the R.java file seems to reside in build/source/r/debug/your-package-name/

joe_deniable
  • 2,562
  • 3
  • 28
  • 38
0

I found my solution here. In short make sure not only the Eclipse plugin(if you are using eclipse) is updated. Also ensure that the Android SDK Tools, the SDK platform-tools and the SDK Build-tools are updated. After this restart your machine.

Community
  • 1
  • 1
Dave Agaba
  • 213
  • 1
  • 4
  • 13
0

I failed even if I tried to update the SDK tools as mentioned here.

Here is how I solved the problem (for the library projects imported).

In File -> Project Structure -> Modules, I removed the problematic projects, and added them again by selecting New Module -> Android Library Module.

Select "Android Library Module" when adding project.

After rebuilding the project, I got all the R.java files back.

Community
  • 1
  • 1
Yi H.
  • 299
  • 5
  • 14
  • I think you also can go in the "Facets" section, then click "Add", choose "Android" and apply it to your library modules. Then go on the "modules" node and select "Library module". Could be quicker than reimporting everything (but maybe was not available in previous releases of android studio) – Christophe Fondacci Dec 12 '13 at 00:36
0

I will answer the question even though it has been a long time since it was thrown, just in case someone else get to it.

Tested on Android Studio ONLY (but I guess it could work for Eclipse as well) :

Check your build/source/r folder. In there, you should find some directories labelled under the name of your gradle build name (default : debug). Verify that the name of the package associated with R is the one you want.

I know this trick solves the problem of switching namespace, because Android Studio (or Gradle I don't know who is responsible for that) seems not to regenerate it in that case.

I haven't tried it when importing a project from Eclipse though.

Nabil Gardon
  • 175
  • 1
  • 1
0

Go to File → Settings → Editor → Auto Import and at java tab you should have:

Insert imports on paste: ASK.
If you have import for ALL, then Android Studio import other file.

grg
  • 5,023
  • 3
  • 34
  • 50
0

Just use the 'Rebuild Project' from the 'Build' menu from the top menu bar.

Jashan PJ
  • 4,177
  • 4
  • 27
  • 41
0

I had a similar problem in a large multi-module project with many dependencies among the modules. What worked for me, was to attempt to build separately from command line all the modules that failed to build within Android Studio. That gave me indications on resources missing in each project. From the project level in my console I did:

$ cd moduleName
$ ../gradlew assembleDebug

This provided me with a number of 'No resource found that matches the given name' errors, that weren't shown before, when I build the project as a whole.

ohaleck
  • 661
  • 4
  • 20
  • after doing this, I could finally import R again. Thank you!! It has something to do with multi-module-project. My lib-module was corrupted. – Tom Bevelander May 17 '16 at 21:47
0

I had a similar problem within my project using Android Studio 1.5.1. For me, bulding the project from command line by using following command resolved the problem:

gradle assembleRelease
bogumil
  • 381
  • 1
  • 5
  • 12
0

In my case I had an empty line prior a drawable definition in xml. This was braking aapt essentially not allowing to generate R.java .

foo
  • 574
  • 8
  • 13
0

I had this problem after update gradle...

There are several reasons that causes the projectenter image description here not to be built:

1-Unknown error in drawable or xml files

2-Update gradle or libraries and etc ...

Solution :

1-Clean and rebuild project

2-Delete .idea and build folders in project file(shown in picture) then goto "File/Invalidate catch-restart"

3-Roll back to previous gradle version and libraries.

Hamed Jaliliani
  • 2,789
  • 24
  • 31
Maryam Azhdari
  • 1,161
  • 11
  • 8
0

This is an edge-case:

I rename my module name and I forget to rename the namespace in my build.gradle file so when I tried to build the proyect the R class never was created.

I fixed it renaming the namespace and the problem was solved.

LordCommanDev
  • 922
  • 12
  • 38
-1

Go to Menu-Tab -> Project -> Build -> Automatically(check this option). and than reopen a new project.

Mystery
  • 552
  • 6
  • 14