34

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.

ajma
  • 12,106
  • 12
  • 71
  • 90
  • Wow. Sorry, I don't know the answer but I'm curious what the limit is? Did you hit it when the DEX compiler was running? – Simon Jun 13 '13 at 18:14
  • 2
    64k :) 65k would be a rather strange limit. – JesusFreke Jun 13 '13 at 23:31
  • 2
    how did you get the total method count? – gunar Mar 07 '14 at 07:27
  • 6
    http://inloop.github.io/apk-method-count/ – Jared Rummler May 15 '15 at 01:44
  • it is actually >64,000 though, 64k reference is just the name: "In the context of computer science, the term Kilo, K, denotes 1024 (or 2^10). Because 65,536 is equal to 64 X 1024, this limit is referred to as the '64K reference limit'" so you're right, but you must have known he was too :) – Saik Caskey Nov 14 '16 at 13:58

6 Answers6

45

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

Mihai Parparita
  • 4,236
  • 1
  • 23
  • 30
  • 1
    Yeah, my solution is more of the quick-and-dirty type. Although I might have to argue with you about the accuracy of it :). Can't argue about the speed though. – JesusFreke Jan 28 '15 at 00:00
  • 1
    @nadavfima Instructions to use it - 1) Download the source from GitHub: https://github.com/mihaip/dex-method-counts. 2) Extract the file in a directory. 3) Open terminal window and navigate to that directory. 4) Run the relevant commands given in Mihai's README https://github.com/mihaip/dex-method-counts/blob/master/README.md – Maurice Gavin Apr 21 '16 at 12:50
22

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" \;
JesusFreke
  • 19,784
  • 5
  • 65
  • 68
  • 5
    ...and if you're like me and don't yet have the smali tools, it's simple: go to https://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. – parkerfath Mar 10 '14 at 23:43
  • You can also use methodscount plugin in AS to see method count of specific dependency in build.gradle file. Or see number of methods on every build with Dexcount from mihaip (https://github.com/mihaip/dex-method-counts). Hope someone finds this useful. – Beemo Jan 04 '16 at 09:00
  • Beemo - that should be posted as an answer, not a comment on another answer :) – JesusFreke Jan 04 '16 at 17:52
17

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).

Nicola Miotto
  • 3,647
  • 2
  • 29
  • 43
13

Piling on to an old question, I've written a Gradle plugin that will report the number of methods in your APK on every build. It uses the same DEX parsing code that the AOSP project uses. You can find it at https://github.com/KeepSafe/dexcount-gradle-plugin.

Ben
  • 6,023
  • 1
  • 25
  • 40
8

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.

Stoycho Andreev
  • 6,163
  • 1
  • 25
  • 27
1

You can also try using https://github.com/KeepSafe/dexcount-gradle-plugin which allows you to see the method counts per package graphically through an html.

Mark Pazon
  • 6,167
  • 2
  • 34
  • 50