dexFile Android:无法打开 DEX 文件

发布于 2024-12-17 06:40:11 字数 2961 浏览 0 评论 0原文

如何获取 .apk 项目中包含的 .dex 文件的引用? 我尝试打开我的 apk 并找到我在用户空间中编写的 classes.dex (/data/data//),然后创建一个文件并创建 dex 但我不能。有什么建议或不同的方法吗?

String apkLocation = getApplication().getPackageCodePath();
String pName = getPackageName();
ZipFile zip = null;
ZipEntry zipen = null;
File dexF = null;
String libLocation = "/data/data/" + pName + "/" + "cl.dex";
try {
    zip = new ZipFile(apkLocation);
    zipen = zip.getEntry("classes.dex");
    InputStream is = zip.getInputStream(zipen);

    dexF = new File(libLocation);
    FileOutputStream os = new FileOutputStream(libLocation);
    byte[] buf = new byte[8092];
    int n;
    while ((n = is.read(buf)) > 0) os.write(buf, 0, n);
    os.close();
    is.close();

} catch (IOException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
}

DexFile dexFile = null;
try {
    dexFile = new DexFile("/data/data/" + pName + "/" + "cl");
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

这是堆栈跟踪:

11-21 15:07:55.894: W/System.err(490): java.io.IOException: unable to open DEX file
11-21 15:07:55.894: W/System.err(490):  at dalvik.system.DexFile.openDexFile(Native Method)
11-21 15:07:55.904: W/System.err(490):  at dalvik.system.DexFile.<init>(DexFile.java:80)
11-21 15:07:55.904: W/System.err(490):  at a.load.LoadClassActivity.addDex(LoadClassActivity.java:130)
11-21 15:07:55.904: W/System.err(490):  at a.load.LoadClassActivity.onCreate(LoadClassActivity.java:184)
11-21 15:07:55.914: W/System.err(490):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-21 15:07:55.914: W/System.err(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-21 15:07:55.925: W/System.err(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-21 15:07:55.925: W/System.err(490):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-21 15:07:55.934: W/System.err(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-21 15:07:55.944: W/System.err(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 15:07:55.944: W/System.err(490):  at android.os.Looper.loop(Looper.java:123)
11-21 15:07:55.944: W/System.err(490):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-21 15:07:55.954: W/System.err(490):  at java.lang.reflect.Method.invokeNative(Native Method)
11-21 15:07:55.954: W/System.err(490):  at java.lang.reflect.Method.invoke(Method.java:507)
11-21 15:07:55.964: W/System.err(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-21 15:07:55.964: W/System.err(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-21 15:07:55.964: W/System.err(490):  at dalvik.system.NativeStart.main(Native Method)

How can I get a reference of my .dex file contained in my .apk project?
I tried to open my apk and find classes.dex that I wrote in my user space (/data/data/<my apk name>/), then create a file and create a dex but I can't. Any suggestion or different approach?

String apkLocation = getApplication().getPackageCodePath();
String pName = getPackageName();
ZipFile zip = null;
ZipEntry zipen = null;
File dexF = null;
String libLocation = "/data/data/" + pName + "/" + "cl.dex";
try {
    zip = new ZipFile(apkLocation);
    zipen = zip.getEntry("classes.dex");
    InputStream is = zip.getInputStream(zipen);

    dexF = new File(libLocation);
    FileOutputStream os = new FileOutputStream(libLocation);
    byte[] buf = new byte[8092];
    int n;
    while ((n = is.read(buf)) > 0) os.write(buf, 0, n);
    os.close();
    is.close();

} catch (IOException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
}

DexFile dexFile = null;
try {
    dexFile = new DexFile("/data/data/" + pName + "/" + "cl");
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

Here's the stack trace:

11-21 15:07:55.894: W/System.err(490): java.io.IOException: unable to open DEX file
11-21 15:07:55.894: W/System.err(490):  at dalvik.system.DexFile.openDexFile(Native Method)
11-21 15:07:55.904: W/System.err(490):  at dalvik.system.DexFile.<init>(DexFile.java:80)
11-21 15:07:55.904: W/System.err(490):  at a.load.LoadClassActivity.addDex(LoadClassActivity.java:130)
11-21 15:07:55.904: W/System.err(490):  at a.load.LoadClassActivity.onCreate(LoadClassActivity.java:184)
11-21 15:07:55.914: W/System.err(490):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-21 15:07:55.914: W/System.err(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-21 15:07:55.925: W/System.err(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-21 15:07:55.925: W/System.err(490):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-21 15:07:55.934: W/System.err(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-21 15:07:55.944: W/System.err(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 15:07:55.944: W/System.err(490):  at android.os.Looper.loop(Looper.java:123)
11-21 15:07:55.944: W/System.err(490):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-21 15:07:55.954: W/System.err(490):  at java.lang.reflect.Method.invokeNative(Native Method)
11-21 15:07:55.954: W/System.err(490):  at java.lang.reflect.Method.invoke(Method.java:507)
11-21 15:07:55.964: W/System.err(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-21 15:07:55.964: W/System.err(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-21 15:07:55.964: W/System.err(490):  at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尐偏执 2024-12-24 06:40:11

我知道这个话题有点老了,但是我今天发现它也有同样的问题。我希望我的回答对将来的人有所帮助。

试试这个代码

    try {
        DexFile dexFile = new DexFile(apkLocation);
    } catch (IOException e) {
        e.printStackTrace();
    }

DexFile创建一个实例就足够了。容易多了,不是吗? :)

I know this topic is a bit old, however I found it today with the same problem. I hope my answer will help somebody in the future.

try this code

    try {
        DexFile dexFile = new DexFile(apkLocation);
    } catch (IOException e) {
        e.printStackTrace();
    }

It is enough for DexFile to create an instance. Much easier, isn't it? :)

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