Declaring a global variable and increasing its value in smali

2.6k views Asked by At

I am trying to alter an apk file using smali, so that each time it accesses another member of a list a variable gets incremented.

I am looking to do two things, first to declare a public static int named val1, and secondly implement this code:

String str = (String)this.ListX.get(val1);
val1++;

However I have very limited knowledge on smali code, and I want some insight on this.

1

There are 1 answers

0
QJGui On

I use apktool to convert dex code to smali:

Java Code:

private HashMap ListX;
public void TestCodeStatic()
{
    String str = (String)this.ListX.get(val1);
    val1++;
}

Smali Code:

.field public static val1:I
# instance fields
.field private ListX:Ljava/util/HashMap;


# virtual methods
.method public TestCodeStatic()V
    .locals 3

    .prologue
  .line 54
    iget-object v1, p0, Lcom/example/app/MainActivity;->ListX:Ljava/util/HashMap;

  .line 57
    sget v2, Lcom/example/app/MainActivity;->val1:I

  .line 60
    invoke-static {v2}, Ljava/lang/Integer;->valueOf(I)Ljava/lang/Integer;

  .line 63
    move-result-object v2

  .line 66
    invoke-virtual {v1, v2}, Ljava/util/HashMap;->get(Ljava/lang/Object;)Ljava/lang/Object;

  .line 69
    move-result-object v0

  .line 72
    check-cast v0, Ljava/lang/String;

    .local v0, str:Ljava/lang/String;
  .line 76
    sget v1, Lcom/example/app/MainActivity;->val1:I

  .line 79
    add-int/lit8 v1, v1, 0x1

  .line 82
    sput v1, Lcom/example/app/MainActivity;->val1:I

  .line 85
    return-void
.end method