Android Studio cannot resolve R in imported project?

105,341

Solution 1

  1. Press F4 into Project Structure, Check SDKs on left
  2. Click Modules ---> Source Tab, check gen and src as sources

PS: The answer over a year old and the menus have changed.

Solution 2

File -> invalidate caches

then

Restart application

Solution 3

None of the answers on the web so far have helped. Build > Make Project did not work for me. For me it was as simple as choosing this other option (on Android Studio 1.3.1):

Build > Make Module 'module name'

Solution 4

In my case I had an Activity file imported from Eclipse that had the line:

import android.R;

So all of my R classes were resolving to the SDK, as soon as I commented out that line everything compiled correctly to my package. I only noticed the issue when I was moving the project from my Mac to my Windows machine.

Solution 5

This issue starting occurring for me when I started working with build.gradle to incorporate the data necessary (signingConfigs) to build a signed .apk. After what appeared to be a successful build from the command line, I discovered that going into any class using R.* that all R.* references were unresolved.

The answers here didn't help me. I decided to re-import the project and it magically fixed the issue. File/Import Project... and select the build.gradle file in my apps root folder. I would love to know why re-import fixed this :)

Share:
105,341

Related videos on Youtube

joe_deniable
Author by

joe_deniable

Updated on July 09, 2022

Comments

  • joe_deniable
    joe_deniable almost 2 years

    I'm trying the new Android Studio. I've exported a project from eclipse, using the build gradle option. I've then imported it in Android Studio. The R.java file under gen has a j in a little red circle on it. And in my source files, I get "cannot resolve symbol R" wherever I have a reference to a resource, e.g. "R.layout.account_list" etc.

    I have never used Intellij before. Would appreciate any help as there obviously aren't many answer yet about Android Studio. Thanks!

    • Blackbelt
      Blackbelt almost 11 years
      Click on Build -> MAke Project
    • joe_deniable
      joe_deniable almost 11 years
      I had tried that but got an error saying "The specified gradle installation is not supported by this tooling API version". The gradle version being used, which I had to specify when importing the project, is in the "plugins" folder of the Android Studio installation folder. (It's version 1.6). Another issue is that when I quit and restart Android Studio, and it reopens the imported project, it says "The modules below have been removed from the gradle structure", and lists my main module (the name of my app).
    • joe_deniable
      joe_deniable almost 11 years
      I've tried using gradle 1.5 and it gets a bit further but still fails with this error: java.lang.ClassNotFoundException: org.gradle.tooling.provider.model.ToolingModelBuilderRegistr‌​y
    • ADK
      ADK almost 11 years
      Maybe you should to use Build -> Rebuild project
    • joe_deniable
      joe_deniable almost 11 years
      Thanks, but that still causes the same errors. With gradle 1.6 the error is that the Gradle installation is not supported, with gradle 1.5 and 1.4 I get the classnotfoundexception. Maybe I'll try 1.3...
    • Benoit Duffez
      Benoit Duffez almost 11 years
      Please, star this issue if you stumbled upon this from the upgrade of Android Studio from 0.1.5 to 0.1.6
  • joe_deniable
    joe_deniable almost 11 years
    Thank you very much, that has fixed the R not resolved problem. I will maybe ask another question about the gradle issues.
  • Cartesian Theater
    Cartesian Theater almost 11 years
    Is there a way to do this manually? After upgrading I got the message "We will provide a UI to configure project settings later. Until then, please manually edit your build.gradle file(s.)" Also, for Mac it's command semicolon instead of F4.
  • takecare
    takecare almost 11 years
    This didn't do it for me (these folders are not present). I have the same problem, but the project was created from scratch in Android Studio, and it stopped working for no reason.
  • Moberg
    Moberg almost 11 years
    What do you mean by "Scheck SDKs on left"? Also, where can I find the gen and src folder?
  • Matt Clark
    Matt Clark almost 11 years
    Please do not post Me-Too responses as answers.
  • Ilya Kogan
    Ilya Kogan almost 11 years
    Actually this was very helpful, as the "We will provide a UI later" message scared me off, and I almost gave up until I read Tim's answer.
  • Ilya Kogan
    Ilya Kogan almost 11 years
    As Tim said, don't get scared by the "We will provide a UI to configure project settings later" message. This solution still works in Android Studio 1.7.0.
  • Andrew Barber
    Andrew Barber almost 11 years
    When you copy content from somewhere else, please be sure to indicate where it came from. I've added that for you here.
  • speedynomads
    speedynomads almost 11 years
    When I go to Modules > Sources, I can see a gen folder but not a src folder. When I run 'Rebuild Project' my project compiles fine. Any ideas?
  • UmAnusorn
    UmAnusorn over 10 years
    I can see a src folder but not a gen folder. What should I do?
  • HBG
    HBG over 10 years
    Sorry to sound overly pedantic but those instructions are quite difficult to follow. What does F4 'into' Project Structure mean? You've said to check SDKs on the left but there's no checkbox. Under modules there is a 'Sources' tab and nowhere to 'check' gen and src and sources. Please elaborate.
  • Crossle Song
    Crossle Song over 10 years
    @Horace you can into Project Structure from File menu. you can check gen or src use right key menu.
  • alicanbatur
    alicanbatur almost 10 years
    There is nothing like SDKs on the left.
  • Axoren
    Axoren almost 10 years
    This answer is no longer relevant due to the time difference. It's over a year old and the menus have changed.
  • Crossle Song
    Crossle Song almost 10 years
    @Axoren Yes, Android Studio 0.6.1 now.
  • Fabian Fagerholm
    Fabian Fagerholm over 9 years
    Could you give some more detail? For example, how to identify which library is the extra one.
  • alap
    alap over 9 years
    What did you do in the Mac/Windows case?
  • Eliasz Kubala
    Eliasz Kubala about 9 years
    Hey bro! It's help me. Thanks ;)
  • Codebeat
    Codebeat almost 9 years
    This is wrong because you cannot access R.layout.<your app layout> . Import com.app.<projectname>.R instead (path can be different for your project).
  • Riot Goes Woof
    Riot Goes Woof over 8 years
    Odd that "Make Project" doesn't do what this does. Just another voodoo magic operation that works because "it works".
  • GaryAmundson
    GaryAmundson over 7 years
    Almost 2 hours of beating my head on my desk over this... so thanks! But why doesn't a Clean or a Build solve this??? aargh.