2011-11-21 223 views
3

如何獲得我的.apk項目中包含的.dex文件的參考? 我試圖打開我的apk,並找到我在我的用戶空間(/data/data/<my apk name>/)中編寫的classes.dex,然後創建一個文件並創建一個dex,但我不能。任何建議或不同的方法?dexFile Android:無法打開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) 

回答

2

我知道這個題目是有點老了,但是我今天發現同樣的問題。我希望我的回答能在將來幫助別人。

試試這個代碼

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

這已經夠了DexFile創建一個實例。更容易,不是嗎? :)

+0

工程像魅力。 :D –

+0

可悲的是,我不喜歡用開瓶器的代碼 –