我想把一个纯文本的AndroidManifest.xml文件转换成Android用来打包的二进制格式。
我想用Java语言做这件事,因为我需要在安卓设备上做这件事(这就是为什么这个问题是而不是How to convert XML to Android Binary XML的副本。我知道你可以使用AAPT,但我需要一个java方法)
有很多工具可以将二进制xml解码成可读的文件,但是没有工具告诉我如何做相反的事情,而这正是我想要的。
任何关于如何实现这一目标的信息或提示都将不胜感激。
发布于 2017-08-22 15:46:14
您可能会发现很多命令行工具可用于编译和反编译 Android 的 xml 文件。 这些工具结合了多个构建工具,包括 aapt (Android Asset Packaging Tools),用于查看、创建和更新与 Zip 兼容的档案(zip、jar、apk)。 由于此工具是 Android SDK 的一部分,因此在 Java 中没有本机实现。
幸运的是,self-compile-Android 存储库具有Java Native Interface (JNI).中的所有必要文件。 它们已准备好从 Android 应用程序内部使用,并且能够自我编译、变异和病毒传播。
这里是应用程序中可用的本机模块列表:
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
如果仔细查看源代码,您会发现应用程序使用原生jni文件执行aapt命令:
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 + ")");
}
}
现在,查看示例代码以了解这些功能是如何实现的。例如改变清单文件中的值,
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)));
}
发布于 2020-04-18 16:27:15
从github发布页面here下载xml2axml.jar
工具,然后可以使用以下命令来解码axml和编码xml文件:
将xml文件编码为axml:的
java -jar xml2axml e [AndroidManifest-readable-in.xml] [AndroidManifest-bin-out.xml]
将axml文件解码为xml:的
java -jar xml2axml d [AndroidManifest-bin-in.xml] [AndroidManifest-readable-out.xml]
发布于 2017-08-22 17:12:00
你的问题可以归结为如何在Android设备上运行aapt。
由于aapt是开源的,所以最好的解决方案就是自己构建它!
很多人已经这样做了--事实上,Play Store中也有这样的例子。有关如何使用从AIDE应用程序获得的aapt的说明,请参阅http://talc1.loria.fr/users/cerisara/posts/buildandroid/。在Play Store上搜索一下,就会发现许多其他的Android IDE,它们也提供aapt功能。
https://stackoverflow.com/questions/29669937
复制相似问题