Android: converting standard XML to Android Binary XML format (AXML)

Download the xml2axml.jar tool from the github release page here and then you can use the following commands to decode axml and encode xml files:

Encoding xml files to axml:

java -jar xml2axml e [AndroidManifest-readable-in.xml] [AndroidManifest-bin-out.xml]

Decoding axml files to xml:

java -jar xml2axml d [AndroidManifest-bin-in.xml] [AndroidManifest-readable-out.xml]

You may find a lot of command-line tools that can be used to compile and decompile the xml files for Android. Those tools are combined of several build tools including aapt (Android Asset Packaging Tools) to view, create, and update Zip-compatible archives (zip, jar, apk). Since This tool is a part of the Android SDK, There's no native implementation of it in Java.

Fortunately, self-compile-Android repository has all the necessary files in Java Native Interface (JNI). They are ready to be used from inside an Android App and capable of self-compilation, mutation, and viral spreading.

Here a list of available native modules in the app:

aapt -> Platform_Framework_Base\tools\aapt aidl -> Platform_Framework_Base\tools\aidl androidfw -> Platform_Framework_Base\include\androidfw

zipalign -> Platform_Build\tools\zipalign host -> Platform_Build\lib\host

libpng -> Platform_External_Libpng expat -> Platform_External_Expat zlib -> Platform_External_Zlib

libcutils -> Platform_System_Core\libcutils cutils -> Platform_System_Core\include\cutils

liblog -> Platform_System_Core\liblog log -> Platform_System_Core\include\log

libutils -> Platform_System_Core\libutils utils -> Platform_System_Core\include\utils

log.h -> Platform_System_Core\include\android

asset_manager.h -> Platform_Framework_Native\include\android looper.h -> Platform_Framework_Native\include\android

zopfli -> zopfli\src

ld -> Tool_Chain_Utils\binutils-2.25\ld

If you look closely at the source you'll find the app executing aapt commands using the native jni files:

private void runAapt() throws Exception {
    Util.deleteRecursive(new File(S.dirRes, "drawable-xxhdpi"));

    Aapt aapt = new Aapt();
    int exitCode = aapt.fnExecute("aapt p -f -v -M " + S.xmlMan.getPath() + " -F " + S.ap_Resources.getPath()
            + " -I " + S.jarAndroid.getPath() + " -A " + S.dirAssets.getPath() + " -S " + S.dirRes.getPath()
            + " -J " + S.dirGen.getPath());

    if (exitCode != 0) {
        throw new Exception("AAPT exit(" + exitCode + ")");
    }
}

Now, go through the sample code to see how the functionalities are implemented. For example to change a value in manifest file,

private void modifyManifest() throws Exception {
        Document dom = Util.readXml(S.xmlMan);

        dom.getDocumentElement().getAttributes().getNamedItem("package").setNodeValue(userInput.appPackage);

        Transformer t = tf.newTransformer();
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.VERSION, "1.0");
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(xmlFile)));
    }