Use of the Java--processbuilder class and get apk information

Source: Internet
Author: User

This is to get the APK package version number, package name, size and other information. There are actually two ways:


One is:

Pure Java Environment Get APK information (package name, version, version number, size, permissions ...), Java language writing PC-side get apk information.

This article is to get information by parsing the XML in the APK.


In fact, there is another way to use the Aapt.exe file inside the Android development tool,

C:/getapkinfo/aapt.exe d badging d:/new/test.apk

Specify the path to the aapt file after two for the location of the APK package followed by the parameter.

This is the direct display of the results in a DOS window, if you want to import the results into a TXT file, you can use:

C:/getapkinfo/aapt.exe d badging D:/new/test.apk>c:/test.txt

back to the Prossbuilder class, we need to get the process instance through Prossbuilder, as the API describes:

The Processbuilder.start () and Runtime.exec methods create a native process and return an instance of a subclass of Proces s that can is used to control the process and obtain information about it. The class process provides methods for performing input from the process, performing output to the process, and waiting for th E process to complete, checking the exit status of the process, and destroying (killing) the process.


The Processbuilder.start () and runtime.exec methods Create a local process and return an instance of the process subclass, which can be used to control processes and get process information. The process class is able to perform input from the process, perform output to the process, wait for the process to complete, check the progress exit status, and terminate the process.


Before writing the timer, it is actually using the Runtime.exec method to direct the command operation. But what if we are going to read the information that the process executes after the command has been manipulated, we need to get it with the class process.


Creation of the Processbuilder class:

Processbuilder PB = new Processbuilder ();

Get the process instance:

Process p = pb.command ("C:/getapkinfo/aapt.exe", "D", "badging", "d:/new/test.apk"). Start ();

Why do you write this: You can view this: Processbuilder exception CreateProcess error=2,????? Μ???


Then use getInputStream to connect the output of the child process:

But in the end we need to read the entire output information, can be used to package read BufferedReader, but the bufferedreader of the construction method of the parameters of reader, now get the InputStream, thinking here may be broken off, Because the flow encapsulation of the relationship is often forgotten, and now through the API has a new idea, since the passed parameter type is reader class, then according to polymorphism, reader subclass can also be passed.


So find the reader and see what subclasses it has underneath, and watch as you look at a familiar class--inputstreamreader, see how you construct it, and find it's just InputStreamReader (InputStream in).

So, write it out in a breath:

BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream ()));            String s = "";            while ((S=br.readline ()) = null) {SYSTEM.OUT.PRINTLN (s); }

Chinese garbled how to do?

There is another way to construct the incoming encoding: InputStreamReader (InputStream in, Charset CS).

BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream (), "UTF-8"));

the entire source code implementation:

public class Testprocess {public static void main (string[] args) {Processbuilder PB = new Processbuilder (); try {Process P = Pb.command ("C:/getapkinfo/aapt.exe", "D", "badging", "d:/new/test.apk"). Start (); BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream (), "UTF-8")); String s = ""; while ((S=br.readline ()) = null) {SYSTEM.OUT.PRINTLN (s);}} catch (IOException e) {e.printstacktrace ();}}}

The final result:


Use of the Java--processbuilder class and get apk information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.