How do we count the number of lines of code in a Library file.
For eg, a Jar or AAR.
Note - CLOC is an awesome tool, but unfortunately, it doesn't process ".class" files.
Converting JAR -> DEX and decompile DEX -> code, is one way of doing it, but the precision could be lost during the conversion and decompilation.
In some cases, you might be able to get a rough idea idea of the number of lines, using the debug information in the dex file.
Using dexlib2, you could do something like:
public static void main(String[] args) throws IOException {
DexFile dexFile = DexFileFactory.loadDexFile(args[0], 15);
long lineCount = 0;
for (ClassDef classDef: dexFile.getClasses()) {
for (Method method: classDef.getMethods()) {
MethodImplementation impl = method.getImplementation();
if (impl != null) {
for (DebugItem debugItem: impl.getDebugItems()) {
if (debugItem.getDebugItemType() == DebugItemType.LINE_NUMBER) {
lineCount++;
}
}
}
}
}
System.out.println(String.format("%d lines", lineCount));
}
An alternative metric for comparing code size might be the number of instructions in a dex file. e.g.
public static void main(String[] args) throws IOException {
DexFile dexFile = DexFileFactory.loadDexFile(args[0], 15);
long instructionCount = 0;
for (ClassDef classDef: dexFile.getClasses()) {
for (Method method: classDef.getMethods()) {
MethodImplementation impl = method.getImplementation();
if (impl != null) {
for (Instruction instruction: impl.getInstructions()) {
instructionCount++;
}
}
}
}
System.out.println(String.format("%d instructions", instructionCount));
}