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!

30条回答
倾城 Initia
2楼-- · 2019-01-07 10:50

This is probably due to a failed resource build

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

查看更多
Evening l夕情丶
3楼-- · 2019-01-07 10:52

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 {
}
查看更多
叼着烟拽天下
4楼-- · 2019-01-07 10:52

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

查看更多
放我归山
5楼-- · 2019-01-07 10:52

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.

查看更多
冷血范
6楼-- · 2019-01-07 10:52

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

查看更多
再贱就再见
7楼-- · 2019-01-07 10:53

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.

查看更多
登录 后发表回答