Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Created July 16, 2013 00:03
Show Gist options
  • Save swankjesse/6004640 to your computer and use it in GitHub Desktop.
Save swankjesse/6004640 to your computer and use it in GitHub Desktop.
package com.android.dex;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
public class Summary {
public static void main(String[] args) throws IOException {
Dex dex = new Dex(new File(args[0]));
Map<String, AtomicInteger> packageToCount = new TreeMap<String, AtomicInteger>();
for (MethodId methodId : dex.methodIds()) {
String typeName = dex.typeNames().get(methodId.getDeclaringClassIndex());
String packageName = typeNameToPackageName(typeName);
AtomicInteger count = packageToCount.get(packageName);
if (count == null) {
count = new AtomicInteger();
packageToCount.put(packageName, count);
}
count.incrementAndGet();
}
for (Map.Entry<String, AtomicInteger> entry : packageToCount.entrySet()) {
System.out.printf("% 8d %s%n", entry.getValue().get(), entry.getKey());
}
}
private static String typeNameToPackageName(String typeName) {
if (typeName.startsWith("[")) typeName = typeName.substring(1); // arrays
if (!typeName.contains("/")) return "<default>"; // default package
return typeName.substring(1, typeName.lastIndexOf('/')).replaceAll("/", ".");
}
}
@swankjesse
Copy link
Author

You'll need dx.jar on your classpath.

@bubbleguuum
Copy link

There is no Dex class in dx.jar.

@matthewmichihara
Copy link

I'm not seeing Dex in dx.jar either. My dx.jar is from $ANDROID_HOME/build-tools/18.0.1/lib/ @swankjesse you sure its in dx.jar?

@jainsanyam
Copy link

Thanks for this code, works pretty well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment