从 Android 项目中的包中检索类列表

发布于 2025-01-04 13:37:53 字数 144 浏览 0 评论 0原文

我知道使用反射获取包中的所有类并不容易,但我想知道是否有人知道一个好的解决方案/解决方法,特别是针对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

む无字情书 2025-01-11 13:37:53

像大多数非 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.

避讳 2025-01-11 13:37:53

现有的依赖注入解决方案使用反射来处理注释,但仍然需要声明资源。请参阅 这个使用反射的 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.

千年*琉璃梦 2025-01-11 13:37:53

我想你可能会在这里找到答案 https://stackoverflow.com/a/1457971/1199538
附有一个 java 文件,因此您可以下载它并尝试

以下答案中的简短片段:

此方法只能在以下情况下使用:

您有一个类位于您想要发现的同一个包中,该类称为
种子类。例如,如果您想列出“java.io”中的所有类,种子类可能是 java.io.File。

您的类位于目录或 JAR 文件中,它具有源文件信息(不是源代码文件,而只是源文件)。据我所尝试,除了 JVM 类(这些类是 JVM 附带的)之外,它几乎 100% 工作。

您的程序必须有权访问这些类的 ProtectionDomain。如果你的程序是本地加载的,应该没有问题。

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:

This method can only be used when:

You have a class that is in the same package you want to discover, This class is called a
SeedClass. For example, if you want to list all classes in 'java.io', the seed class may be java.io.File.

Your classes are in a directory or in a JAR file it has source file information (not source code file, but just source file). As far as I've tried, it work almost 100% except the JVM class (those classes come with the JVM).

Your program must have permission to access ProtectionDomain of those classes. If your program is loaded locally, there should be no problem.

∞梦里开花 2025-01-11 13:37:53

您可以在编译时、在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文