Android Studio cannot resolve R in imported projec

2019-01-07 10:16发布

问题:

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!

回答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.



回答2:

File -> invalidate caches

then

Restart application



回答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'



回答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.



回答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 :)



回答6:

This is probably due to a failed resource build

Once the issue is fixed, a mere Build > Rebuild Project will do the trick



回答7:

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

then rebuild project



回答8:

I've been frustrated on many occasions with this problem, especially when intergating a project library as part of another project. In the most recent occurrence of this plagued problem, my Android lib project was using a WebView. It turns out that Gradle builds the R.class file in a sub directory called "web" whose path does not have anything to do with my source code path. When I imported this, my problem was gone. What have I learned? In the future, simply do a file search for R.class and note the path where it is located. Then import the package into your code using that path. It really sucks that you have to manually perform this. Gradle should automatically import packages it generates.



回答9:

At first check if there is import android.R; in top of your class this happens when auto import in enable in android studio.

after that comment all lines of your code that you use R variable (you can comment entire class or ...) then run application it regenerates R variable and you can uncomment your code.



回答10:

Here's what worked for me in IntelliJ (Not Studio), in addition to the replies presented above:

You need to attach an Android facet to an existing Android module. To do this, select the module from the module list in 'Module Settings', hit the '+' button on top and select 'Android'. See https://www.jetbrains.com/idea/webhelp/enabling-android-support.html.



回答11:

The solution is to open project structure window. [by press command+; on mac ox s. i don't what's the key shortcut for other platforms. you should be able to find it under "File" menu.] and click "Modules" under "Project settings" section, then your project is revealed, finally mark the generated R.java as Sources.

I am using Intellij idea 14.0 CE. The generated R.java is located at build/generated/source/r/debug/com/example/xxx

Really not easy to find for the first time.



回答12:

Follow the five steps below:

  1. Step 1 ===>Delete(CTRL X) the additional tag(s) in XML manifest file.
  2. Step 2 ===>Open Build-->Clean project.
  3. Step 3 ===>Open XML manifest file and past the last additional tag(s) like: Tag Manifest file example: (<uses-permission android:name="android.permission.VIBRATE"></uses-permission>)
  4. Step 4 ===>Step 2 again.
  5. Step 5 ===> Open Tool-->Android-->Sync project with 'Gradle'.

I have had the same problem but after all the steps it's work for me.



回答13:

On IntelliJ 14

Check if the generated sources are excluded. Usually in Project Structure -> [module] -> Paths -> Compiler output: "Inherit compile output path"



回答14:

My problem was that I had file 'default.jpg' in drawable folder and for some reason every resource was not resolved. Fixed after deleting that file!



回答15:

I was also facing the same issue and I went through lot of answer on stackoverflow. Then i found one solution which help me resolve this issue.

Check package name in AndroidManifest.xml file. I forgot to change it while copy pasting another project into new project.



回答16:

You should check all your files if it don't have errors and again rebuild your project in:

  • Build > Rebuild Project.

Or in:

  • File > Settings > Build, Execution, Deployment > Compiler

Check use external build and after rebuild the project.



回答17:

Regarding the following, from Crossle Song's answer

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

Despite the message "We will provide a UI to configure project settings later....etc" this worked for me.



回答18:

None of these answers helped me!

My problem was not a problem! The program could compile completely and run on the device but the IDE has given me an annoying syntax error. It has underlined the lines of codes that included "R.".

The way that I could solve this issue:

I just added these three classes in "myapp/gen/com.example.app/" folder:

BuildConfig

package com.example.app;
public final class BuildConfig {
    public final static boolean DEBUG = true;
}

Manifest

package com.example.app;
public final class Manifest {
}

R

package com.example.app;
public final class R {
}


回答19:

I solve that problem setting JAVA_HOME, CLASSPATH.

People sometimes skip JAVA_HOME and CLASSPATH when setting Java_path.

So try to check Environment Variable.

CLASSPATH -> .;

JAVA_HOME -> C:\Program Files\Java\jdk(version) or where Java installed

and then check the xml file which can have error(Do not naming well-known things such as button,text etc)



回答20:

In my case (Linux, Android Studio 0.8.6 ) the following helps :

File > Project Structure > Modules > select main module > select its facet > Generated Sources

change value of "Directory for generated files:"

from

MY_PATH/.idea/gen

to

MY_PATH/gen

Without that code is compiled, apk is build and run successfully

but Android Studio editor highlights mypackage.R.anything as "cannot resolve" in all sub-packages classes



回答21:

I had the same problem and got it fixed by deleting an extra library.

To do try this solution go to File > Project Structure (on a mac you can use the command "Apple ;")

Then select app on the left tab. Go to the dependencies tab and delete the extra library.



回答22:

check the build tools version in build.gradle(for module app). Then go to android sdk manager see if the version is installed if not install it or change the build tools version to the one which is installed like below.

    android {
          buildToolsVersion "22.0.1"
          ..
          ..
          ..
     }


回答23:

The solution posted by https://stackoverflow.com/users/1373278/hoss above worked for me. I am reproducing it here because I cannot yet comment on hoss' post:

Remove import android.R; from your activity file in question.

My setup:

Android Studio 1.2.2

Exported project from one Mac to Git (everything was working). I then imported the project on another Mac from Git. Thats when it stopped resolving the resource files.

I tried everything on this thread:

  1. invalidating cache and restart
  2. Delete .iml files and .idea folder and reimport project
  3. Clean and Rebuild project

Nothing worked except removing import android.R; from my activity java file.

To avoid this issue in the future add .idea folder to your .gitignore file. There is a nice plugin from git for gitignore in Android Studio. Install this plugin and right click in .idea > Add to .gitignore



回答24:

just clean project and then sync your project with gradle file.



回答25:

In my case, I found that "android.R.layout.account_list" was referenced instead of "R.layout.account_list". Simply remove "android." can fix the problem. Hope this help because sometimes you may overlook when you are too focusing.



回答26:

In my case:

I had to Copy Reference the R file; i.e. right click gen/<package>/R and Copy Reference. Then paste that over the R in your code where it fails to resolve.

That solved it for me (after trying everything else here). Still not sure why it worked to be honest.



回答27:

I had this same issue. The package name was wrong in two of the following three files. I manually updated the package name and everything started working again.

gen/com/example/app/BuildConfig.java

gen/com/example/app/Manifest.java

gen/com/example/app/R.java

I'm not sure what caused them to change as I've never modified these files before.



回答28:

Had this error when importing project from Eclipse. The reason was some of the xml files had errors. Check ALL your XML files for errors. Including androidmanifest.xml, and values, themes, styles, layouts folders, etc. Warnings in xml are ok, but errors will generate this error.

Afterwards, do a Clean Project and rebuild.



回答29:

For me, with Android Studio 1.5.1, the solution was to recreate the whole project with a slightly different name.

I think it didn't handle the app name "Kommentator_AS", because several places the package was named"Kommentator" instead.



回答30:

I hit this issue with intellij 2016.1 because I had set the generated sources to go to a directory that wasn't in the base project directory.

For example, in the Android facet, under "Generated Sources" I had:

Directory for generated files: <project dir>/target/intellij-gen

When I changed this to <project dir>/gen the problem went away.

Likewise when I then changed it to <project dir>/gen2 the problem also went away - but I had to do a deep clean to get it to compile again by doing:

rm -rf ~/.IdeaIC2016.1/system/compile-server/<project>_<hash>

and deleting all the generated directories and target dir.