• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Showing dex method count by package

I'm working on android app that's running up against the dex method count limit. Is there a simple way to show the method count grouped by package? I can get the total method count, but my app has multiple components and I'm trying to figure out which component is the biggest contributor to this.

I've written a dex-method-counts tool that outputs per-package method counts faster and more accurately than the smali-based tools referenced in JesusFreke's answer¹. It can be installed from https://github.com/mihaip/dex-method-counts.

[1] that script disassembles the .dex and re-assembles it by package, but this means that methods that are referenced by multiple packages are counted twice

  • 45
Reply Report

This will give you an idea of how much each package hierarchy contributes to the method count. The results include all classes in that directory/package and all subdirectories/packages.

baksmali blah.apk -o out
cd out
find * -type d -print -exec sh -c "smali {} -o {}/classes.dex && sh -c \"dexdump -f {}/classes.dex | grep method_ids_size\"" \;

This slightly modified version is similar, except that it is only for the classes in that particular directory/package, not including any subdirs/subpackages

baksmali blah.apk -o out
cd out
find * -type d -print0 | xargs -0 -I{} sh -c "echo {} && find {} -maxdepth 1 -name \"*.smali\" -print0 | xargs -r -0 smali -o {}/classes.dex"
find -name "*.dex" -print -exec sh -c "dexdump -f {} | grep method_ids_size" \;
  • 21
Reply Report
      • 1
    • ...and if you're like me and don't yet have the smali tools, it's simple: go to code.google.com/p/smali, find the binary downloads (currently a bitbucket link), download the smali and baksmali JARs, rename them to smali.jar and baksmali.jar, download the wrapper scripts, chmod them to be executable, and you're done, more or less.

I and a collegue published the last month a service that can help you with your purpose: http://www.methodscount.com/. It shows the methods count and other neat information

enter image description here

Under the hood we use gradlew to get the dependencies and the Android SDK tool dx to calculate the dex information. We apply the procedure recursively to the library and its dependencies. We also persist the results for the already computed libraries so to speed up the process on consequent requests.

If you use Android Studio or IntelliJ, you can just install the plugin to have all the information as an handy hint (the plugin uses the service as well).

  • 17
Reply Report
      • 1
    • Hi Nicola , is there any chance to see the total number of methods use in my package? I can see every single dependency methods count but I can't see any total to my package. Your plugin is awesome guys +1
      • 1
    • Hi @Sniper, the feature is not there yet, but we may implement it in the future. Thanks for the feedback btw :)

I appreciated the work off all developers above and I am always happy to have such great community, but since Android studio 2.2 Google team has released APK Analyzer -> https://developer.android.com/studio/build/apk-analyzer.html

Now we all have build in tool to analyze out methods count, our resources size and even we can compare two .apk files agains each other. Finally we all have this tool build in. Android development is coming to be easier and easier.

BTW: To find how much methods you have, open your .apk file in Android Studio (support 2.2+ only) and click on the classes.dex file. After that you will be able to see the methods count in all libraries and in your project as well.

  • 8
Reply Report