How to debug smali code of an android application?

Viewed 31458

I have a working android application. of which i dont have a source code. I would like to debug a functionality of this application. I could successfully reverse engineer this application apk file using apktool - https://code.google.com/p/android-apktool/ This tool generates class files in smali format.

My requirement is :

  1. To be able to debug a method by adding debug logs.
  2. To be able to debug method calls by printing a stack trace.

To achieve this I need to inject/insert smali equivalent of a debug log or stack trace. I tried adding some smali instruction at the start of one of the method but it crashed with ClassVerifyError.

Example smali code -

.method public declared-synchronized b()V
    .locals 2

    .prologue

    .line 87
    monitor-enter p0

    :try_start_0
    iget-object v0, p0, Lcom/example/viewerlib/t/d;->a:Ljava/lang/Thread;

    invoke-virtual {v0}, Ljava/lang/Thread;->isAlive()Z

               :
               :

Could someone help me out in adding smali debug logs. Thnx in advance.

6 Answers

I tried using smalidea plugin but could not get it working so tried with different approach stated below.

There is an option in Android Studio 3.1.2, Go to File -> Select Profile or Analyze Apk option. It will then decompile the Apk in classes.dex file and out file which will have smali files. You can then Rus As-> Debug option and put breakpoint in any smali files an it will stop at specified breakpoint. This works only if you have single dex file in your application. There is however an work around for multi dex application in which you need to follow below steps -

1) Download Apktool tool from here

2) Decompile the Apk using -> apktool d

3) Copy all the smali files generated above in multiple folders - smali, smali_classess2, smali_classes3 and so on into the out folder created from Android Studio -> Analyze Apk method earlier, so that all the smali files are in single folder

4) This step I am not sure if it is required, but I had followed this, feel free to verify this step. Here you again build the Apk using apktool using folder created in step 2 using command -> apktool b . This will generate all the .dex files and copy all the files in the same location where single classes.dex file was generated using Analyze Apk method earlier.

5) Now use the same Project created using Analyze Apk method and you are good to debug.

P.S. - Android studio will complain of debugging using Java code but this method will work. This method is only helpful if you are editing any smali file and you want to debug it.

Related