Editing an XMLFile gives a read-only error

I am trying to modify and XML file, but when I try to run the code it is saying that the file in read-only and cannot be modified.

How do I change  the file to writable. I have checked the system settings and the file is not marked as read-only.

 
// Look for the Android Manifest
PsiFile androidManifest = Utils.findLayoutResourceByName(psiFile, project, "AndroidManifest");

// Create XML element
StringBuilder xmlTagBuilder = new StringBuilder();
// <uses-permission android:name="android.permission.INTERNET" />
xmlTagBuilder.append(" <uses-permission android:name=\"android.permission.");
xmlTagBuilder.append(permissionsMaps[2].getPermission());
xmlTagBuilder.append("\" />");
String xmlTag = xmlTagBuilder.toString();

XmlTag tagFromText = XmlElementFactory.getInstance(project).createTagFromText(xmlTag);
XmlFile containingFile = (XmlFile) androidManifest.getContainingFile();

containingFile.getRootTag().addSubTag(tagFromText, false);
3 comments
Comment actions Permalink

Please give the exact error message you get.


0
Comment actions Permalink

Alright I found out why the file was Read only. It was because a search via this method was bringing the AndroidManifest from a jar thats part of a dependency from Android SDK, and not the project's own AndroidManifest.xml.

Here's the method I am using.

 
// restricting the search to the current module - searching the whole project could return wrong layouts
Module module = ModuleUtil.findModuleForPsiElement(element);
PsiFile[] files = null;
if (module != null) {
  GlobalSearchScope moduleScope = module.getModuleWithDependenciesAndLibrariesScope(false);
  files = FilenameIndex.getFilesByName(project, name, moduleScope);
}
if (files == null || files.length <= 0) {
  // fallback to search through the whole project
 files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project));
  if (files.length <= 0) {
    return null; //no matching files
  }
}

return files[0];
   
When I use global scope ( like in the first if block ) I get the AndroidManifest from a dependency. 
 
And the next one returns an empty array.
  

I am probably searching incorrectly.

0

Please sign in to leave a comment.