''cannot resolve symbol R' android studio error
Execution failed for task :app:mergeDebugResources.
What is this error and how to I fix it?
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
This is my xml above... and below is onCreate() where setContentView(R.layout.activity_main)
is causing the problem:
cannot resolve symbol R
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
Solution 1:[1]
Another solution, If anyone need it
I was using gradle 3.3.0. It was the main culprit. Wasted 6.50 hours from my life. Gradle 3.2.1 removed the error.
classpath 'com.android.tools.build:gradle:3.2.1'
Solution 2:[2]
'cannot resolve symbol R' error usually occurs and the 'symbol R' turns red in color whenever the gradle sync is not completed. Hence before running any project it is always advisable to first sync the project if any dependencies or anything have been added and then clean the project and then run it.
It should solve your problem.
Solution 3:[3]
R is predefined class in android.
you should clean your project and then build it again. it will resolve R.
Solution 4:[4]
Go to the file directory.
- Click "app"
- Click "com.example.?????"
- Right Click
- Hover over "New"
- Click on "Java Class"
- Name the file "R" and leave the other things blank & accept the defaults
- Click OK
You have finished the first part! - Click File
- Hover over "New"
- Click "New Project"
- Accept all defaults until you've reached the layout xml file
- Click "app"
- Click "com.example.android.?????"
- Click "R"
- Select all
- Go back to your old file
- Paste
- Finished!
Solution 5:[5]
probably your gradle sync failed , that is why it cannot be resolved resync the gradle or you have not imported yourpackagename.R;
Solution 6:[6]
Only this solution worked for me:
1- Go to your app directory -> /app/src/main/res/
2- Delete folder XML (Make sure that you don't have important files inside it)
Solution 7:[7]
Also i just found out if you have used an updated version of android studio like 4.2.2 and you want to go back to android studio 3.2.1 for example... the constraint layouts can also cause issues. if you delete the constraints to each one like: app:layout_constraintStart_toStartOf="parent" for one example it would successfully sync and this problem would be solved
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tushar Monirul |
| Solution 2 | UserName_Untold |
| Solution 3 | ojus kulkarni |
| Solution 4 | Stanislav Kralin |
| Solution 5 | JAAD |
| Solution 6 | |
| Solution 7 | Gary Hayes |
