In every project I've tried to create in Android Studio, all usages of R are marked in red with the error message "cannot resolve symbol R", but the compilation succeeds and the application runs. This is really annoying, as it blocks auto-completion and shows huge red waved lines all over my code.
I'm running Android Studio 1.7.0 and creating the project with default settings. A screenshot is attached:
This is my build.gradle
:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
This is how the Project Structure looks like:
Any idea how to fix this?
Just Click on Build -> Rebuild Project option in your Android Studio.
This error happens to me because there is an invalid input in my activity_main.xml file.
When you try to build or clean your project you will see error messages.
I resolved mine by reading those error messages, correct what is wrong in my xml file. then rebuild project.
I get bitten by this every now and then and there's another thing to try: match your compileSdk and buildSdk versions to the latest available.
I had changed my compileSdkVersion from 21 back to 19 because the Android Studio (1.1 canary channel) debugger was not tracing properly into prebuilt libraries. I found that it seemed to be tracing based on the buildToolsVersion (19.1.0), which I verified by clicking an error in the stack trace (and then choosing 19 instead of 21). Though I changed it directly in the build.gradle file, a graphical walkthrough is here: https://www.blogger.com/comment.g?blogID=4722727826918942842&postID=3875998513888772255 and I'll summarize:
Confirm, and the project should automatically sync properly.
The lesson I'm getting from this is that they should both be the same.
If you are getting this error, then clean your project.
The reason behind this is that by cleaning your previous
.apk
file that is present in your android studio project folder on your system is deleted.Make sure in your
AndroidManifest.xml
the package name is correct. That fixed the problem for me when myR.whatever
was marked red!I would also recommend checking all your
gradle
files to make sure those package names are correct. That shouldn't make everything red, but it will stop yourgradle
files from syncing properly.I ran into the same error, turns out that the manifest file had some errors. After fixing it, clicked Make Project again, it worked fine. Android studio sometimes do not give very helpful error messages.