Obtain the APK information (package name, version, version number, size, permission...) in a pure JAVA environment, and obtain the APK information on the PC end in a pure JAVA language.

Source: Internet
Author: User
Tags addgroup

Obtain the APK information (package name, version, version number, size, permission...) in a pure JAVA environment, and obtain the APK information on the PC end in a pure JAVA language.

Obtain APK information in a pure JAVA environment: package name, version, version number, size, permission...

To obtain APK information in a pure Java environment, two packages are required: AXMLPrinter2.jar and jdom. jar, which are used to decompile XML and parse XML

Project directory

 

This class obtains the APK information.

 

Public class ApkUtil {private static final Namespace NS = Namespace. getNamespace (http://schemas.android.com/apk/res/android); @ SuppressWarnings (unchecked) public static String getApkInfo (String apkPath) {ApkInfo apkInfo = new ApkInfo (); SAXBuilder builder = new SAXBuilder (); Document document = null; try {document = builder. build (getXmlInputStream (apkPath);} catch (Exception e) {e. printStackTrace ();} Element root = document. getRootElement (); // follow the node --> manifestapkInfo. setVersionCode (root. getAttributeValue (versionCode, NS); apkInfo. setVersionName (root. getAttributeValue (versionName, NS); apkInfo. setApkPackage (root. getAttributeValue (package, NS); Element elemUseSdk = root. getChild (uses-sdk); // child node --> uses-sdkapkInfo.setMinSdkVersion (elemUseSdk. getAttributeValue (minSdkVersion, NS); List listPermission = root. getChildren (uses-permission); // The subnode is a collection of List permissions = new ArrayList (); for (Object object: listPermission) {String permission = (Element) object ). getAttributeValue (name, NS); permissions. add (permission);} apkInfo. setUses_permission (permissions); String s = root. getAttributes (). toString (); String c [] = s. split (,); String versionCode = null; String versionName = null; String packageName = null; for (String a: c) {if (. contains (versionCode) {versionCode =. substring (. indexOf (versionCode =) + 13,. lastIndexOf ();} if (. contains (versionName) {versionName =. substring (. indexOf (versionName =) + 13,. lastIndexOf ();} if (. contains (package) {packageName =. substring (. indexOf (package =) + 9,. lastIndexOf ();} System. out. println (Version: + versionCode + version name: + versionName + packageName); String str = version: + versionCode + version name: + versionName + packageName; // return root. getAttributes (). toString (); return str;} private static InputStream getXmlInputStream (String apkPath) {InputStream inputStream = null; InputStream xmlInputStream = null; ZipFile zipFile = null; try {zipFile = new ZipFile (apkPath); ZipEntry zipEntry = new ZipEntry (AndroidManifest. xml); inputStream = zipFile. getInputStream (zipEntry); AXMLPrinter xmlPrinter = new AXMLPrinter (); xmlPrinter. startPrinf (inputStream); xmlInputStream = new ByteArrayInputStream (xmlPrinter. getBuf (). toString (). getBytes (UTF-8);} catch (IOException e) {e. printStackTrace (); try {inputStream. close (); zipFile. close () ;}catch (IOException e1) {e1.printStackTrace () ;}} return xmlInputStream ;}}

Go directly to the code and explain it again next time.

The following is the main class of the interface, which mainly reads the location and size of the APK.

 

/***** @ Author Administrator */public class MainTest extends javax. swing. JFrame {private File f = null; private List
 
  
PathList = new ArrayList
  
   
();/*** Creates new form NewJFrame */public MainTest () {initComponents ();} /*** This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. the content of this method is always * regenerated by the Form Editor. * // @ SuppressWarnings (unchecked )//
   
    
// GEN-BEGIN: initComponents private void initComponents () {jButton1 = new javax. swing. JButton (); jScrollPane1 = new javax. swing. JScrollPane (); jTextArea1 = new javax. swing. JTextArea (); setdefaclocloseoperation (javax. swing. windowConstants. EXIT_ON_CLOSE); jButton1.setText (select the file location); jButton1.addActionListener (new java. awt. event. actionListener () {public void actionreceivmed (java. awt. event. actionEvent evt) {openFilePath (evt) ;}}); jTextArea1.setColumns (20); jTextArea1.setFont (new java. awt. font (, 0, 20); jTextArea1.setRows (5); jScrollPane1.setViewportView (jTextArea1); javax. swing. groupLayout layout = new javax. swing. groupLayout (getContentPane (); getContentPane (). setLayout (layout); layout. setHorizontalGroup (layout. createParallelGroup (javax. swing. groupLayout. alignment. LEADING ). addGroup (layout. createSequentialGroup (). addContainerGap (). addGroup (layout. createParallelGroup (javax. swing. groupLayout. alignment. LEADING ). addComponent (jScrollPane1, javax. swing. groupLayout. DEFAULT_SIZE, 542, Short. MAX_VALUE ). addGroup (layout. createSequentialGroup (). addComponent (jButton1 ). addGap (0, 0, Short. MAX_VALUE ))). addContainerGap (); layout. setVerticalGroup (layout. createParallelGroup (javax. swing. groupLayout. alignment. LEADING ). addGroup (layout. createSequentialGroup (). addContainerGap (). addComponent (jButton1 ). addPreferredGap (javax. swing. layoutStyle. componentPlacement. UNRELATED ). addComponent (jScrollPane1, javax. swing. groupLayout. PREFERRED_SIZE, 218, javax. swing. groupLayout. PREFERRED_SIZE ). addContainerGap (); pack ();}//
   // GEN-END: initComponents private void openFilePath (java. awt. event. actionEvent evt) {// GEN-FIRST: event_openFilePath // TODO add your handling code here: int result = 0; String path = null; JFileChooser fileChooser = new JFileChooser (); fileSystemView fsv = FileSystemView. getFileSystemView (); // note that an important sentence is System. out. println (fsv. getHomeDirectory (); // obtain the desktop path fileChooser. setCurrentDirectory (fsv. getHom EDirectory (); fileChooser. setDialogTitle (select the file path); fileChooser. setApproveButtonText (OK); fileChooser. setFileSelectionMode (JFileChooser. DIRECTORIES_ONLY); result = fileChooser. showOpenDialog (this); if (JFileChooser. APPROVE_OPTION = result) {path = fileChooser. getSelectedFile (). getPath (); System. out. println (path: + path);} if (path = null | path. equals () {return;} jTextArea1.setText (); f = new F Ile (path); if (f. exists () {getList (f); for (String filePath: pathList) {File file File = new File (filePath); if (! File. exists () {break;} try {String str = jTextArea1.getText (); String s = file Name: + file. getName () ++ file size: + getFileSize (file) + Bit; + (float) getFileSize (file)/(float) 1024/(float) 1024 + MB + ApkUtil. getApkInfo (file. getPath () +; jTextArea1.setText (s + str);} catch (Exception ex) {Logger. getLogger (MainTest. class. getName ()). log (Level. SEVERE, null, ex) ;}}} private void getList (File file) {if (file. isFile () & file.getName().endsWith(.apk) {pathList. add (file. getPath ();} else {File fileList [] = file. listFiles (); for (File f: fileList) {try {getList (f);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}}/*** get the specified File size * @ param f * @ return * @ throws Exception */private long getFileSize (file File) throws Exception {long size = 0; if (file. exists () {FileInputStream fiis = null; fiis = new FileInputStream (file); size = fiis. available ();} else {file. createNewFile ();} return size;}/*** @ param args the command line arguments */public static void main (String args []) {/* Set the Nimbus look and feel *///
   
    
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */try {for (javax. swing. UIManager. lookAndFeelInfo info: javax. swing. UIManager. getInstalledLookAndFeels () {if (Nimbus. equals (info. getName () {javax. swing. UIManager. setLookAndFeel (info. getClassName (); break ;}} catch (ClassNotFoundException ex) {java. util. logging. logger. getLogger (MainTest. class. getName ()). log (java. util. logging. level. SEVERE, null, ex);} catch (InstantiationException ex) {java. util. logging. logger. getLogger (MainTest. class. getName ()). log (java. util. logging. level. SEVERE, null, ex);} catch (IllegalAccessException ex) {java. util. logging. logger. getLogger (MainTest. class. getName ()). log (java. util. logging. level. SEVERE, null, ex);} catch (javax. swing. unsupportedLookAndFeelException ex) {java. util. logging. logger. getLogger (MainTest. class. getName ()). log (java. util. logging. level. SEVERE, null, ex );}//
   /* Create and display the form */java. awt. eventQueue. invokeLater (new Runnable () {public void run () {new MainTest (). setVisible (true) ;}}) ;}// Variables declaration-do not modify // GEN-BEGIN: variables private javax. swing. JButton jButton1; private javax. swing. JScrollPane jScrollPane1; private javax. swing. JTextArea jTextArea1; // End of variables declaration // GEN-END: variables}
  
 

Instance test. This is found in Jinshan.


 

Only the package name, version, version number, and size are checked. I have not checked the permission.

 

List listPermission = root. getChildren (uses-permission); // The subnode is a collection of List permissions = new ArrayList (); for (Object object: listPermission) {String permission = (Element) object ). getAttributeValue (name, NS); permissions. add (permission);} apkInfo. setUses_permission (permissions );
This section is the code for obtaining permissions.
 

 

Related Article

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.