Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 205
ajma Avatar asked Jun 13 '13 18:06

ajma


5 Answers

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

like image 113
Mihai Parparita Avatar answered Nov 10 '22 21:11

Mihai Parparita


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" \;
like image 30
JesusFreke Avatar answered Nov 10 '22 22:11

JesusFreke


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

like image 17
Nicola Miotto Avatar answered Nov 10 '22 22:11

Nicola Miotto


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.

like image 13
Ben Avatar answered Nov 10 '22 22:11

Ben


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.

like image 8
Stoycho Andreev Avatar answered Nov 10 '22 23:11

Stoycho Andreev