dexFile Android:无法打开 DEX 文件
如何获取 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这个话题有点老了,但是我今天发现它也有同样的问题。我希望我的回答对将来的人有所帮助。
试试这个代码
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
It is enough for DexFile to create an instance. Much easier, isn't it? :)