在Android Studio的默认设置中,“排除文件夹”列表保存在项目根目录下的.iml文件中。可以通过以下代码示例来访问.iml文件,并获取或修改排除文件夹列表。
//获取.iml文件的路径 File imlFile = new File(projectDirPath + "/.iml"); //读取.iml文件中的内容 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(imlFile); Element component = doc.getDocumentElement().getElementsByTagName("component").item(1); Element contentElement = ((Element) component.getElementsByTagName("content").item(0)). getElementsByTagName("excludeFolder").item(0); String excludedFolders = contentElement.getAttribute("url"); //修改排除的文件夹列表 contentElement.setAttribute("url", excludedFolders + ",newExcludedFolder"); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(imlFile); transformer.transform(source, result);