It looks like I'm flirting with the dex limit of 65K, if I write some new methods I have a dex error, if I remove some older things, it's gone.
Is there a way to check how many methods you are using at the moment in Android Studio?
It looks like I'm flirting with the dex limit of 65K, if I write some new methods I have a dex error, if I remove some older things, it's gone.
Is there a way to check how many methods you are using at the moment in Android Studio?
Android Studio provides a built in way to do this. Change to the 'Project' view, navigate to /app/build/outputs/apk/path to flavor/type apk. Double click on the APK. This will launch the apk in the editor where you can see the various libraries and their associated sizes in the top half of the window. Scroll down in this window until you find classes.dex (or if you have more than one, classes2.dex, etc.). Click on this and in the bottom of the window you will see method references by package. You can also use this view to analyse the constructed Android Manifest if you work with multiple flavors or build types. More info can be found on the android developer site
I can find a fragile way to do it, which is maybe better than no way at all. Copy and paste the following to the bottom of your module's build.gradle file, replacing ANDROID_HOME
with the path of your Android SDK installation and BUILD_TOOLS_VERSION
with the same version specified in the buildToolsVersion
spec of your android
block:
buildscript {
dependencies {
classpath files("/Users/sbarta/sdk/build-tools/21.0.2/lib/dx.jar")
}
}
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
variant.assemble.doLast {
// Show the dex count after the assemble task is finished
showDexCount(
[filename: output.outputFile],
)
}
}
}
def showDexCount(Map... files) {
def maxReferences = (int) Math.pow(2, 16);
def buffer = 5000 // that's for safety, because you can't burn maxReferences
println "\n\n***********************************************************************************"
println "* DEX COUNTS *"
println "***********************************************************************************"
files.each {
def dex = new com.android.dex.Dex(it.filename)
def count = dex.tableOfContents.methodIds.size
if ((maxReferences - count - buffer) >= 0)
println String.format('* %1$5d (there are still %2$5d references to burn...) *',
count, maxReferences - count - buffer)
else
println String.format('* %1$5d !!!WARNING!!! Too many references, please decrease by %2$4d! *',
count, -(maxReferences - count - buffer))
}
println "***********************************************************************************\n"
}
This loads up the dex code itself to evaluate the dex files and count the number of methods; it adds its work to the end of the assemble
task in the build script, so you'll see it in command line builds or if you actually run it from Android Studio (where it will show up in the Gradle console).
I tried to make it more resilient and use the ANDROID_HOME environment variable instead of requiring you to hardcode the path, but using environment variables when building from Android Studio is problematic (it works from the command line though). Similarly, I tried to have it pull in the build tools version from the other place in the build script where it's being referenced, and I also tried defining a global constant, but couldn't make the scoping and order of execution work. If someone can improve on this, please comment or edit the answer.
This is adapted from something written by Carlos Sobrinho; I can't find a Web-accessible reference to the original.
I used two different tools to find number of methods:
1) Simple tool that counts methods and shows amount of methods in different packages: https://github.com/mihaip/dex-method-counts
It will show you simple data like this:
Read in 65490 method IDs.
<root>: 65490
: 3
android: 6837
accessibilityservice: 6
bluetooth: 2
content: 248
pm: 22
res: 45
...
com: 53881
adjust: 283
sdk: 283
codebutler: 65
android_websockets: 65
...
Overall method count: 65490
2) And here is another tool that is even easier to use, you have to add a dependency to your project and it will show you methods on each build. You can simply comment this dependency line when you are not needing it: https://github.com/KeepSafe/dexcount-gradle-plugin
methods fields package/class name
5037 1103 android.support.v4
29 1 android.support.v4.accessibilityservice
57 16 android.support.v4.animation
https://developer.android.com/studio/build/apk-analyzer.html#view_dex_files
APK Analyzer's DEX file viewer gives you immediate access to the underlying information in the DEX file(s) in your app. Class, package, total reference and declaration counts are provided within the viewer, which can assist in deciding whether to use multi-DEX or how to remove dependencies to get below the 64k DEX limit.
Figure 4 depicts a medium-size app approaching the 64k DEX limit.