Reflections library returns empty list in Android

2019-07-10 07:25发布

问题:

I'm attempting to retrieve a list of classes with a specific annotation in Android. I'm trying to use the Reflections library to do so. But no matter what I do, Reflections returns an empty set. Finally, I tried using Reflections.getAllTypes(), which should return all classes in a package, and it gives me the message "Couldn't find subtypes of Object. Make sure SubTypesScanner initialized to include Object class - new SubTypesScanner(false)" I have verified that I am doing so, so I looked in the code for that method and it returns that error if there are no classes in the package.

Here's an example of the code where I'm getting the error:

Reflections reflections = new Reflections(this.getClass().getPackage().getName(), 
        new SubTypesScanner(false));
Set<String> classes = reflections.getAllTypes(); // Throws runtime error

This should, at the very least, return the class that it is called from. I've also tried using an empty string for the package, and the answer here. Any ideas what could be causing this?

回答1:

I met the same problem. Since Reflections use java assist to resolve .class file in .jar file, which isn't exist in android application, so it is not possible to use Reflections. However we could use other tool to resolve .dex and it would work