1

I'm trying to inject a imageview in to a decompiled apk's smali code. But the drawable used by the imageview needs it's own unique resource identifier. So far i have been unsuccessful in adding a new resource identifier without getting building errors from apktool. Any help would be greatly appreciated. Here are the steps i have taken:

Step 1:

Find the last resource id in the public.xml file. In my case : <public type="menu" name="search_item_minioverflow" id="0x7f0d0003" />.

Convert the id from hex to text, this gives me the following: 2131558403. Increment the number by 1 and convert it back to hex : 0x7f0d0004.

Step 2:

Paste the new resource with incremented id: <public type="drawable" name="cross" id="0x7f0d0004" /> in to the public.xml file.

Step 3:

Paste the new static field with incremented id: .field public static final cross:I = 0x7f0d0004 in to the R$drawable.smali file.

Finally:

I try to build the decompiled apk with apktool but receive the following building error:

res\values\public.xml:986: error: Public resource drawable/cross has conflicting type codes for its public identifiers (0x2 vs 0xd).
    W:
    brut.androlib.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [C:\Users\user\AppData\Local\Temp\brut_util_Jar_6742820997882567173.tmp, p, --forced-package-id, 127, --min-sdk-version, 14, --target-sdk-version, 25, --version-code, 111, --version-name, 5.2.1, --no-version-vectors, -F, C:\Users\user\AppData\Local\Temp\APKTOOL3513689331380739071.tmp, -0, arsc, -0, png, -0, arsc, -I, C:\Users\user\AppData\Local\Temp\1.apk, -S, C:\Users\user\Desktop\tune\res, -M, C:\Users\user\Desktop\tune\AndroidManifest.xml]

EDIT: And here is the smali code being injected, it runs normally in the apk it's taken from:

.method public showDialog()V
    .locals 6

    .line 92
    new-instance v0, Landroid/widget/RelativeLayout;

    invoke-virtual {p0}, Lcom/example/MainActivity;->getApplicationContext()Landroid/content/Context;

    move-result-object v1

    invoke-direct {v0, v1}, Landroid/widget/RelativeLayout;-><init>(Landroid/content/Context;)V

    .line 93
    .local v0, "overlay":Landroid/widget/RelativeLayout;
    new-instance v1, Landroid/widget/RelativeLayout$LayoutParams;

    const/4 v2, -0x1

    invoke-direct {v1, v2, v2}, Landroid/widget/RelativeLayout$LayoutParams;-><init>(II)V

    .line 94
    .local v1, "params":Landroid/widget/RelativeLayout$LayoutParams;
    invoke-virtual {v0, v1}, Landroid/widget/RelativeLayout;->setLayoutParams(Landroid/view/ViewGroup$LayoutParams;)V

    .line 95
    const/4 v2, 0x0

    invoke-virtual {v0, v2}, Landroid/widget/RelativeLayout;->setBackgroundColor(I)V

    .line 96
    invoke-virtual {v0, v2}, Landroid/widget/RelativeLayout;->setClickable(Z)V

    .line 97
    invoke-virtual {v0, v2}, Landroid/widget/RelativeLayout;->setFocusable(Z)V

    .line 99
    new-instance v3, Landroid/widget/ImageView;

    invoke-virtual {p0}, Lcom/example/MainActivity;->getApplicationContext()Landroid/content/Context;

    move-result-object v4

    invoke-direct {v3, v4}, Landroid/widget/ImageView;-><init>(Landroid/content/Context;)V

    .line 100
    .local v3, "cross":Landroid/widget/ImageView;
    const v4, 0x7f060055

    invoke-virtual {v3, v4}, Landroid/widget/ImageView;->setImageResource(I)V

    .line 102
    new-instance v4, Landroid/widget/RelativeLayout$LayoutParams;

    const/4 v5, -0x2

    invoke-direct {v4, v5, v5}, Landroid/widget/RelativeLayout$LayoutParams;-><init>(II)V

    move-object v1, v4

    .line 103
    const/16 v4, 0xb

    invoke-virtual {v1, v4}, Landroid/widget/RelativeLayout$LayoutParams;->addRule(I)V

    .line 104
    invoke-virtual {v3}, Landroid/widget/ImageView;->getDrawable()Landroid/graphics/drawable/Drawable;

    move-result-object v4

    invoke-virtual {v4}, Landroid/graphics/drawable/Drawable;->getIntrinsicHeight()I

    move-result v4

    div-int/lit8 v4, v4, 0x2

    invoke-virtual {v3}, Landroid/widget/ImageView;->getDrawable()Landroid/graphics/drawable/Drawable;

    move-result-object v5

    invoke-virtual {v5}, Landroid/graphics/drawable/Drawable;->getIntrinsicWidth()I

    move-result v5

    div-int/lit8 v5, v5, 0x2

    rsub-int/lit8 v5, v5, 0x50

    invoke-virtual {v1, v2, v4, v5, v2}, Landroid/widget/RelativeLayout$LayoutParams;->setMargins(IIII)V

    .line 105
    invoke-virtual {v3, v1}, Landroid/widget/ImageView;->setLayoutParams(Landroid/view/ViewGroup$LayoutParams;)V

    .line 107
    invoke-virtual {v0, v3}, Landroid/widget/RelativeLayout;->addView(Landroid/view/View;)V

    .line 109
    new-instance v2, Landroid/app/Dialog;

    const v4, 0x1030010

    invoke-direct {v2, p0, v4}, Landroid/app/Dialog;-><init>(Landroid/content/Context;I)V

    .line 110
    .local v2, "topDialog":Landroid/app/Dialog;
    invoke-virtual {v2, v0}, Landroid/app/Dialog;->setContentView(Landroid/view/View;)V

    .line 111
    invoke-virtual {v2}, Landroid/app/Dialog;->show()V

    .line 113
    return-void
.end method
Daniël Visser
  • 581
  • 3
  • 13

1 Answers1

8

You're not supposed to find the last resource ID, but the last resource ID of the type you want to add. You incremented 1 to the resource ID of a menu.

You can't just use any resource ID for any resource type. The 3rd and 4th digits of the ID (after the x) represent the type. For instance, in your project at least, 0d is for the menu type, while 02 is for the drawable type.

Instead, look for the last ID of type "drawable" in your public.xml (be careful, they aren't ordered in increasing order) and add 1 to that one instead. It should be 0x7f02xxxx + 1.

TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • Yes it's building now! Thank you for the fast response. But my image view is still not being displayed. Are there anymore steps i need to take to create a new resource id? Other views i inject work fine, only the imageview with resource id is giving me trouble. – Daniël Visser Oct 10 '18 at 18:22
  • I don't see a problem off-the-bat, but you might want to give JADx a try. Compile to an APK and then open that APK in JADx GUI and check the Java result. – TheWanderer Oct 10 '18 at 18:55
  • Everything is working fine now, the problem was the injection location of the function call. Thanks again! – Daniël Visser Oct 11 '18 at 10:05
  • You've saved my life :))) – Tony Apr 22 '19 at 09:27