C # Call aapt to view apk file information,
This is the first article.
1. Origin
The idea comes from the project development process. You need to confirm the apk file version to verify its functional differences so as to locate the problem, so du Niang, get the APK information Viewer (APK-info) tool, whose version is 0.2.
It displays detailed apk information, as shown in:
However, it is not easy to use. You can only find and display the apk file in the double-click dialog box, or drag and drop the apk to its icon to open it. You cannot drag and drop the apk to open the interface. It does not open the portal any more and has poor support for Chinese characters, such as the Baidu mobile guard apk Information on the right.
2. Principles
After analyzing the information, aapt.exe is used to unbind the AndroidManifest. xml file in the apk package for information display.
The usage of aapt.exe and many tutorials on the Internet. Apk information is stored in AndroidManifest. xml, which is an encrypted xml file. Use the dump command of aapt to parse the file. The syntax is as follows:
> Aapt dump badging QQ_482.apk
It seems that all the information you want has ...... Slow down. What about Chinese?
> Aapt dump badging QQ_482.apk
Garbled characters are displayed in Chinese. If it's messy, transcode it!
How can I not write a similar tool by myself? Okay, whole!
3. Implementation
Using this idea, c # implements parsing and obtains the output data of the cmd pipeline. The core code is as follows (for smooth interface sound, I set it to the first line of parsing ):
Private void Decoder (object state) {if (! File.Exists(this.apk Path) return; string aaptPath = Path. Combine (this. appPath, @ "tools \ aapt.exe"); if (! File. Exists (aaptPath) aaptPath = Path. Combine (this. appPath, @ "aapt.exe"); if (! File. Exists (aaptPath) {var handler = AaptNotFoundEvent; if (handler! = Null) handler (); return;} var startInfo = new ProcessStartInfo (aaptPath); string args = string. format ("dump badging \" {0} \ "", this.apk Path); startInfo. arguments = args; startInfo. useShellExecute = false; startInfo. redirectStandardOutput = true; startInfo. createNoWindow = true; using (var process = Process. start (startInfo) {var sr = process. standardOutput; while (! Sr. EndOfStream) {infos. Add (sr. ReadLine ();} process. WaitForExit (); // parse ParseInfo (sr. CurrentEncoding );}}
//application: label='MobileGo' icon='r/l/icon.png'if (info.IndexOf("application:") == 0){ string appName = GetKeyValue(info, "label="); this.AppName = Encoding.UTF8.GetString(currentEncoding.GetBytes(appName)); this.IconPath = GetKeyValue(info, "icon="); GetAppIcon(this.IconPath);}
The execution interface is as follows:
It can be seen that the support for Chinese characters is still unfriendly, even if UTF-8 is used to convert to the default Chinese encoding.
What should we do?
4. Improvement
However, if it is directly output to an external file, the Chinese display is correct:
> Aapt dump badging QQshurufa_1991.apk> info.txt
Parse the output file! In the first place, I was in conflict with this solution because I didn't want to generate additional files. I wouldn't want to generate external files if I could intercept the output from the internal cmd pipeline, it's just a personal liking.
However, if the current transcoding scheme is ineffective, you only need to use it. The output code is as follows:
Private void Decoder (object state) {if (! File.Exists(this.apk Path) return; string aaptPath = Path. Combine (this. appPath, @ "tools \ aapt.exe"); if (! File. Exists (aaptPath) aaptPath = Path. Combine (this. appPath, @ "aapt.exe"); if (! File. Exists (aaptPath) {var handler = AaptNotFound; if (handler! = Null) handler (); return;} StringBuilder sb = new StringBuilder (255); int result = getpaipathname (aaptPath, sb, 255); if (result! = 0) aaptPath = sb. toString (); var startInfo = new ProcessStartInfo ("cmd.exe"); string dumpFile = Path. getTempFileName (); // This is a time-consuming process, only for processing Chinese garbled Characters string args = string. format ("/k {0} dump badging \" {1} \ "> \" {2} \ "& exit", aaptPath, this.apk Path, dumpFile); startInfo. arguments = args; startInfo. windowStyle = ProcessWindowStyle. hidden; this. infos. clear (); using (var process = Process. start (startInfo) {pr Ocess. waitForExit (2000);} if (File. exists (dumpFile) {// parse using (var sr = new StreamReader (dumpFile, Encoding. UTF8) {string line; while (line = sr. readLine ())! = Null) {this. infos. Add (line) ;}parseinfo () ;}try {File. Delete (dumpFile) ;}catch {}}}
Okay, everything is normal ...... It only takes some time to build the cmd pipeline script.
Check whether the Chinese information is displayed. Everything is normal:
5. Postscript
This is my first blog. I have been writing code for more than ten years. Although I always want to record my experiences and experiences, I am so lazy that I can't help but finally write it out.
In fact, this example has been completed for a long time, but it has been improved recently. You can see the Meteoric_cry blog post during the data query process: the principle of the windows apk viewing tool is quite similar, he also wrote the APK Helper tool, which is simple and easy to use.
I want to add the source code to Github, but I have to wait for half a day and it is not successful. Therefore, I plan to put it on hold for the moment.
This tool is stored on the Internet and updated on an irregular basis as needed. If necessary, the tool can be used by a partner at: ApkInfo.zip. If your partner has other requirements, leave a message to wait.