iBotPeaches / Apktool

A tool for reverse engineering Android apk files
https://apktool.org/
Apache License 2.0
20.33k stars 3.59k forks source link

[BUG] Decompilation error of resources #3702

Closed cpereirarafa closed 2 weeks ago

cpereirarafa commented 2 months ago

Hello,

I have been trying to decompile an apk, but resources parsing fails. You'll be able to find the stacktrace below, but I tried to dig into the failing point in apktool, dicovering:

In the when decoding a file, a good decoded attribute comes like this:

image

In the case of the affected attributes, they do not come with the name, rather with an id that I suppose is linked to nowhere:

image

The two attributes shown above are referring to the lines 13 and 14 of this layout (and we can see that JADx also didn't managed to bring the id of this resource): Screenshot 2024-09-23 at 13 59 09

From my (small) understanding of the flow, the indexes of the tables that brings the attribute values are correct, but the xml parser that deodes the original file may be missing the actual value?

Information Apktool Version (apktool -version) - 2.9.3 Operating System (Mac, Linux, Windows) - macOS 14.7 APK From? (Playstore, ROM, Other) - Other Java Version (java --version) - 17.0.9

Stacktrace/Logcat

I: Using Apktool 2.9.3 I: Checking whether sources has changed... I: Checking whether sources has changed... I: Checking whether resources has changed... I: Building resources... W: decoded_app/res/animator/2130837504.xml:4: error: not well-formed (invalid token). W: decoded_app/res/animator/2130837504.xml: error: file failed to compile. W: decoded_app/res/animator/2130837527.xml:4: error: not well-formed (invalid token). W: decoded_app/res/animator/2130837527.xml: error: file failed to compile. W: decoded_app/res/color/2131100411.xml:5: error: not well-formed (invalid token). W: decoded_app/res/color/2131100411.xml: error: file failed to compile. W: decoded_app/res/color/2131100412.xml:5: error: not well-formed (invalid token). W: decoded_app/res/color/2131100412.xml: error: file failed to compile. W:decoded_app/res/drawable/2131231146.xml:5: error: not well-formed (invalid token). W: decoded_app/res/drawable/2131231146.xml: error: file failed to compile. W: decoded_app/res/drawable/2131231148.xml:5: error: not well-formed (invalid token). W: decoded_app/res/drawable/2131231148.xml: error: file failed to compile. W: decoded_app/res/font/2131296256.xml:2: error: not well-formed (invalid token). W: decoded_app/res/font/2131296256.xml: error: file failed to compile. W: decoded_app/res/font/2131296257.xml:2: error: not well-formed (invalid token). W: decoded_app/res/font/2131296257.xml: error: file failed to compile. W: decoded_app/res/font/2131296258.xml:2: error: not well-formed (invalid token). W: decoded_app/res/font/2131296258.xml: error: file failed to compile. W: decoded_app/res/font/2131296259.xml:2: error: not well-formed (invalid token). W: decoded_app/res/font/2131296259.xml: error: file failed to compile. brut.androlib.exceptions.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [/var/folders/38/pd7j36w954b83s1_8zmfrn6m0000gn/T/brut_util_Jar_23304051720274397738368976131288687923.tmp, compile, --dir, decoded_app/res, --legacy, -o, decoded_app/build/resources.zip]

Steps to Reproduce

java -jar apktool.jar d -o tmp/ -f "app.apk" java -jar apktool.jar java -jar b tmp/ -o "repacked_app.apk"

APK

If this APK can be freely shared, please upload/attach a link to it. Sent apkpure url by mail

Questions to ask before submission

Have you tried apktool d, apktool b without changing anything? Yes, decompiling resources fails on build time, with --no-res works well, but for what I need to do is mandatory to decompile it.

If you are trying to install a modified apk, did you resign it? Yes

Are you using the latest apktool version? Yes

cpereirarafa commented 1 month ago

Is this related to https://github.com/iBotPeaches/Apktool/issues/3553#issuecomment-2026080032_ ?

sevilS commented 1 month ago

Any update on this?, can we help on how to fix this?

iBotPeaches commented 2 weeks ago

Generally what is happening here is there are resources that are stripped out/removed. I believe this is some new optimization technique as those IDs cannot be resolved from system resources (framework) or self resources (the app).

There is a chance its an OEM resource, but in the case of this application - the resources in this sample are

    <LinearLayout android:gravity="center" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginVertical="@dimen/2131165731" 2130969280="@id/2131362156" 2130969309="2131362933">

The final two would be like:

0x7f0402c0="@id/id_0x7f0a016c"
0x7f0402dd="id_0x7f0a0475"

Generally I believe since Apktool often wants to rebuild an application - we may have to strip all attributes/tags that we cannot resolve. I'm tracking that effort already in: https://github.com/iBotPeaches/Apktool/issues/3533