我的项目在我的
Android项目的原始资源目录中有XML文件:
当我打包在.apk文件中时,我想从这些文件中删除评论(<! - ... - >):
原始文件样本:
<?xml version="1.0" encoding="utf-8"?> <celebrations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="celebrations_schema.xsd"> <!-- This is a very important comment useful during development --> <celebration name="celebration 1" type="oneTime" ... /> <!-- This is another very important comment useful during development --> <celebration name="celebration 2" type="reccurent" ... /> </celebrations>
预期过滤文件:
<?xml version="1.0" encoding="utf-8"?> <celebrations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="celebrations_schema.xsd"> <celebration name="celebration 1" type="oneTime" ... /> <celebration name="celebration 2" type="reccurent" ... /> </celebrations>
我找到了一些有趣的类似解决方案here,但我无法管理它的工作.在我的应用程序build.gradle中添加这些行
processResources { filesMatching('**/myxmlfiletobestrippedfromcomments.xml') { filter { String line -> line.replaceAll('<!--.*-->','') } } }
会产生以下错误:
Error:(86,0) Gradle DSL method not found: 'processResources()'
InputStream definition = context.getResources().openRawResource(resId); ... try { XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false); parser.setInput(definition,null); parser.nextTag(); return parseCelebrations(parser); } finally { definition.close(); }
解决方法
默认的xml的评论将不会打包成apk,你可以反编译来测试它.