Monday, August 27, 2012

Debugging Smali code with apk-tool and NetBeans works!

Ho-ho-ho, it works now! Here is a (more or less detailed) how-to. To debug Smali code with apk-tool, you need
  1. Apk-tool 1.4.1 and NetBeans 6.8. Use these versions, not the latest ones! Currently, the latest versions of apk-tool and NetBeans do not allow to debug Smali code.
  2. Java, JDK and other stuff installed in your system to make Apk-tool and  NetBeans working
The step-by-step instruction:
  1. Decode your .apk file to out directory, use -d option:
    java -jar apktool.jar d -d my.app.apk out
    
  2. Add android:debuggable="true" attribute to <application> section in out/AndroidManifest.xml file.
  3. Build out directory to .apk file:
    java -jar apktool.jar b -d out my.app.to.debug.apk
    
  4. Sing and install my.app.to.debug.apk to the Android device used for debugging
  5. Delete out/build folder
  6. Run NetBeans, click "File" -> "New Project". Choose "Java"->"Java Project with Existing Sources". Click "Next".
  7. Specify out as "Project Folder". Click "Next".
  8. Add out/smali folder to the "Source Package Folder" list. Click "Next" and then "Finish".
  9. Start my.app.to.debug.apk on the device, run DDMS, find your application on a list and click it. Note port information in last column - it should be something like 86xx / 8700".
  10. In Netbeans, click "Debug" -> "Attach Debugger" -> select "JPDA" and set "Port" to 8700 (or whatever you saw in previous step). Rest of fields should be ok, click "OK".
  11. Debugging session should start: you will see some info in a log and debugging buttons will show up in top panel.
  12. Set breakpoint. You must select line with some instruction, you can't set breakpoint on lines starting with ".", ":" or "#".
  13. Trigger some action in application. If you run at breakpoint, then thread should stop and you will be able to debug step by step, watch variables, etc.
I copy-pasted steps 9-13 from the original instruction (sorry, English is not my native language... and I'm too lazy to "produce unique content for the blog", so just copy-pasted description of last 5 steps :)).

Questions are welcome. Have a nice day :)

P.S.  If you have problems with breakpoints, this may help.

25 comments:

  1. Thank you so much for this. You saved my life.
    I'll be indebted to you for this.

    ReplyDelete
  2. after step 3 I am get an exception. that the aapt was not found, although I have aapt in the same folder. I tried it from different directories but it gives me the same error. below is the excption details. I am a bit confused

    I: Checking whether sources has changed...
    I: Smaling...
    I: Checking whether resources has changed...
    I: Building resources...
    Exception in thread "main" brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, -F, /tmp/APKTOOL4160944918573250929.tmp, -I, /root/apktool/framework/1.apk, -S, /home/lab2alex/Documents/out/res, -M, /home/lab2alex/Documents/out/AndroidManifest.xml]
    at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:193)
    at brut.androlib.Androlib.buildResourcesFull(Androlib.java:301)
    at brut.androlib.Androlib.buildResources(Androlib.java:248)
    at brut.androlib.Androlib.build(Androlib.java:171)
    at brut.androlib.Androlib.build(Androlib.java:154)
    at brut.apktool.Main.cmdBuild(Main.java:174)
    at brut.apktool.Main.main(Main.java:59)
    Caused by: brut.common.BrutException: could not exec command: [aapt, p, -F, /tmp/APKTOOL4160944918573250929.tmp, -I, /root/apktool/framework/1.apk, -S, /home/lab2alex/Documents/out/res, -M, /home/lab2alex/Documents/out/AndroidManifest.xml]
    at brut.util.OS.exec(OS.java:87)
    at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:191)
    ... 6 more
    Caused by: java.io.IOException: Cannot run program "aapt": java.io.IOException: error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:488)
    at java.lang.Runtime.exec(Runtime.java:610)
    at java.lang.Runtime.exec(Runtime.java:483)
    at brut.util.OS.exec(OS.java:78)
    ... 7 more
    Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.(UNIXProcess.java:164)
    at java.lang.ProcessImpl.start(ProcessImpl.java:81)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:470)
    ... 10 more

    ReplyDelete
    Replies
    1. you are probably on linux, and current directory is not on the path except when explicit called (./blahblah). move aapt to path folder, i.e /usr/local/bin

      Delete
    2. Linux:

      Download apktool-install-linux-* file
      Download apktool-* file
      Unpack both to /usr/local/bin directory (you must have root permissions)

      Delete
  3. Is it working with Apktool v2.0.0-Beta4? Apktool 1.4.1 is old and does not work with a lot of apk. The code produces by Apktool v2.0.0-Beta4 doesn't seem the be wrapped in classes for use in IDE

    ReplyDelete
    Replies
    1. IT IS!!!!!!! :D THANKS

      Delete
    2. can any one post compiled Apktool v2.0.0-Beta4 .jar file ? i can't rebuild it in windows

      Delete
    3. Here is a detailed instruction how to build apktool from sources (for Windows):
      http://codedigging.com/2014/01/how-to-build-android-apktool-2-00-beta-from-sources/
      It's tested and work (at least, for me)

      Delete
    4. Apktool v2.0.0-a7b06f-SNAPSHOT - a tool for reengineering Android apk files with smali v2.0.3-a7b06f16 and baksmali v2.0.3-a7b06f16

      http://www73.zippyshare.com/v/13712059/file.html

      Delete
  4. NetBeans выдает ошибку "в подключении отказано". В чем может быть дело? ddms подключается и выдает порты на приложении 8600/8700. Пробовал и тот и другой.

    ReplyDelete
    Replies
    1. Черт, я уже и забыл про этот пост, а сюда всё ещё что-то пишут... давайте по почте обсудим?
      dm.o.kovalenko@gmail.com

      Delete
    2. В таких случаях попробуйте поменять свой host на localhost (там обычно показывается имя компьютера). После этого у меня подсоединился он, но ни один breakpoint не удалось установить - писал, что место кода не находится ни в одном классе.

      Delete
  5. Can you help me please?
    is there a debug tutorial for apktool 2

    ReplyDelete
    Replies
    1. Not yet. As far as I know it, apktool2 is beta at the moment. You should build it from source.

      Delete
    2. this tutotial works exactly as it is with apktool 2.0.0b7.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Hugo, as far as I know it: yes, it works with apktool 2.0.0b7 as well

      Delete
    5. It wasn't a question ehehe tested here, works as it is!
      thanks!

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  8. Good post but I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it! Lucky Patcher APK

    ReplyDelete