Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

directories support added #3

Merged
merged 4 commits into from Oct 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -5,12 +5,12 @@ Simple tool to output per-package method counts in an Android DEX executable gro
To run it with Ant:

$ ant jar
$ ./dex-method-counts path/to/App.apk # or .zip or .dex
$ ./dex-method-counts path/to/App.apk # or .zip or .dex or directory

or with Gradle:

$ ./gradlew assemble
$ ./dex-method-counts path/to/App.apk # or .zip or .dex
$ ./dex-method-counts path/to/App.apk # or .zip or .dex or directory

You'll see output of the form:

Expand All @@ -30,6 +30,7 @@ You'll see output of the form:
codebutler: 65
android_websockets: 65
...
Overall method count: 65490

Supported options are:

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Mon Jun 30 12:25:44 CEST 2014
#Mon Oct 13 19:48:24 MSK 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-1.12-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-1.12-bin.zip
2 changes: 2 additions & 0 deletions src/info/persistent/dex/DexMethodCounts.java
Expand Up @@ -29,6 +29,7 @@

public class DexMethodCounts {
private static final PrintStream out = System.out;
public static int overallCount = 0;

enum Filter {
ALL,
Expand All @@ -43,6 +44,7 @@ private static class Node {
void output(String indent) {
if (indent.length() == 0) {
out.println("<root>: " + count);
overallCount += count;
}
indent += " ";
for (String name : children.navigableKeySet()) {
Expand Down
47 changes: 36 additions & 11 deletions src/info/persistent/dex/Main.java
Expand Up @@ -22,10 +22,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Main {
private static final String CLASSES_DEX = "classes.dex";
Expand All @@ -34,7 +35,6 @@ public class Main {
private String packageFilter;
private int maxDepth = Integer.MAX_VALUE;
private DexMethodCounts.Filter filter = DexMethodCounts.Filter.ALL;
private String[] inputFileNames;

/**
* Entry point.
Expand All @@ -49,17 +49,17 @@ public static void main(String[] args) {
*/
void run(String[] args) {
try {
parseArgs(args);
boolean first = true;

for (String fileName : inputFileNames) {
String[] inputFileNames = parseArgs(args);
for (String fileName : collectFileNames(inputFileNames)) {
System.out.println("Processing " + fileName);
RandomAccessFile raf = openInputFile(fileName);
DexData dexData = new DexData(raf);
dexData.load();
DexMethodCounts.generate(
dexData, includeClasses, packageFilter, maxDepth, filter);
raf.close();
}
System.out.println("Overall method count: " + DexMethodCounts.overallCount);
} catch (UsageException ue) {
usage();
System.exit(2);
Expand Down Expand Up @@ -165,7 +165,7 @@ RandomAccessFile openInputFileAsZip(String fileName) throws IOException {
return raf;
}

void parseArgs(String[] args) {
String[] parseArgs(String[] args) {
int idx;

for (idx = 0; idx < args.length; idx++) {
Expand Down Expand Up @@ -195,19 +195,44 @@ void parseArgs(String[] args) {
if (fileCount == 0) {
throw new UsageException();
}

inputFileNames = new String[fileCount];
String[] inputFileNames = new String[fileCount];
System.arraycopy(args, idx, inputFileNames, 0, fileCount);
return inputFileNames;
}

void usage() {
System.err.print(
"DEX per-package/class method counts v1.0\n" +
"Usage: dex-method-counts [options] <file.{dex,apk,jar}> ...\n" +
"Usage: dex-method-counts [options] <file.{dex,apk,jar,directory}> ...\n" +
"Options:\n" +
" --include-classes\n" +
" --package-filter=com.foo.bar\n" +
" --max-depth=N\n");
" --max-depth=N\n"
);
}

/**
* Checks if input files array contain directories and
* adds it's contents to the file list if so.
* Otherwise just adds a file to the list.
*
* @return a List of file names to process
*/

private List<String> collectFileNames(String[] inputFileNames) {
List<String> fileNames = new ArrayList<String>();
for (String inputFileName : inputFileNames) {
File file = new File(inputFileName);
if (file.isDirectory()) {
String dirPath = file.getAbsolutePath();
for (String fileInDir: file.list()){
fileNames.add(dirPath + File.separator + fileInDir);
}
} else {
fileNames.add(inputFileName);
}
}
return fileNames;
}

private static class UsageException extends RuntimeException {}
Expand Down