如何在打包文件时使用Gradle删除原始XML文件中的注释

Den*_*sGL 10 android gradle

我的项目在我的Android项目的原始资源目录中有XML文件:

项目布局

我希望<!-- ... -->.apk文件中打包时从这些文件中删除comments():

原始文件样本:

<?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>
Run Code Online (Sandbox Code Playgroud)

预期的过滤文件:

<?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>
Run Code Online (Sandbox Code Playgroud)

在这里找到了一些有趣的类似解决方案,但我无法管理它.在我的应用中添加这些行build.gradle

processResources {
    filesMatching('**/myxmlfiletobestrippedfromcomments.xml') {
        filter {
            String line -> line.replaceAll('<!--.*-->', '')
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

生成以下错误:

Error:(86, 0) Gradle DSL method not found: 'processResources()'
Run Code Online (Sandbox Code Playgroud)

要从java代码打开和解析XML文件,我使用以下方法:

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();
}    
Run Code Online (Sandbox Code Playgroud)

lao*_*omo 6

默认的xml的注释将没有包进入apk,你可以反编译来测试它.