反射库在 Android 中返回空列表 [英] Reflections library returns empty list in Android

查看:123
本文介绍了反射库在 Android 中返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 中检索具有特定注释的类列表.我正在尝试使用 Reflections 库 来做到这一点.但是无论我做什么,Reflections 都会返回一个空集.最后,我尝试使用 Reflections.getAllTypes(),它应该返回一个包中的所有类,它给了我消息找不到对象的子类型.确保初始化 SubTypesScanner 以包含对象类 - new SubTypesScanner(false)"我已经验证我正在这样做,所以我查看了该方法的代码,如果包中没有类,它会返回该错误.

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?

推荐答案

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

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

这篇关于反射库在 Android 中返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆