如何在apktool反编译的应用程序中为可绘制对象添加新的资源标识符?

时间:2018-10-10 14:37:24

标签: android android-drawable android-resources smali

我试图将imageview注入反编译的apk的smali代码中。但是imageview使用的drawable需要它自己的唯一资源标识符。到目前为止,我一直没有添加新的资源标识符而没有从apktool得到构建错误,但是没有成功。任何帮助将不胜感激。这是我已采取的步骤:

第1步:

在public.xml文件中找到最后一个资源ID。 就我而言:<public type="menu" name="search_item_minioverflow" id="0x7f0d0003" />

将id从十六进制转换为文本,这给了我以下内容: 2131558403 。 将数字递增1并将其转换回十六进制: 0x7f0d0004

第2步:

将ID为<public type="drawable" name="cross" id="0x7f0d0004" />的新资源粘贴到public.xml文件中。

第3步:

将ID为.field public static final cross:I = 0x7f0d0004的新静态字段粘贴到R $ drawable.smali文件中。

最后:

我尝试使用apktool构建反编译的apk,但收到以下构建错误:

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]

编辑:这是被注入的smali代码,它在从以下设备获取的apk中正常运行:

.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

1 个答案:

答案 0 :(得分:3)

您不应找到最后一个资源ID,而是要添加的类型的最后一个资源ID。您将菜单的资源ID递增1。

您不能仅对任何资源类型使用任何资源ID。 ID的第3位和第4位(在x之后)代表类型。例如,至少在您的项目中,0d代表菜单类型,而02代表可绘制类型。

相反,请在public.xml中查找“ drawable”类型的最后一个ID(请注意,它们不是按升序排列的),而是向其添加1。应该是0x7f02xxxx + 1。