user1038712
user1038712

Reputation: 41

dexFile Android: unable to open DEX file

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)

Upvotes: 4

Views: 6068

Answers (1)

Ruzard
Ruzard

Reputation: 1359

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? :)

Upvotes: 2

Related Questions