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:42

File -> invalidate caches

then

Restart application

查看更多
对你真心纯属浪费
3楼-- · 2019-01-07 10:42

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

查看更多
家丑人穷心不美
4楼-- · 2019-01-07 10:46

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'

查看更多
唯我独甜
5楼-- · 2019-01-07 10:46

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.

查看更多
唯我独甜
6楼-- · 2019-01-07 10:46

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.

查看更多
姐就是有狂的资本
7楼-- · 2019-01-07 10:48

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.

查看更多
登录 后发表回答