反射库 returns Android 中的空列表

Reflections library returns empty list in Android

我正在尝试检索 Android 中带有特定注释的 class 列表。我正在尝试使用 Reflections library 来这样做。但无论我做什么,Reflections return 都是一个空集。最后,我尝试使用 Reflections.getAllTypes(),它应该 return all classes in a package,它给了我消息 "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 ,所以我查看了该方法的代码,如果包中没有 classes,它 return 就是那个错误。

这是我遇到错误的代码示例:

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

这至少应该 return 调用它的 class。我也试过为包使用空字符串,答案 here。知道是什么原因造成的吗?

我遇到了同样的问题。由于 Reflections 使用 java 辅助解析 .jar 文件中的 .class 文件,而 android 应用程序中不存在该文件,因此无法使用 Reflections。但是我们可以使用其他工具来解析 .dex 并且它会起作用