标签:des android style blog http io ar color os
纯JAVA环境获取APK信息:包名,版本,版本号,大小,权限...
纯Java环境获取APK信息需要两个包:AXMLPrinter2.jar 跟jdom.jar,用于反编译XML和解析XML的
项目目录
这个类是获取APK信息的
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();//跟节点-->manifest apkInfo.setVersionCode(root.getAttributeValue("versionCode",NS)); apkInfo.setVersionName(root.getAttributeValue("versionName", NS)); apkInfo.setApkPackage(root.getAttributeValue("package", NS)); Element elemUseSdk = root.getChild("uses-sdk");//子节点-->uses-sdk apkInfo.setMinSdkVersion(elemUseSdk.getAttributeValue("minSdkVersion", NS)); List listPermission = root.getChildren("uses-permission");//子节点是个集合 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(a.contains("versionCode")){ versionCode = a.substring(a.indexOf("versionCode=\"")+13, a.lastIndexOf("\"")); } if(a.contains("versionName")){ versionName = a.substring(a.indexOf("versionName=\"")+13, a.lastIndexOf("\"")); } if(a.contains("package")){ packageName = a.substring(a.indexOf("package=\"")+9, a.lastIndexOf("\"")); } } System.out.println("\n版本号:"+versionCode+"\n版本名:"+versionName+"\n包名:"+packageName); String str = "\n版本号:"+versionCode+"\n版本名:"+versionName+"\n包名:"+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; } }
直接上代码,下次再补过讲解.
下面这个是一个界面的主类,主要读取APK位置和大小
/** * * @author Administrator */ public class MainTest extends javax.swing.JFrame { private File f = null; private List<String> pathList = new ArrayList<String>();; /** * 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") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("选择文件位置"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(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(); }// </editor-fold>//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(); //注意了,这里重要的一句 System.out.println(fsv.getHomeDirectory()); //得到桌面路径 fileChooser.setCurrentDirectory(fsv.getHomeDirectory()); fileChooser.setDialogTitle("选择文件路径"); fileChooser.setApproveButtonText("确定"); 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 File(path); if (f.exists()) { getList(f); for (String filePath: pathList) { File file = new File(filePath); if (!file.exists()) { break; } try { String str = jTextArea1.getText(); String s = "文件名:" + file.getName() + "\n" + "文件大小:"+ getFileSize(file) + "Bit; "+ (float)getFileSize(file)/(float)1024/(float)1024+"MB" +ApkUtil.getApkInfo(file.getPath()) + "\n\n"; 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 block e.printStackTrace(); } } } } /** * 获取指定文件大小 * @param f * @return * @throws Exception */ private long getFileSize(File file) throws Exception { long size = 0; if (file.exists()) { FileInputStream fis = null; fis = new FileInputStream(file); size = fis.available(); } else { file.createNewFile(); } return size; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* 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); } //</editor-fold> /* 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 }
这里只查了包名,版本,版本号,大小,没有查权限,权限被我屏蔽掉了
List listPermission = root.getChildren("uses-permission");//子节点是个集合 List permissions = new ArrayList(); for(Object object : listPermission){ String permission = ((Element)object).getAttributeValue("name", NS); permissions.add(permission); } apkInfo.setUses_permission(permissions);这一段就是获取权限的代码
尊重原创,转载请注明出处:http://blog.csdn.net/chillax_li/article/details/41850863
纯JAVA环境获取APK信息(包名,版本,版本号,大小,权限...),纯JAVA语言编写PC端获取APK信息
标签:des android style blog http io ar color os
原文地址:http://blog.csdn.net/chillax_li/article/details/41850863