1. 程式人生 > >將jar檔案與dex檔案的轉換

將jar檔案與dex檔案的轉換

一、dex檔案轉為jar檔案

1.第一篇

Android 反編譯資料整理

Made by 李文棟  [email protected]

2010-12-13  Monday 於北京

一、反編譯流程圖

                 

二、工具使用方法(命令)

準備工作

假設我的工作目錄為 $AndroidDecompile,首先要將system.img中(或者說從原始碼中編譯好的)幾個重要的odex檔案拷貝到工作目錄中,他們是:core.odex, ext.odex, framework.odex, android.policy.odex, services.odex(也可以放在別的目錄,通過設定BOOTCLASSPATH指定,預設就是當前目錄,關於BOOTCLASSPATH請參考baksmali的幫助資訊)。

下載以下工具到 $AndroidDecompile中:

Baksmali :

Smali :

Dex2jar :

JD-GUI (Java Decompile GUI) :

AutoSign :

Apktool

假設我們有一個應用,它的類檔案編譯後被單獨拿了出來,即有兩個檔案app.apk和app.odex,把他們放在$AndroidDecompile下。

1. 使用 baksmali.jar 將 odex 檔案分解為 smali 檔案

$ java –jar baksmali-1.2.5.jar –x app.odex

如果成功的話,會在 $AndroidDecompile下生成一個 out目錄,裡面是一些以“.smali”為字尾名的檔案,在此不深究這些檔案的作用。

2. 使用 smali.jar將 out/目錄下的smali檔案轉換為 classes.dex

$ java -Xmx512M –jar smali-1.2.5.jar out –o classes.dex

classes.dex便是Dalvik VM所使用的編譯後的類檔案格式,在正常的apk檔案裡都會有。

3. 使用 dex2jar將classes.dex反編譯為jar檔案

將下載後的dex2jar壓縮包解壓後,裡面會有dex2jar.sh(和dex2jar.bat)檔案,假如classes.dex檔案與dex2jar.sh在同一目錄下,使用以下方式將classes.dex反編譯為jar檔案:

$dex2jar.sh classes.dex

如果執行成功,則會在當前目錄下生成反編譯後的檔案classes.dex.dex2jar.jar。

dex2jar即可以操作dex檔案,也可以直接操作apk檔案,它的使用規則為:

dex2jar file1.dexORapk file2.dexORapk ...

4. 使用JD-GUI檢視反編譯後的jar檔案

JD-GUI是一個視覺化的Java反編譯程式碼檢視器,它可以實時的將class檔案反編譯成java檔案進行檢視。解壓下載的jd-gui檔案,執行目錄中的jd-gui可執行檔案啟動,然後載入上一步中反編譯好的classes.dex.dex2jar.jar檔案即可。

5. 將從odex反編譯後的classes.dex與其他資原始檔重新打包成一個完整的apk

以上我們假設的情況是應用程式編譯後的類檔案從apk檔案中被剝離出來,下面要做的是如何將上述步驟中得到的classes.dex與apk中的其他檔案重新打包成一個可用的apk。

首先將反編譯後的classes.dex和原先的app.apk(不含classes.dex)重新壓縮成一個完整的app.apk(apk檔案可用壓縮工具開啟),也就是說將classes.dex放進app.apk中。

將下載的AutoSign檔案解壓,可以看到有signapk.jar(還有個Sign.bat)檔案,執行以下命令給app.apk檔案簽名,就可以生成一個可以執行的apk檔案了。

$ java -jar signapk.jar testkey.x509.pem testkey.pk8 app.apk app_signed.apk

6. apktool的使用

網上還有個工具是apktool,可以對apk進行解析,反編譯資原始檔,並將類檔案解析成smali檔案;同時還可以將解析後的檔案重新打包成apk。功能和以上介紹的幾個工具類似,它的使用方法如下:

apktool d app.apk and    反編譯 app.apk到資料夾and

apktool b app                從資料夾app重建APK,輸出到ABC\dist\out.apk

具體的使用方法在此不再贅述,請參考官方網站,或者:

7. 我的 $AndroidDecompile目錄下的檔案的截圖

三、一些工具的幫助資訊

1. baksmali 的幫助資訊

usage: java -jar baksmali.jar [options] <dex-file>

disassembles and/or dumps a dex file

 -?,--help                                 Prints the help message then exits.

 -b,--no-debug-info                         Specify twice for debug options

                           don't write out debug info (.local,

                                           .param, .line, etc.)

 -c,--bootclasspath <BOOTCLASSPATH>      The bootclasspath jars to use, for

                                           analysis. Defaults to

                                           core.jar:ext.jar:framework.jar:andro

                                           id.policy.jar:services.jar. If the

                                           value begins with a :, it will be

                                           appended to the default

                                           bootclasspath instead of replacing it

 -d,--bootclasspath-dir <DIR>                The base folder to look for the

                                           bootclasspath files in. Defaults to

                                           the current directory

 -f,--code-offsets                           Add comments to the disassembly

                                           containing the code offset for each address

 -l,--use-locals                             Output the .locals directive with

                                           the number of non-parameter

                                           registers, rather than the .register

 -o,--output <DIR>                         Directive with the total number of  register

                                           the directory where the disassembled

                                           files will be placed. The default is out

 -p,--no-parameter-registers                  Use the v<n> syntax instead of the

                                           p<n> syntax for registers mapped to

                                           method parameters

 -r,--register-info <REGISTER_INFO_TYPES>  Print the specificed type(s) of

                                           register information for each

                                           instruction. "ARGS,DEST" is the

                                           default if no types are specified.

                                           Valid values are:

                                           ALL: all pre- and post-instruction registers.

                                           ALLPRE: all pre-instruction registers

                                           ALLPOST: all post-instruction registers

                                           ARGS: any pre-instruction registers

                                               used as arguments to the instruction

                                           DEST: the post-instruction

                                               destination register, if any

                                           MERGE: Any pre-instruction register

                                               has been merged from more than 1

                                               different post-instruction register

                                               from its predecessors

                                           FULLMERGE: For each register that

                                             would be printed by MERGE, also show

                                             the incoming register types that

                                             were merged

 -s,--sequential-labels                       Create label names using a

                                           sequential numbering scheme per

                                           label type, rather than using the

                                           bytecode address

 -v,--version                               Prints the version then exits

 -x,--deodex                               Deodex the given odex file. This

                                           option is ignored if the input file

                                           is not an odex file

2. smali 的幫助資訊

usage: java -jar smali.jar [options] [--] [<smali-file>|folder]*

assembles a set of smali files into a dex file

 -?,--help            prints the help message then exits. Specify twice for

                      debug options

 -o,--output <FILE>   the name of the dex file that will be written. The default

                      is out.dex

 -v,--version         prints the version then exits

3. auto-sign 的幫助資訊

SignApk.jar is a tool included with the Android platform source bundle.

testkey.pk8 is the private key that is compatible with the recovery image included in this zip file

testkey.x509.pem is the corresponding certificate/public key

Usage:

java -jar signapk.jar testkey.x509.pem testkey.pk8 update.zip update_signed.zip

4. apktool 的幫助資訊

Apktool v1.3.2 - a tool for reengineering Android apk files

Copyright 2010 Ryszard Wi?niewski <[email protected]>

Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)

Usage: apktool [-v|--verbose] COMMAND [...]

COMMANDs are:

    d[ecode] [OPTS] <file.apk> [<dir>]

        Decode <file.apk> to <dir>.

        OPTS:

        -s, --no-src

            Do not decode sources.

        -r, --no-res

            Do not decode resources.

        -d, --debug

            Decode in debug mode. Check project page for more info.

        -f, --force

            Force delete destination directory.

        -t <tag>, --frame-tag <tag>

            Try to use framework files tagged by <tag>.

        --keep-broken-res

            Use if there was an error and some resources were dropped, e.g.:

            "Invalid config flags detected. Dropping resources", but you

            want to decode them anyway, even with errors. You will have to

            fix them manually before building.

    b[uild] [OPTS] [<app_path>] [<out_file>]

        Build an apk from already decoded application located in <app_path>.

        It will automatically detect, whether files was changed and perform

        needed steps only.

        If you omit <app_path> then current directory will be used.

        If you omit <out_file> then <app_path>/dist/<name_of_original.apk>

        will be used.

        OPTS:

        -f, --force-all

            Skip changes detection and build all files.

        -d, --debug

            Build in debug mode. Check project page for more info.

    if|install-framework <framework.apk> [<tag>]

        Install framework file to your system.

四、參考資料

1. Smali

2. ApkTool


二、jar檔案轉為dex檔案

1.第一篇

可以通過dx工具將jar包中所有的類打包編譯為一個名為classes.dex的檔案,然後通過aapt工具將classes.dex檔案重新新增到你的jar包中。命令格式為dx --dex --output=classes.dex destination.jar
aapt add destionation.jar classes.dex。這兩個工具都在android sdk目錄中的platform/tools目錄中,可提前將目錄加到環境變數中,方便執行。另外這兩個命令其實是個指令碼檔案,在linux下執行的時候別忘了加上路徑,如:./dx or ./aapt,否則提示無法找到命令,或者將tools加入環境變數,就可以不用加路徑標識了。具體的dx工具使用引數可在linux的命令列終端下輸入dx查詢。