Android Studio项目raw文件夹map_style文件引用未解析
Here are actionable fixes to resolve the unresolved reference issue, even after rebuilding and restarting Android Studio:
Verify the raw resource file details
Ensure the file insrc/main/res/rawis named exactlymap_style(case-sensitive, no uppercase letters, spaces, or special characters except underscores). File extensions (like.json) are ignored by the Android resource system, so the resource name will bemap_styleregardless of the extension.Check the R class import
Make sure you’re importing your app’s package-specificRclass (e.g.,com.yourappname.R) instead of the defaultandroid.R. The latter doesn’t include your custom resources.Clean and rebuild with cache invalidation
Try a deeper clean than just a standard rebuild:- Go to
Build > Clean Project - Go to
File > Invalidate Caches..., check "Clear file system cache and local history", then click "Invalidate and Restart" - After restart, run
Build > Rebuild Project
- Go to
Validate the resource file content
If the map style JSON file has syntax errors, Android may fail to generate the resource entry. Use a JSON validator to check for issues like missing commas, mismatched brackets, or invalid property names.Check the raw folder location
Confirm therawfolder is directly undersrc/main/res/(not insrc/test/resor another subdirectory). Android only processes resources in the main source set by default.Use getIdentifier as a temporary workaround
If the R class still doesn’t pick up the resource, usegetResources().getIdentifier()to fetch the resource ID dynamically:int mapStyleResId = getContext().getResources().getIdentifier("map_style", "raw", getContext().getPackageName()); if (mapStyleResId != 0) { googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), mapStyleResId)); }This bypasses the R class and directly looks up the resource by name.
Check module dependencies
If the raw resource is in a separate module, ensure that module is added as a dependency in your app’sbuild.gradlefile, and that resources are set to be accessible (no resource hiding rules applied).
内容的提问来源于stack exchange,提问作者Marc Exposito Francisco

