在Android包中查找所有类 [英] Find all classes in a package in Android

查看:76
本文介绍了在Android包中查找所有类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android的程序包中找到所有类?我使用PathClassLoader,但是它总是返回一个空的枚举?

How can i find all classes inside a package on Android? I use PathClassLoader, but it always returns an empty enumeration?

其他信息

尝试了建议的思考方法.关于反射库的几个要点.通过Maven Central可用的库与Android不兼容,并且会出现dex错误.我必须包含源代码并编译Java助手dom4j.

Tried the suggested Reflections approach. Couple of important points about reflections library. The library available through maven central is not compatible with Android and gives dex errors. I had to include source and compile dom4j, java-assist.

反射问题,我的原始解决方案是android中的PathClassLoader返回包的空枚举.

The problem with reflections, and my original solution is that PathClassLoader in android returns an empty enumeration for package.

方法的问题在于,Android中的getResource始终返回空的枚举.

The issue with approach is that getResource in Android is always returning empty enumeration.

final String resourceName = aClass.getName().replace(".", "/") + ".class";

for (ClassLoader classLoader : loaders) {
      try {
            final URL url = classLoader.getResource(resourceName);
            if (url != null) {
                final String normalizedUrl = url.toExternalForm().substring(0, url.toExternalForm().lastIndexOf(aClass.getPackage().getName().replace(".", "/")));
                return new URL(normalizedUrl);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
   }

推荐答案

使用DexFile列出apk中的所有类:

Using DexFile to list all classes in your apk:

    try {
        DexFile df = new DexFile(context.getPackageCodePath());
        for (Enumeration<String> iter = df.entries(); iter.hasMoreElements();) {
            String s = iter.nextElement();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

其余的都使用regexp或其他方法过滤掉所需的类.

the rest is using regexp or something else to filter out your desired class.

这篇关于在Android包中查找所有类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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