Skip to content Skip to sidebar Skip to footer

Check How Many Methods You Have 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 met

Solution 1:

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

enter image description here

Solution 2:

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) {
    defmaxReferences= (int) Math.pow(2, 16);
    defbuffer=5000// that's for safety, because you can't burn maxReferences

    println "\n\n***********************************************************************************"
    println "* DEX COUNTS                                                                      *"
    println "***********************************************************************************"
    files.each {
        defdex=newcom.android.dex.Dex(it.filename)
        defcount= 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.

Solution 3:

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:

Readin65490methodIDs.<root>:65490:3android:6837accessibilityservice:6bluetooth:2content:248pm:22res:45...com:53881adjust:283sdk:283codebutler:65android_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/classname50371103     android.support.v4
291        android.support.v4.accessibilityservice
5716       android.support.v4.animation

Solution 4:

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.

Post a Comment for "Check How Many Methods You Have In Android Studio"