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条回答
2楼-- · 2019-01-07 10:59

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.

查看更多
太酷不给撩
3楼-- · 2019-01-07 11:02

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.

查看更多
在下西门庆
4楼-- · 2019-01-07 11:02

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.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-07 11:05

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楼-- · 2019-01-07 11:05

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.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-07 11:06

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.

查看更多
登录 后发表回答