11

My project have XML files in the raw resource directory of my Android project:

Project Layout

I would like to remove comments (<!-- ... -->) from these files when it is packaged in .apk file:

Original file sample:

<?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>

Expected filtered file:

<?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>

I have found some interesting similar solutions here, but I can't manage it to work. Adding these lines in my app build.gradle

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

generates the following error:

Error:(86, 0) Gradle DSL method not found: 'processResources()'

For opening and parsing the XML file from java code, I use the following method:

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();
}    
Community
  • 1
  • 1
DenisGL
  • 1,190
  • 1
  • 16
  • 31
  • Seems like you are trying to make a Gradle task. Not sure how to add that to the build process, though. https://docs.gradle.org/current/userguide/tutorial_using_tasks.html – OneCricketeer May 27 '16 at 23:18

2 Answers2

6

Default xml‘s comment will no package into apk, you can decompile to test it.

laomo
  • 436
  • 4
  • 12
  • Except if they are stored in the `raw` folder. At first I put this file in `res\xml` directory but had issues with using an XSD file just beside the xml one. I tried again, and now everything is ok: xml files are unreadable in apk and my schema validation works (I don't know what I did wrong at first). However, I had to change a little bit the opening of my xml file in the code. – DenisGL Jun 07 '16 at 18:39
1

I solved my issue by moving my file to the res\xml directory. In this case, the XML file is 'compiled' and is unreadable if I open the APK archive.

However, I had to change the opening and the parsing code of my XML file, to something like this:

XmlResourceParser xmlResourceParser = context.getResources().getXml(resId);
...
// I had to add this line (to skip START_DOCUMENT tag, which was not needed
// with opening from the `raw` directory)
while (xmlResourceParser.next() != XmlPullParser.START_TAG);

return parseCelebrations(xmlResourceParser);
DenisGL
  • 1,190
  • 1
  • 16
  • 31
  • However, I don't know why, and that's another issue / question, when moved to XML folder, my xml file cannot be validated against its XML schema with Android Studio anymore. – DenisGL Jun 09 '16 at 18:51