Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 methods you are using at the moment in Android Studio?

like image 550
TomCB Avatar asked Jan 04 '15 14:01

TomCB


People also ask

How can I see all methods in Android Studio?

Open your file in editor and click on structure tab, it will show all the methods in the class selected. Click on any method and you will be redirected to that method. Show activity on this post. You can use CTRL + F12 shortcut in windows for that purpose.

Where is the method in Android Studio?

You may need to hold down the "fn" key while pressing a function key to actually use the function instead of the quick command. So for example, F8 will pause/play media, while fn+F8 will actually produce F8.

What is Dex count Android?

DEX specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code.


2 Answers

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

like image 128
Chris Knight Avatar answered Sep 22 '22 21:09

Chris Knight


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.

like image 42
Scott Barta Avatar answered Sep 22 '22 21:09

Scott Barta