1 year ago

#15996

test-img

Brad Lendon

Reading classes present in an android app from its apk file

Recently I came across this app known as Warden that reads the classes.dex file of all apps installed in your phone and compares it to a list of trackers/loggers to determine the trackers installed in each app. After digging around the source code I found the following code snippet.

final Uri uri = Uri.fromFile(new File(packageInfo.applicationInfo.sourceDir));
File classFile = null;
File optimizedFile = null;

        try {
            final InputStream inputStream = context.getContentResolver().openInputStream(uri);

            classFile = File.createTempFile("classes" + Thread.currentThread().getId(),
                    ".dex",
                    context.getCacheDir());

            optimizedFile = File.createTempFile("opt" + Thread.currentThread().getId(),
                    ".dex",
                    context.getCacheDir());

            FileUtils.copyInputStreamToFile(inputStream, classFile);

            final DexFile dexFile = DexFile.loadDex(classFile.getPath(), optimizedFile.getPath(), 0);

            for (Enumeration<String> classNames = dexFile.entries(); classNames.hasMoreElements(); ) {
                final String className = classNames.nextElement();
                if (className.length() >= 8 && className.contains("."))
                    classNameList.add(className);
            }

(code snippet taken from https://github.com/whyorean/Warden)

From what I understand, the app creates a temproary dex file and reads it using the LoadDex method.

What I don't understand is that the inputstream (1st line of the try block) is of the entire apk file, which (as far as I know) contains other files along with the classes.dex file, if that is the case how is the apk file directly being copied to a .dex file ?

I tried googling a bit and found that most answers use zipping and manually loading the "classes.dex" file, so it would be helpful if someone can point to some documentation/reference which explains the above approach in a detailed manner

java

android

dex

0 Answers

Your Answer

Accepted video resources