如何获取.dex
项目中包含的.apk
文件的引用?
我试图打开我的apk并找到我在用户空间(classes.dex
)中写的/data/data/<my apk name>/
,然后创建一个文件并创建一个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)
答案 0 :(得分:2)
我知道这个话题有点陈旧,但我今天发现它有同样的问题。我希望我的回答能在将来帮助某人。
试试这段代码
try {
DexFile dexFile = new DexFile(apkLocation);
} catch (IOException e) {
e.printStackTrace();
}
DexFile足以创建一个实例。更容易,不是吗? :)