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?
This worked for me:
Close and reopen project
Press " Sync Project with Gradle file " (is next to AVD manager icon in top menu) repeat 1-2 if Gradle could not Sync for first time. (mine worked in second time).
Worked with Android Studio 1.2.2
You should now see the "MyAppProject/MyApp/build/source" folder. This should contain the "r" folder (where the R.java file is generated). Here, you should either see "debug", "release" in blue (if you have a projectFlavour in your build.gradle, these will be under the "" folder). Within that debug/release folder should be your app's package name and then an R.java file that contains all your resource ids.
I resolved it by :
1) Sync Project with gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project
4) File -> Invalidate caches
//imp step
5) Check your xml files properly.
I had similar issue with 'com.android.tools.build:gradle:3.3.1', I tried all above solutions nothing worked. It was actually build:gradle 3.3.1 issue. I changed it to 3.2.1 then it worked.
So it can be issue with build:gradle version.