Mas*_*r_T 18 java android android-manifest
我想将明文AndroidManifest.xml文件转换为Android用于将其打包到最终APK中的二进制格式.
我想用Java做这个,因为我需要在android设备上做这个(这就是为什么这个问题不是重复的原因:如何将XML转换为Android二进制XML.我知道你可以使用AAPT,但我需要一个java方法)
有很多工具可以将二进制xml解码为可读文件,但没有关于如何做相反的事情,这就是我想要的.
任何关于如何实现这一点的信息或提示都表示赞赏.
您可能会发现许多可用于编译和反编译Android的xml文件的命令行工具.这些工具结合了几个构建工具,包括aapt(Android资产包装工具),以查看,创建和更新Zip兼容的存档(zip,jar,apk).由于此工具是Android SDK的一部分,因此Java中没有本机实现.
幸运的是,自编译-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
Run Code Online (Sandbox Code Playgroud)
如果仔细查看源代码,您会发现应用程序使用本机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 + ")");
}
}
Run Code Online (Sandbox Code Playgroud)
现在,查看示例代码以了解如何实现功能.例如,要更改清单文件中的值,
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)));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2920 次 |
| 最近记录: |