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?
For some reasons, Android studio use different configs for the editor and for the compiler. If it works for the compiler then it's good. If it doesn't work for the editor. (it sees unresolved files).
You need to add some directories to the source of your project. For example all resources should be marked as "source".
File->Project Structure
Select "Modules", then your project. And select the
sources
tab. On the right find your resources directory and click on the blue "source" button. Close all and it should work.Also, you'll have to make sure that
build/source/r/debug
is also a source. In I have all mybuild/source/*/debug
marked as source.main module .iml
project iml
Black magic
Not sure about this one, but I heard it working while I'm pretty sure it shouldn't change anything. Try compiling and saving the project after commenting all lines that requires
R
. Then when everything is not red. Try adding them back. The strange this is that your build/source doesn't get generated.Also this question suggest checking "compiler use external build"
Android Studio don't generate R.java for my import project
Side note
Also make sure that in your java code there is no.
import android.r;
from what you shown, everything seems fine. Just strange that the build/source isn't being created. For example, I have no build/apk. May be you're in release mode and it doesn't create those directories.If neither works, check all your xml files. This error occurs when your xml file is incorrect.
The solution is simple:
Go to
File > Project Structure > app
and choose theFlavours
tab, selectAPI 21: Android 5.0 (Lollipop)
in the drop down list forMin Sdk Version
.I had this bug few days ago, I tried looking around until I decided to re-install Android Studio. It worked for me.
It has also worked for me by doing manually cleaning the folder .idea/libraries and clicking afterward on "Sync Project with Gradle Files" solves the problem. Apparently this Sync does not remove elements, but keep the old ones and add the current ones. Probably a bug to report :)
you need to IMPORT your project R file