Menu

ModifyApkWithDexTool

Featured (4)
Anonymous Bob Pan

Introduction

Dex2jar have the ability to modify the code of an apk.

  1. It first translate the code from dex to jar.
  2. modify .class files in the jar.
  3. It translate jar back to dex and put into apk
  4. sign the apk, and we done the modification.

for demo, we will modify a apk to let it showing a message when it start.

and you need following components in you PATH. android-sdk,ant,dex2jar,jdk6

Details

got an apk

you need an apk, which you didn't have the source. Just for demo, we build a simple hello-world apk by running the following command

android create project --name test_apk --path test_apk --package a.b --activity Main --target 1
cd test_apk
ant debug
cd bin

now we got test_apk/bin/test_apk-debug.apk. we can check it by running the apk inside an emulator.

work with dex-tools

Now we got the apk, and It's time to modify it.

Convert the code to a modifiable format.

we can't modify dex or jar(.class) file directly. dex2jar support to assemble/disassemble .class file from/to jasmin file. so we convert the apk to jar and disassemble it.

# convert classes.dex in test_apk-debug.apk to test_apk-debug_dex2jar.jar
d2j-dex2jar.sh -f -o test_apk-debug_dex2jar.jar test_apk-debug.apk
# verify jar
d2j-asm-verify.sh test_apk-debug_dex2jar.jar
# convert to jasmin format
d2j-jar2jasmin.sh -f -o test_apk_jasmin test_apk-debug_dex2jar.jar

edit test_apk_jasmin/a/b/Main.j to show a toast

.method public onCreate(Landroid/os/Bundle;)V
aload 0
aload 1
invokespecial android/app/Activity/onCreate(Landroid/os/Bundle;)V
aload 0
ldc_w 2130837504
invokevirtual a/b/Main/setContentView(I)V

; Toast.makeText(this.getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
aload 0
invokevirtual android/app/Activity/getApplicationContext()Landroid/content/Context;
ldc "hi"
ldc_w 1
invokestatic android/widget/Toast/makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;
invokevirtual android/widget/Toast/show()V

return
.limit locals 2
.limit stack 3
.end method

repack apk

# build jar
d2j-jasmin2jar.sh -f  -o test_apk_jasmin.jar  test_apk_jasmin/ 
# verify jar
d2j-asm-verify.sh test_apk_jasmin.jar
# convert to dex
d2j-jar2dex.sh  -f -o classes.dex test_apk_jasmin.jar
# make a copy
cp test_apk-debug.apk test_apk-debug-toast.apk
# replace classes.dex in test_apk-debug-toast.apk
zip -r test_apk-debug-toast.apk classes.dex
# sign the apk
d2j-apk-sign.sh -f -o test_apk-debug-toast-signed.apk test_apk-debug-toast.apk

run the apk

# uninstall previously apk
adb uninstall a.b
# install
adb install test_apk-debug-toast-signed.apk
# start main activity
adb shell am start -n a.b/.Main

NOTICE

all the above command is for *unix, for windows users, please replace the suffix from .sh to .bat.

and for the zip command, just open the test_apk-debug-toast.apk with winRAR,winZIP or 7z, then drag the classes.dex into it.


Related

Wiki: Faq

Discussion

  • Anonymous

    Anonymous - 2012-02-02

    Originally posted by: haojies...@gmail.com

    What is the difference between d2j-dex2jar.bat and dex2jar.bat?

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-04-27

    Originally posted by: preetamk...@gmail.com

    i didnt understand

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-05-08

    Originally posted by: sreedhar...@gmail.com

    Steps to Break, Edit and Rebuild your apk file on WIndows Platform ************************ **

    Create a "New Folder"

    Download Dex2Jar? Utility zip file from http://code.google.com/p/dex2jar/ and Extract into the "New Folder"

    Create an apk file and .. Place your apk file inside Extracted folder

    Open Command prompt and switch to the Windows Extracted directory

    Type in the below commands in sequence..

    d2j-dex2jar.bat -f -o abc.jar MyFile?.apk

    d2j-asm-verify.bat abc.jar

    d2j-jar2jasmin.bat -f -o JasminFolder? abc.jar

    Edit JasmineFolder?/xyz.j and save

    d2j-jasmin2jar.bat -f -o JasminFile?.jar JasminFolder?/

    d2j-asm-verify.bat JasminFile?.jar

    d2j-jar2dex.bat -f -o classes.dex JasminFile?.jar

    Backup your apk file

    Rename MyFile?.apk to MyFile?.apk.zip

    Delete the classes.dex file you find inside this zip file

    Move the new classes.dex file to MyFile?.apk.zip

    Rename MyFile?.apk.zip to MyFile?.apk

    d2j-apk-sign.bat -f -o MyFile?-signed.apk MyFile?.apk

    Run and test your MyFile?-signed.apk

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-05-22

    Originally posted by: kuz...@gmail.com

    I've just disassembled and assembled. without changing jasmine. but after install apk it does't run. android say 'app bla-bla unfortunately stopped'. is there another method to disassemble and assemble APK classes?

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-05-22

    Originally posted by: kuz...@gmail.com

    apk does't work even next situa: convert dex to jar verify jar convert jar to dex zip dex to apk sign apk

    I didn't change jar, but something wrong with dex file... maybe. who can explain me what is wrong? thanks a lot.

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-06-20

    Originally posted by: piqpiq90

    Ive got the same problem with my own written android-app - do you know what could be the problem? I use java 1.6.0_25 for the development. Thx in advance guys :) - nice tool nevertheless.

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-06-30

    Originally posted by: piqpiq90

    By the way in the end it worked for me. My problem where problems with the source-code. It did not successfully recreated my source. Problem was a "synchronized"-"Tag".

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-07-12

    Originally posted by: hram...@gmail.com

    is there a tool that will prevent decompling?

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-09-06

    Originally posted by: shinom...@gmail.com

    I have edited some app... in the old version of app, have no any problem. But in the newer version, it crashed after run.

    edit at the same class file and only 6 byte replaced... any suggestion?

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-09-22

    Originally posted by: dave113x...@gmail.com

    hello when i use the frst command line d2j-dex2jar.sh -f -o test_apk-debug_dex2jar.jar test_apk-debug.apk i have many errors: java.lang.NullPointerException?

    at org.objectweb.asm.Type.getType(Unknown Source) at com.googlecode.dex2jar.v3.V3ClassAdapter?.build(V3ClassAdapter?.ja

    3)

    at com.googlecode.dex2jar.v3.V3ClassAdapter?.visitField(V3ClassAdapt?

    va:247)

    at com.googlecode.dex2jar.reader.DexFileReader?.acceptField(DexFileR

    .java:607)

    at com.googlecode.dex2jar.reader.DexFileReader?.acceptClass(DexFileR

    .java:442)

    at com.googlecode.dex2jar.reader.DexFileReader?.accept(DexFileReader?

    :333)

    at com.googlecode.dex2jar.v3.Dex2jar.doTranslate(Dex2jar.java:82) at com.googlecode.dex2jar.v3.Dex2jar.to(Dex2jar.java:219) at com.googlecode.dex2jar.v3.Dex2jar.to(Dex2jar.java:210) at com.googlecode.dex2jar.tools.Dex2jarCmd?.doCommandLine(Dex2jarCmd?

    :108)

    at com.googlecode.dex2jar.tools.BaseCmd?.doMain(BaseCmd?.java:118) at com.googlecode.dex2jar.tools.Dex2jarCmd?.main(Dex2jarCmd?.java:34)

    don't know why

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2012-10-21

    Originally posted by: shubhamP...@gmail.com

    i tried to follow step to convert apk into jasmin format..but after third step it respond on command prompt "disassemble Sample.jar -> sample_jasmin" but it didn't create in that directry rrlated jasmin file formate...any help?

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2013-02-22

    Originally posted by: andrewru...@gmail.com

    hello hi I use smali basmali to edit apks I like. would this be real similar or produce same or give more abilities. dont guess you could make app with just smalis but to alter with, easy. i learn new google code talk this page I google code better now. I like love Android.

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2014-02-18

    Originally posted by: chan.sh...@gmail.com

    please send that commands in the proper way that we can understand....please

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2014-02-26

    Originally posted by: sravan6...@gmail.com

    i could successfully convert .dex file to jar. but when i am trying to verify the jar using "d2j-asm-verify.sh" command, it fails.. Any help would be appreciated.. and what is this jasmin format. how to edit it?

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2014-03-10

    Originally posted by: emneg.ze...@gmail.com

    so , would the key(pem) changed after signed?

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2014-03-31

    Originally posted by: nevil...@gmail.com

    using this tool, you will be easily able to edit your apk file. Apk Edit - Edit Your Apk File (Android App)

     

    Last edit: Anonymous 2018-10-12
  • Anonymous

    Anonymous - 2014-04-16

    Originally posted by: sharanan...@gmail.com

    if u see the steps it is not clear edit test_apk_jasmin/a/b/Main.j to show a toast. generally in android project it is mainactivity.java but here the author tells to edit Main.j (what is .j???). i edited mainactivity.java and followed all the steps and after that made to run the installed signed apk it gives error

     

    Last edit: Anonymous 2018-10-12

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.