22

I want to convert a plaintext AndroidManifest.xml file into the binary format Android uses to package it inside the final APK.

I want to do this in Java, since I need to do it on the android device itself (which is why this question is NOT a duplicate of: How to convert XML to Android Binary XML. I know you can use AAPT, but I need a java method)

There's plenty of tools around to decode the binary xml into a readable file, but nothing on how to do the opposite, which is what I want.

Any info or hint on how this could be achieved is appreciated.

Community
  • 1
  • 1
Master_T
  • 7,232
  • 11
  • 72
  • 144
  • 1
    There is this SO question as a resource for decoding AXML to XML https://stackoverflow.com/a/4761689/4307644 He was nice enough to comment on the format. All we need is to reverse engineer the "hack" portion of unknown data, and then reverse this guy's process. It's late here, so I will look into it in the morning; but hopefully someone with a bit more time can use this resource and help before then. – MeetTitan Aug 22 '17 at 06:24
  • 1
    To give a bit more clarity on the bounty, what @MeetTitan has suggested meets the criteria for receiving the bounty. Tools like aapt, apktool, etc while they do "work" don't produce the desired outcome. – Gavin Miller Aug 22 '17 at 15:46
  • @Gavin So basically you are looking for a code answer that doesn't depend on sys calls to external tools, right? – David Rawson Aug 22 '17 at 21:16
  • @David he explained it pretty well. He'd like a pure Java method of encoding his manifest. Reverse engineering the aapt open source project and writing the encoding module in Java is another idea solid idea. – MeetTitan Aug 23 '17 at 00:38
  • @MeetTitan I'm just trying to clarify so that you get the answer you want :-) You can see there are already two answers that simply run `aapt` using Java. While they are good answers in of themselves, they don't seem to be the solution the bounty poster looking for. – David Rawson Aug 23 '17 at 00:49
  • @David, sorry if my tone was misconstrued as confrontational, I was simply clarifying as well. However what I suggested was a rewrite of the aapt tool in Java instead of C/C++, which would be a pure Java method and meet the requirements of the bounty (encoding in Java without native methods). Aapt source will provide great insight into the formatting methods to generate a sane binary android XML file, which can be ported to java. – MeetTitan Aug 23 '17 at 01:04
  • 1
    @MeetTitan no offence taken, I just hope this interesting question gets a good answer. – David Rawson Aug 23 '17 at 01:12

3 Answers3

9

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)));
    } 
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
  • Thanks, this was a very old question and I'm not working on that project anymore, but I'll accept it as it looks very well researched and I have no reason to believe it wouldn't work. – Master_T Sep 01 '17 at 15:48
  • Much appreciated. @Master_T – Prokash Sarkar Sep 01 '17 at 16:53
  • 3
    @Master_T I think this should not be accepted because the question is about `how to convert a single standard XML to AXML`. – amrezzd Feb 18 '19 at 07:15
2

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]
  • 2
    Please don't post identical answers on multiple questions. The system can detect this and will aim to delete them automatically and you run the risk of posts being flagged as possible spam. – David Buck Apr 18 '20 at 09:03
  • This question is similar to [this question](https://stackoverflow.com/questions/11367314/how-to-convert-xml-to-android-binary-xml). I have posted the same answer on both of these questions because it is the right answer for both of the questions. I don`t see any spam here and it was never my intention. If you want i can delete my answer from here. What else should i do? – Talha Asghar Apr 18 '20 at 17:23
  • Per the comment from Dharman on your other answer, it is definitely a much better answer now you have demonstrated the use of the product rather than simply directing someone to look at the repo. – David Buck Apr 18 '20 at 17:34
  • Its always an honor for me to have such great guys like you around here. – Talha Asghar Apr 18 '20 at 17:41
  • Great bro thanks, i worked painfully but now it's working absolutely fine for me, on Android – DiLDoST Oct 12 '22 at 19:39
0

Your problem boils down to how can you run aapt on the Android device itself.

Since aapt is open source, the best solution is just build it yourself!

Plenty of people have already done that - and indeed there are examples in the Play Store. See http://talc1.loria.fr/users/cerisara/posts/buildandroid/ for instructions on how to use aapt obtained from the AIDE app. A search on the Play Store turns up plenty of other Android IDEs which also offer aapt functionaility.

zmarties
  • 4,809
  • 22
  • 39