Is there a way to get count of number methods used in a jar file

前端 未结 12 2164
粉色の甜心
粉色の甜心 2020-11-28 03:20

Can i have the count of all methods used in a jar file . My APK uses certain external JARS and there are a number of classes around hundred to be precise.

I have use

相关标签:
12条回答
  • 2020-11-28 03:49

    I wrote a script based on @JesusFreke's answer for this problem and for another problem (https://stackoverflow.com/a/21485222/6643139):

    #!/bin/bash
    unzip $1 classes.jar -d aarsize &&
      dx --dex --output=aarsize/temp.dex aarsize/classes.jar &&
      hexdump -s 88 -n 4 -e '1/4 "%d\n"' aarsize/temp.dex &&
      rm -rf aarsize
    

    Save it to a file name "aarsize", chmod +x for it, output of aarsize your_aar_file :

    $ aarsize status-bar-compat-release.aar 
    Archive:  status-bar-compat-release.aar
      inflating: aarsize/classes.jar     
    91
    

    The 91 is the method counts of your aar file.

    0 讨论(0)
  • 2020-11-28 03:50

    You can convert the jar to a dex file, and then pull the number of method references out of the header. It is stored as an unsigned little endian integer, at offset 88 (0x58).

    dx --dex --output=temp.dex orig.jar
    cat temp.dex | head -c 92 | tail -c 4 | hexdump -e '1/4 "%d\n"'
    

    Keep in mind that this is the number of unique methods referenced, not the number of method references. In other words, if a particular method is referenced twice in the dex file, it will only be counted once in the count in the header. And when you import this jar into your apk, the method references that are common between the two are deduplicated, so the total method reference count of the final merged apk will be <= the sum of the two.

    0 讨论(0)
  • 2020-11-28 03:50

    Use the http://github.com/mihaip/dex-method-counts for calculationg number of methods from everywhere (JAR, AAR, DEX, APK)

    DEX or APK:

    ./dex-method-counts <your_file_path>.DEX or ./dex-method-counts <your_file_path>.APK

    JAR

    just make a DEX from JAR as it was shown above like this:

    <your_path_to_android_buil_tools>/dx --dex --output=temp.dex orig.jar

    and then

    ./dex-method-counts temp.dex

    AAR

    Firstly unzip it (yeah AAR it is ZIP actually) and then use classes.jar as it shown above in JAR section

    unzip mylib.aar -d mylib

    dx --dex --output=temp.dex mylib/classes.jar

    dex-method-counts temp.dex

    0 讨论(0)
  • 2020-11-28 03:51

    To find methods count in gradle library, you can use this - http://www.methodscount.com

    It also provides plugin for android studio.

    http://www.methodscount.com/plugins

    0 讨论(0)
  • 2020-11-28 03:53

    I just wrote a python script for this to get a rough estimate

    import re
    import subprocess
    import sys
    
    for jarfile in sys.argv[1:]:
        class_output = subprocess.check_output(['jar', 'tf', jarfile])
        classes = [c.replace('/', '.') for c in re.findall(r'(.*)\.class', class_output)]
        methods = []
        if classes:
            def_out = subprocess.check_output(['javap', '-classpath', jarfile] + classes)
            # This is pretty hacky: look for parentheses in the declaration line.
            num_methods = sum(1 for line in def_out if '(' in line)
        else:
            num_methods = 0
        print '{} {} {}'.format(num_methods, len(classes), jarfile)
    

    I had just run into the Android 64K method limit, and my dependencies weren't indexed by the methodcount.com service yet.

    0 讨论(0)
  • 2020-11-28 03:54
    1. Use the jar program with the -x parameter to extract the files from your jar file.

    2. Apply a decompiler to each .class file to get the number of methods in each file.

    3. Add up the method counts.

    0 讨论(0)
提交回复
热议问题