Cannot Resolve symbol R

馋奶兔 提交于 2019-12-01 17:58:41

Below some procedures are mentioned :

1) Try Build -> Clean/Rebuild project .

2) Manually typing import <package name>.R, If in import R is missing.

3) complete all resources of its corresponding XML file .

I hope any one of them would be helpful for you as there is no Particular way to resolve this issue.

JasonYang

If in your case in imports R is missing you Have to import R manually.

import <packagename>.R;

Replace <packagename> with your own package name.

In your code, R was not imported, therefore your Android Studio might got confused to find resources in your package.

I know others have said most of these points, but this should serve as a sum-up and hopefully bring up some new solutions!

If only the letter 'R' is red, it means there is something wrong with your R file (found at build\generated\source\r\debug\com\domain.projectname\R). Don't go around editing your R file- because each time the project is cleaned and built/rebuilt the R file regenerates (it completely resets without fail). There are a few possible solutions:

1) You might have written import android.R; at the top of your Activity. You need to have a different R file imported. This means if your domain is "example" and your project name is "project" then your import should be: import com.example.project.R

2) R is a build file. That means it disappears when you clean and generates anew when you build, as previously said above. Make sure you build or rebuild your project after cleaning it.

3) Maybe your gradle project sync has not completed. Wait until all processes have finished running!

4) It is also possible your R file is corrupt or missing. If the above don't work, you can come to this conclusion. It has a very easy fix, because as I explained earlier when you rebuild a new R file is generated. So just navigate to Build and click Clean project, then when that's finished, click Rebuild project. With the new R file your problems should be gone.

P.S: It's nearly impossible to give an accurate answer without all relevant code provided

The thing that finally did the trick for me was looking at the "event log" tab in Android Studio where I saw:

Frameworks detected: Android framework is detected in the project Configure

clicking on "Configure" set the AndroidManifest.xml as configuration-file of the project (identified it as "android") and only then all the "R" errors went away and I was able to compile & run.

Before doing that - all the actions I've tried such as: "clean", "rebuild" and restarting Android studios - didn't work.

It happens if your class is missing the link of R.Class which you may have to import. So, Before starting the class you can simply start by

import Your_Package_name.R;

E.g.

import com.example.myproject.R;

You can manually write this when Clean/Rebuild project is not the solution.

Use-case:

import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import com.example.jsonwebservice.R;

This worked for me.

Karue Benson Karue

It seems like Gradle is taking too long time to execute for your computer. Wait for the Gradle to finish building the project.

Kudehinbu Oluwaponle

I resolved it by editing my build.gradle file.

The compiledSdkVersion must align with your appcompat dependency.

My compileSdkVersion was 22 so I edited my appcompat dependency to

compile 'com.android.support:appcompat-v7:22.2.1'

So, I hope this can be a solution.

Just in case any of the above answers don't work, the following solved the issue in my case...

Here's what I tried:

  • Multiple "cleans", "rebuilds" and "invalidate caches restart"

  • Attempts to manually add [my package].R,

This didn't work, so I uninstalled Android Studio, and reinstalled the latest version.

The accepted answer to the above link has the following steps (which I reproduce here):

  1. Uninstall from installer. (via control panel for windows)
  2. Remove Android Studio files

    Go to your user folder (%USERPROFILE%), and delete .android, .AndroidStudio and any analogous directories with versions on the end, i.e. .AndroidStudio1.2, as well as .gradle and .m2 if they exist. Then go to %APPDATA% and delete the JetBrains directory. Finally, go to C:\Program Files and delete the Android directory.

  3. Remove the SDK

    To delete any remains of the SDK, go to %LOCALAPPDATA% and delete the Android directory.

  4. Delete Android Studio Projects

    Android Studio creates projects in a folder %USERPROFILE%\AndroidStudioProjects, which you may want to delete.

I skipped 4), then uninstalled a separate Java 7 installation I had, for good measure.

Next I downloaded the latest Android Studio (3.3.2) and installed it.

A bit drastic perhaps, but now everything is working and the "Cannot resolve symbol R" error is gone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!