从 Android 项目中的包中检索类列表
我知道使用反射获取包中的所有类并不容易,但我想知道是否有人知道一个好的解决方案/解决方法,特别是针对 Android 项目?
给定一个包,我需要能够从中检索所有类并使用反射处理它们的注释。
有谁知道有什么方法可以做到这一点?有可用的图书馆吗?
I'm aware that it isn't easily feasible to get all of the classes in a package using reflection, but I'm wondering if someone knows of a good solution/workaround, specifically for an Android project?
Given a package, I need to be able to retrieve all of the classes from it and process annotations from them using reflection.
Does anyone know of a way to do this? Are there any libraries available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像大多数非 Android Java 解决方案一样扫描文件系统在 Android 上没有帮助。这是一个特定于 android 的(理论)解决方案: http:// mindtherobot.com/blog/737/android-hacks-scan-android-classpath/
然而,它仍然是一个 hack,因为不幸的是 Java 并不直接支持这一点。
Scanning the filesystem as most solutions for non-Android Java do won't help on Android. Here's a (theoretical) solution that is android-specific: http://mindtherobot.com/blog/737/android-hacks-scan-android-classpath/
However, it remains a hack, since Java unfortunately does not directly support this.
现有的依赖注入解决方案使用反射来处理注释,但仍然需要声明资源。请参阅 这个使用反射的 DI 示例。
如果您使用 Ant 构建工件,则可以使用 Bash 或 Java 读取源目录的内容,并使用它在每次构建期间自动重新生成完整的类层次结构。如果您严重依赖 Eclipse IDE,这可能会让事情变得棘手,因为在您运行另一个 Ant 构建之前该列表可能已经过时。 (注意:根据 Pyscho,您可以通过更改项目配置使 Eclipse 使用 Ant,请参阅注释)
另一种选择可能是使用 AssetManager 处理 AndroidManifest 文件,但您将仅限于声明的资源在该文件中。编译的类本身在classes.dex 文件中内联和优化,因此您不太可能从中获得很多有用的信息。
Existing dependency injection solutions use reflection for processing the annotations, but still need the resources to be declared. See this example of DI using reflection.
If you are using Ant to build your artifacts, you could read the contents of your source directory using Bash or Java, and use this to regenerate the full hierarchy of classes automatically during each build. This might make things tricky if you rely on heavily on the Eclipse IDE though, since the list might be out of date until you run another Ant build. (Note: according to Pyscho you can make Eclipse use Ant by altering the project configuration, see comments)
Another option might be to process the AndroidManifest file using the AssetManager, but you would be limited to the resources declared in that file. The compiled classes themselves are in-lined and optimised in the classes.dex file, and as such you're unlikely to get much useful information from it.
我想你可能会在这里找到答案 https://stackoverflow.com/a/1457971/1199538
附有一个 java 文件,因此您可以下载它并尝试
以下答案中的简短片段:
I think you might find the answer here https://stackoverflow.com/a/1457971/1199538
there is a java file attached so you can download it and try it
short snippet from the answer following:
您可以在编译时、在 JVM 字节码转换为 Dalvik 字节码之前对 Android 进行类路径扫描,例如使用 ClassGraph 库(我是作者):
https://github.com/classgraph/classgraph/wiki/Build-Time-Scanning
You can do classpath scanning for Android at compiletime, before the JVM bytecodes have been converted to Dalvik bytecodes, e.g. using the ClassGraph library (I am the author):
https://github.com/classgraph/classgraph/wiki/Build-Time-Scanning