In every instance in all of my classes where I reference R.id.something, the R is in red and it says "cannot resolve symbol R". Also every time there is R.layout.something it is underlined in red and says "cannot resolve method setContentView(?)". The project always builds fine. It is annoying to see this all the time. I have read many other questions on here about something similar but most involved importing projects from Eclipse. I am using what I believe to be the most recent version of Android Studio and the project was created with Android Studio and worked without any "cannot resolve R" problems. I would like to know what causes this if anyone knows.


当前回答

我现在也有类似的问题。问题是在我的项目中,我有更多的模块。例如app模块和库模块。虽然类和R.string资源都来自app模块,但我在那里导入了com.package.library.R。不知怎的,Android Studio没有意识到这是一个错误,Gradle/Sync构建我的项目没有问题。但是当我试图运行单元测试时,Gradle构建失败,出现了所描述的错误。

package com.package.app

import com.package.library.R;
//...

class SomeClass {
  //...
  public void someMethod() {
     Toast.make(context, R.string.message, TOAST_LONG).show();
  }
}

当r .string. message资源实际上在我的com.packagename.app模块而不是com.packagename.library模块时。删除导入…解决了我的问题。

其他回答

在我的例子中,导入应用程序域名后跟“。R解决了这个问题。例子:

import com.example.myapp.R;

我也遇到过类似的问题,我是这样做的:

清洁项目和同步项目与Gradle,

检查buildTools版本在我的sdk

从build gradle (module)中将minSdkVersion从8修改为9

defaultConfig {
        applicationId "PackageName"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

但这一切都无济于事。

最后我找到了答案(这对我来说很管用)

从build.gradle(module:app)更改

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.1'
......
....
}

Or

选择文件|项目结构将构建工具版本更改为21.1.1

首先检查是否有任何错误在任何xml布局,如果然后解决它首先。

否则,从项目中删除junit依赖项并重新构建项目。

你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。

//有时由于不完全同步会产生问题

//所以只要按照步骤,它会做你的工作肯定

Click on Build->Clean Project and that will perform a gradle clean