Count the number of methods in the JAR package Dex file

Source: Internet
Author: User

in the previous period of time doing Android projects, there has been a problem of more than 65535 methods, if the number of methods in the code is not more than 65535, you can

add a line to the Project.Properties file dex.force.jumbo=true, solve this problem.

Later, referring to some methods on the Internet, I wrote a gadget to count the number of methods in jar packages and Dex files. The main principle is to use the Dex file structure of the file header has a method_ids_siz to count the number of methods.

Now share it, the code is as follows, the direct copy is compiled into a jar package, and then the console: Java-jar Xxx.jar file can see the output. folder, jar, and Dex files are currently supported.

Import Java.io.bufferedinputstream;import Java.io.bufferedreader;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstreamreader;public class Test2 {private void Test () {}private final static String jar = "Jar";p rivate final static string dex = "Dex";p rivate final static string DIR = "dir";p rivate final static string UNKNOWN = "UNKNOWN";/** * based on The extension gets the file type */public static string Getfiletype (string path) {String type = unknown;try {File File = new file (path), if (file. Isdirectory ()) {type = DIR;} else {if (Getextensionname (File.getname ()). Equalsignorecase (DEX)) {type = DEX;} else if (Gete Xtensionname (File.getname ()). Equalsignorecase (Jar)) {type = jar;}}} catch (Exception e) {}return type;} public static void Main (string[] args) {if (args.length! = 1) {System.out.println ("Param Error");} else {String type = Get FileType (Args[0]), if (Type.equalsignorecase (DEX)) {Resolvedex (args[0]);} else if (Type.equalsignorecASE (JAR)) {Resolvejar (Args[0]),} else if (Type.equalsignorecase (DIR)) {Resolvedir (args[0]);} else {System.err.println ("Unknown File Type");}} SYSTEM.OUT.PRINTLN ("parsing End");} /** * Simple get extension, not completely accurate. If it is completely accurate, it can be judged according to the file stream. * @param filename * @return */public static string Getextensionname (string filename) {if ((filename! = null) && (f Ilename.length () > 0)) {int dot = filename.lastindexof ('. '); if (dot >-1) && (dot < (Filename.length ()-1)) {return filename.substring (dot + 1);}} return filename;} /** * Process Dex File * * @param path */public static void Resolvedex (String path) {try {File file = new file (path);  FileInputStream fis = new FileInputStream (file); byte[] bytes = new byte[1000];if (fis.read (bytes)! =-1) {StringBuilder SB = new StringBuilder (); for (int i = i--; i >) {sb.append (integer.tobinarystring (Bytes[i] & 255));} System.out.println (File.getname () + "Number of methods:" + Integer.parseint (sb.tostring (), 2));}} catch (FileNotFoundException e) {e.printstacktrace ();} CATCH (IOException e) {e.printstacktrace ();}} /** * Parse jar * * @param path */public static void Resolvejar (string filePath) {try {string path = system.getenv ("DX"); Processbuilder pb=new processbuilder ("Dx.bat", "--dex", "--output=c://temp.dex", FilePath);p b.directory (New File ( Path));p B.redirecterrorstream (true); Process p =pb.start (); Bufferedinputstream in = new Bufferedinputstream (P.getinputstream ()); BufferedReader INBR = new BufferedReader (new InputStreamReader (in)); String linestr;while ((linestr = Inbr.readline ()) = null) {//Get command output information in console after//system.out.println (LINESTR);//print output information} Checks whether the command failed to execute. if (p.waitfor () = 0) {if (P.exitvalue ()! = 0) {//P.exitvalue () ==0 indicates normal end, 1: Abnormal end System.err.println (filepath+ "command execution failed!"); Inbr.close (); In.close (); return;}} Inbr.close (); In.close (); File Originfile = new file (FilePath); File File = new file ("C://temp.dex");  FileInputStream fis = new FileInputStream (file); byte[] bytes = new byte[1000];if (fis.read (bytes)! =-1) {StringBuilder SB = new StringBuilder (); for (int i = 91; i > 87; i--) {sb.append (String.Format ("%02x", (Bytes[i] & 255)));} System.out.println (Originfile.getabsolutepath () + "Number of methods:" + Integer.parseint (sb.tostring (), 16));} File.deleteonexit ();} catch (IOException e) {e.printstacktrace ();} catch (Interruptedexception e) {e.printstacktrace ()}} /** * Recursively processing file directory * @param path */public static void Resolvedir (String path) {file File=new file (path); file[] fileList = File.listfiles (); for (int i = 0; i < filelist.length; i++) {if (Filelist[i].isdirectory ()) {Resolvedir ( Filelist[i].getabsolutepath ());} Else{if (Getfiletype (Filelist[i].getabsolutepath ()). Equalsignorecase (DEX)) {Resolvedex (Filelist[i]. GetAbsolutePath ());} else if (Getfiletype (Filelist[i].getabsolutepath ()). Equalsignorecase (JAR)) {Resolvejar (Filelist[i]. GetAbsolutePath ());}}}}

  

Count the number of methods in the JAR package Dex file

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.