有没有办法获取给定的classes.dex 文件中的类名?

2024-04-01

我正在构建一个家庭自动化应用程序。我正在尝试添加一个插件系统。作为测试,我将测试类(Button 的子类)导出为 APK 文件,并将其放入我的应用程序的文件目录中。我能够创建此类的新实例并将其放入我的视图中使用DexClassLoader and .loadClass.

下一步是扫描该目录中的所有 APK 并获取其中的类名称。

我发现 DexFile 类可以做到这一点,但是它会抛出以下异常:

04-18 17:26:15.697: E/dalvikvm(726): Can't open dex cache '/data/dalvik-cache/data@[email protected] /cdn-cgi/l/email-protection@[email protected] /cdn-cgi/l/email-protection@classes.dex': No such file or directory

04-18 17:26:15.705: I/dalvikvm(726): Unable to open or create cache for /data/data/com.strutton.android.testplugin/files/testloadclass.apk (/data/dalvik-cache/data@[email protected] /cdn-cgi/l/email-protection@[email protected] /cdn-cgi/l/email-protection@classes.dex)

它似乎试图在系统缓存中找到优化的DexFile,但我没有缓存目录的权限。据我所知,这可能是设计使然,我对此没有任何问题。是否有另一种方法来解析 DEX 文件并从中获取类名?

我查看了一些 dex 反编译器项目的源代码。我必须推出自己的解决方案吗?

这是我的测试应用程序代码(来自我的 Activity OnCreate),以防万一我错过了某些内容:

    try {
        ArrayList<String> UIPlugins = new ArrayList<String>();
        final File filesDir = this.getFilesDir();
        final File tmpDir = getDir("dex", 0);
        final DexClassLoader classloader = new DexClassLoader( filesDir.getAbsolutePath()+"/testloadclass.apk",
                tmpDir.getAbsolutePath(),
                null, this.getClass().getClassLoader());
        final Class<Button> classToLoad = 
                (Class<Button>) classloader.loadClass("com.strutton.android.testloadclass.MyTestClass_IRDroidUIPlugIn");
        Button mybutton = classToLoad.getDeclaredConstructor(Context.class).newInstance(this);
        mybutton.setId(2);
        mybutton.setOnClickListener(this);
        main.addView(mybutton);
        // Up to here everything works as expected
        // This line throws the exceptions
        DexFile mDexFile = new DexFile(filesDir.getAbsolutePath()+"/testloadclass.apk";);
        Enumeration<String> classNames = mDexFile.entries();
        while (classNames.hasMoreElements()){
            String className = classNames.nextElement();
            if (className.endsWith("IRDroidUIPlugIn")){
                UIPlugins.add(className);
            }
        }
        final Class<Button> classToLoad = 
                (Class<Button>) classloader.loadClass("com.strutton.android.testloadclass.MyTestClass_IRDroidUIPlugIn");
        Button mybutton = classToLoad.getDeclaredConstructor(Context.class).newInstance(this);
        mybutton.setId(2);
        mybutton.setOnClickListener(this);
        main.addView(mybutton);
        btn.setText(tmpDir.getAbsolutePath());
      } catch (Exception e) {
        e.printStackTrace();
    }

代替:

DexFile mDexFile = new DexFile(filesDir.getAbsolutePath()+"/testloadclass.apk";);

try:

DexFile mDexFile = DexFile.loadDex(filesDir.getAbsolutePath()+"/testloadclass.apk", tmpDir.getAbsolutePath()+"/testloadclass.odex", 0);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法获取给定的classes.dex 文件中的类名? 的相关文章

随机推荐

  • 为什么在 initWIthCoder:(NSCoder *)aDecoder 中调用时未设置我的 UIView 图层属性?

    我构建了一个自定义 View 类 它是 UIView 的子类 该视图通过 ViewController 中的 nib 文件加载 NSBundle main loadNibNamed MyCustomView owner self optio
  • CUnit - “模拟”libc 函数

    我正在使用 CUnit 进行项目单元测试 我需要测试是否使用正确的参数调用 libc 函数以及是否以正确的方式处理它们的返回值 例如 如果我调用 bind 函数 我想检查我传递的 af 参数并断言如果这是错误的 并且我还想模拟它的返回值并断
  • Google OAuth2 授权 OAuth 令牌错误:redirect_uri_mismatch

    我正在根据此网页创建并授权 OAuth 令牌 https code google com p google mail oauth2 tools wiki OAuth2DotPyRunThrough https code google com
  • 找到最小正值

    从固定数量 在本例中为 3 个值中找到最小非零正值或在没有正问题时返回 0 的最佳算法是什么 我的天真的方法如下 在Delphi中 但请随意使用您喜欢的任何方法 但我认为有一种更优雅的方法 value1Temp MaxInt value2T
  • 将多个源 ArrayList 同步到单个目标列表

    需要从两个不同的表加载项目列表 ArrayList 由于 Hibernate 映射复杂 它们无法作为一个 ArrayList 加载 所以它们是由类定义中的两个不同的Hibernate包分别加载的 当我使用它们时 我想合并成一个列表 所以我正
  • 仅在调试模式下使用特定的 minSdkVersion

    如何仅在调试模式下使用特定的 minSdkVersion 我想使用 minSdkVersion 21 进行调试模式 但使用 minSdkVersion 15 进行发布 我不想为此使用香料 因为会带来麻烦 我认为可能是这样的 但不起作用 de
  • 你能在 Perl 中强制刷新输出吗?

    我在 Perl 中有以下两行 print Warning this will overwrite existing files Continue y N n my input
  • 将 SAFEARRAY 从 C++ 返回到 C#

    我有一个 C 方法 可以创建 填充并返回 SAFEARRAY SAFEARRAY TestClass GetResult long size return GetSafeArrayList size How should I export
  • Lua中运算符~=是什么意思?

    什么是 Lua中的运算符是什么意思 例如 在以下代码中 if x params then the is not equals 这在其他语言中是等价的
  • 尝试在Flash AS3.0中使用BindingUtils

    我无法使此代码在包含 Flex SDK 4 0 的 AS3 0 Flash 中工作 import mx binding utils Bindable var myValue int 0 var cw ChangeWatcher Bindin
  • 如何手动向已在 mechanize 中设置了 cookie 的会话添加更多 cookie?

    我有一个 python 脚本 它抓取页面并接收 cookie 我想将另一个 cookie 附加到发送到服务器的现有 cookie 中 这样 在下一个请求中 我将获得原始页面中的 cookie 以及我手动设置的 cookie 无论如何要这样做
  • 更改 axios 的默认基本 url

    我已经像这样配置了我的 axios const axiosConfig baseURL http 127 0 0 1 8000 api timeout 30000 Vue prototype axios axios create axios
  • Android 滚动时如何在 RecyclerView 中加载更多数据

    我想为一个网站开发android应用程序 我阅读了以下网站的帖子json并显示其在RecyclerView每 10 个帖子以及当用户滚动时RecyclerView显示更多 10 条帖子并结束 我是业余爱好者 我写了下面的代码 但我不知道滚动
  • 如何在 blob 中计算情感分析

    我使用以下方法计算 200 个短句子的情绪 我没有使用训练数据集 for sentence in textblob sentences print sentence sentiment 分析返回两个值 极性和主观性 根据我在网上读到的内容
  • Google 文本转语音 API 音调调整

    如何在此代码中将音调调整为 1 20 from google cloud import texttospeech def text to wav voice name text language code join voice name s
  • 单击带有最新更新的 EditText 时应用程序崩溃(gradle 4.4 - android studio 3.1)

    我最近将我的 android studio 更新到版本 3 1 并将我的 gradle 更新到版本 4 4 从那时起 我一直面临着应用程序在点击某个按钮时进入 ANR 的问题EditText这应该会弹出一个软输入键盘 单击EditText我
  • Ajax 调用不会更新我的行

    我想用字符的 id 更新我的数据库 但是当我将它们放入插槽中时 它不会更新我想要更新的行 我的问题是 你能为我指明如何正确编码或修复我的错误的正确方向吗 function updateTeam var team slot if input
  • 在.Net 中使用 Scala 有什么好处?

    Scala 是一种特殊的编程语言 因为它同时针对 JVM 和 CLR 但有什么好处呢 是否值得将其视为 F 语言的可行替代方案 NET 上的 Scala 是 Miguel Garcia 领导的一项持续努力 最新状态是我们几乎能够在 NET
  • While 循环重置 Bash 脚本中的数字变量[重复]

    这个问题在这里已经有答案了 我正在尝试编写一个简单的 bash 脚本来在一组文件夹中的每个文件中执行某些操作 我还喜欢计算脚本读取了多少个文件 但是当脚本通过循环时 数值变量会被重置 我正在使用的代码是这样的 bin bash let AU
  • 有没有办法获取给定的classes.dex 文件中的类名?

    我正在构建一个家庭自动化应用程序 我正在尝试添加一个插件系统 作为测试 我将测试类 Button 的子类 导出为 APK 文件 并将其放入我的应用程序的文件目录中 我能够创建此类的新实例并将其放入我的视图中使用DexClassLoader