在Android项目检索类的列表从包项目、列表、Android

2023-09-06 13:30:41 作者:罕见硬汉子i

我知道,这是不容易可行让所有使用反射在一个包中的类,但我不知道是否有人知道一个很好的解决方案/变通办法,专门为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?

推荐答案

现有的依赖注入的解决方案使用反射来处理注解,但仍然需要资源,需要申报。请参阅使用DI反射的这个例子。

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.

如果你正在使用Ant构建文物,你可以阅读使用bash或Java源目录中的内容,并使用此过程中的每个版本自动重新生成类的完整的层次。这可能使事情变得棘手,如果你依赖于大量的Eclipse IDE的,虽然,因为列表可能会过时,直到您运行另一个Ant构建。 (注:可根据Pyscho你可以使用Eclipse的Ant通过改变项目配置,看评论)的

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)

另一种选择可能是处理使用AssetManager的Andr​​oidManifest文件,但你将被限制在该文件中声明的资源。已编译的类本身的内衬和classes.dex文件优化的,因此你不可能从它那里得到很多有用的信息。

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.