Introducing dexinfo Gradle plugin

This was originally published on the Mutual Mobile blog. I’m re-posting it here for posterity.

As applications are getting increasingly complex, more and more Android developers are running into the DEX 64K limit. I covered this topic–and how to work your way around it–in a previous post, so make sure you read it if you haven’t done so already.

Most developers don’t worry about this limit while working on their apps, and then end up spending a lot of time and effort to resolve it. We believe that prevention is better than finding a cure. Before you choose to bring a cool library into your code base, one of the things you should seriously consider is how many methods it adds to your application.

To date, we’ve had multiple ways of counting the methods in an application, like this one by Jake Wharton and this one by Mihai Parparita. However, my colleague, James Ayvaz and I thought we can make things easier by providing this functionality in the form of a Gradle plugin that you can simply integrate into your application. Today, we are happy to announce the plugin is publicly available!

How to use it

Add the dexinfo plugin in your app’s build.gradle file -

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.mutualmobile.gradle.plugins:dexinfo:0.1.2'
    }
}

Apply the plugin after the com.android.application or com.android.library plugins -

apply plugin: 'com.android.application'
apply plugin: 'com.mutualmobile.gradle.plugins.dexinfo'

Build your apk

$ ./gradlew -q assembleDebug

Run the dexinfo task

$ ./gradlew -q dexinfoDebug

Sample output

$ ../gradlew dexinfoDebug
:example:dexinfoDebug
Processing /Users/foo/git/example/build/intermediates/dex/devServer/debug/classes.dex
Read in 38163 method IDs.
<root>: 182
   butterknife: 182
      ButterKnife: 48
         Action: 1
         Finder: 22
            1: 3
            2: 3
            3: 3
         Finder[]: 1
         Injector: 2
      ImmutableList: 4
      InjectView: 1
      InjectViews: 1
      OnCheckedChanged: 1
      OnClick: 1
         ...
      internal: 99
         Binding: 1
         ButterKnifeProcessor: 22
         CollectionBinding: 12
            Kind: 5
            Kind[]: 1
         DebouncingOnClickListener: 7
            1: 2
         ListenerBinding: 5
         ListenerClass: 11
            NONE: 4
            NONE[]: 1
         ListenerMethod: 4
         Parameter: 5
         ViewBinding: 6
         ViewInjection: 8
         ViewInjector: 18
            1: 1

Overall method count: 182

BUILD SUCCESSFUL

You can also specify various options like how far into package paths the count should be reported for, get the count for a specific package, get count only for referenced methods, etc. We’ve been using this internally for some time now and have already saved a lot of time in the process, we hope you’ll find it useful too.

Check it out on GitHub for more information, and remember that pull requests are always welcome!