Kevin Parker
Kevin Parker

Reputation: 17216

Android - get loaded classes / reflection to get classes in a package?

Does anyone know how to get a list of classes in an Android package (from within an Android application) or how to use reflection that will work with the Dalvik VM to retrieve the class names from a given package?

Upvotes: 0

Views: 1037

Answers (1)

Kevin Parker
Kevin Parker

Reputation: 17216

So for Android we can use the DexFile class to enumerate over the visible classes in a given APK.

        try {
            DexFile dexFile = new DexFile(new File("/data/app/com.uxpsystems.cepclient-2.apk"));
            Enumeration<String> enumeration = dexFile.entries();

            if (enumeration.hasMoreElements() == false){
                Logger.d(LOG_TAG, "--> Enumeration has no elements");
            }

            while (enumeration.hasMoreElements()){
                String className = enumeration.nextElement();

                if (className.substring(0, 18).equals("com.somecompany.aproduct")){
                    Logger.d(LOG_TAG, "--> Enumeration: " + className); 
                }else{
//                  Logger.d(LOG_TAG, "--> Failed match: " + className.substring(0, 18));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Upvotes: 2

Related Questions