北京網(wǎng)站優(yōu)化流程線上推廣外包公司
背景:
上一篇文章
android系統(tǒng)中data下的xml亂碼無法查看問題剖析及解決方法
發(fā)布后,想要尋找一個可以直接把二進制xml和普通xml進行相互轉(zhuǎn)換的,當時還寫了相關(guān)的方案,但是當時沒有找到現(xiàn)成的開源工具,后來經(jīng)過相關(guān)粉絲提醒找到了和方法2一模一樣思路的開源工具那就是abx2xml和xml2abx。
轉(zhuǎn)化命令使用介紹abx2xml和xml2abx
轉(zhuǎn)換前
屬于二進制亂碼
使用命令轉(zhuǎn)換,命令:
abx2xml ./system/users/0/appwidgets.xml ./system/users/0/appwidgets-read.xml
轉(zhuǎn)化后的./system/users/0/appwidgets-read.xml變成我們常見的普通xml
同樣普通xml也可以轉(zhuǎn)成二進制xml,轉(zhuǎn)化命令
xml2abx ./system/users/0/appwidgets-read.xml ./system/users/0/appwidgets-binary.xml
然后看看./system/users/0/appwidgets-binary.xml是不是變得二進制不可讀了
abx2xml和xml2abx命令解析
abx2xml和xml2abx其實代碼都是一樣的,本質(zhì)就是個sh腳本而已,運行的
#!/system/bin/sh
export CLASSPATH=/system/framework/abx.jar
exec app_process /system/bin com.android.commands.abx.Abx "$0" "$@"
可以看出本質(zhì)上都是調(diào)用到了Abx這個java類,參數(shù)就是一個$0,這個代表命令本身,比如使用使用是:
abx2xml input.xml output.xml
那么這里的 0 就是 a b x 2 x m l ,后面 i n p u t . x m l o u t p u t . x m l 就是 0就是abx2xml,后面input.xml output.xml就是 0就是abx2xml,后面input.xmloutput.xml就是@
源碼剖析Abx類
public class Abx {private static final String USAGE = "" +"usage: abx2xml [-i] input [output]\n" +"usage: xml2abx [-i] input [output]\n\n" +"Converts between human-readable XML and Android Binary XML.\n\n" +"When invoked with the '-i' argument, the output of a successful conversion\n" +"will overwrite the original input file. Input can be '-' to use stdin, and\n" +"output can be '-' to use stdout.\n";private static InputStream openInput(String arg) throws IOException {if ("-".equals(arg)) {return System.in;} else {return new FileInputStream(arg);}}private static OutputStream openOutput(String arg) throws IOException {if ("-".equals(arg)) {return System.out;} else {return new FileOutputStream(arg);}}private static void mainInternal(String[] args) {if (args.length < 2) {throw new IllegalArgumentException("Missing arguments");}final XmlPullParser in;final XmlSerializer out;if (args[0].endsWith("abx2xml")) {//這里根據(jù)傳遞近來參數(shù)看看是否要二進制xml轉(zhuǎn)普通還是逆過來轉(zhuǎn)in = Xml.newBinaryPullParser(); //二進制轉(zhuǎn)普通,那么輸入就是BinaryPullParser,輸出就是普通的out = Xml.newSerializer();} else if (args[0].endsWith("xml2abx")) {in = Xml.newPullParser();//普通轉(zhuǎn)二進制,那么輸入就是普通的,輸出就是newBinarySerializerout = Xml.newBinarySerializer();} else {throw new IllegalArgumentException("Unsupported conversion");}final boolean inPlace = "-i".equals(args[1]);final String inputArg = inPlace ? args[2] : args[1];final String outputArg = inPlace ? args[2] + ".tmp" : args[2];try (InputStream is = openInput(inputArg);OutputStream os = openOutput(outputArg)) {in.setInput(is, StandardCharsets.UTF_8.name());//輸入設(shè)置對應(yīng)的流InputStreamout.setOutput(os, StandardCharsets.UTF_8.name());//輸出設(shè)置對應(yīng)的流OutputStreamout.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);Xml.copy(in, out);//再調(diào)用 Xml.copy,重點就是在這out.flush();} catch (Exception e) {// Clean up failed output before throwingif (inPlace) {new File(outputArg).delete();}throw new IllegalStateException(e);}// Successful in-place conversion of a file requires a renameif (inPlace) {if (!new File(outputArg).renameTo(new File(inputArg))) {throw new IllegalStateException("Failed rename");}}}public static void main(String[] args) {try {mainInternal(args);System.exit(0);} catch (Exception e) {System.err.println(e.toString());System.err.println();System.err.println(USAGE);System.exit(1);}}
}
上面代碼核心部分有注釋,其實整體算比較簡單,核心方法就剩下一個Xml.copy方法
XML的相關(guān)copy方法如下:
/*** Copy the first XML document into the second document.* <p>* Implemented by reading all events from the given {@link XmlPullParser}* and writing them directly to the given {@link XmlSerializer}. This can be* useful for transparently converting between underlying wire protocols.** @hide*/public static void copy(@NonNull XmlPullParser in, @NonNull XmlSerializer out)throws XmlPullParserException, IOException {// Some parsers may have already consumed the event that starts the// document, so we manually emit that event here for consistencyif (in.getEventType() == XmlPullParser.START_DOCUMENT) {out.startDocument(in.getInputEncoding(), true);}while (true) {final int token = in.nextToken();//不斷循環(huán)xml的內(nèi)容節(jié)點等,簡單說就是in讀出什么就往out中寫什么switch (token) {case XmlPullParser.START_DOCUMENT:out.startDocument(in.getInputEncoding(), true);break;case XmlPullParser.END_DOCUMENT:out.endDocument();return;case XmlPullParser.START_TAG:out.startTag(normalizeNamespace(in.getNamespace()), in.getName());for (int i = 0; i < in.getAttributeCount(); i++) {out.attribute(normalizeNamespace(in.getAttributeNamespace(i)),in.getAttributeName(i), in.getAttributeValue(i));}break;case XmlPullParser.END_TAG:out.endTag(normalizeNamespace(in.getNamespace()), in.getName());break;case XmlPullParser.TEXT:out.text(in.getText());break;case XmlPullParser.CDSECT:out.cdsect(in.getText());break;case XmlPullParser.ENTITY_REF:out.entityRef(in.getName());break;case XmlPullParser.IGNORABLE_WHITESPACE:out.ignorableWhitespace(in.getText());break;case XmlPullParser.PROCESSING_INSTRUCTION:out.processingInstruction(in.getText());break;case XmlPullParser.COMMENT:out.comment(in.getText());break;case XmlPullParser.DOCDECL:out.docdecl(in.getText());break;default:throw new IllegalStateException("Unknown token " + token);}}}
更多framework詳細代碼和資料參考如下鏈接
hal+perfetto+surfaceflinger
https://mp.weixin.qq.com/s/LbVLnu1udqExHVKxd74ILg
其他課程七件套專題:
點擊這里
https://mp.weixin.qq.com/s/Qv8zjgQ0CkalKmvi8tMGaw
視頻試看:
https://www.bilibili.com/video/BV1wc41117L4/
更多framework假威風耗:androidframework007