iBotPeaches / Apktool

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

attributes in ^attr-privates.xml not found #1668

Closed jtothe4n closed 3 years ago

jtothe4n commented 6 years ago

Information

  1. Apktool Version (apktool -version) - 2.3.0
  2. Operating System (Mac, Linux, Windows) - Win10
  3. APK From? (Playstore, ROM, Other) - HTC Oreo SystemUI

Stacktrace/Logcat

W: C:\Android\reverse_engineering\SystemUI\res\values\public.xml:1193: error: Public symbol ^attr-private/viewType declared here is not defined.
W: C:\Android\reverse_engineering\SystemUI\res\values\public.xml:1194: error: Public symbol ^attr-private/voiceIcon declared here is not defined.
W: C:\Android\reverse_engineering\SystemUI\res\values\public.xml:1195: error: Public symbol ^attr-private/widgetLayout declared here is not defined.
Exception in thread "main" W: C:\Android\reverse_engineering\SystemUI\res\values\public.xml:1196: error: Public symbol ^attr-private/wifi_friction declared here is not defined.
W: C:\Android\reverse_engineering\SystemUI\res\values\public.xml:1197: error: Public symbol ^attr-private/wifi_signal declared here is not defined.
brut.androlib.AndrolibException: brut.androlib.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [C:\Users\J3F4F~1.DUO\AppData\Local\Temp\brut_util_Jar_1618230815674308433.tmp, p, --forced-package-id, 127, --min-sdk-version, 23, --target-sdk-version, 26, --version-code, 26, --version-name, 8.0.0, --no-version-vectors, -F, C:\Users\J3F4F~1.DUO\AppData\Local\Temp\APKTOOL224544575642684951.tmp, -0, arsc, -0, webp, -0, arsc, -I, C:\Users\J.Duong\AppData\Local\apktool\framework\1.apk, -S, C:\Android\reverse_engineering\SystemUI\res, -M, C:\Android\reverse_engineering\SystemUI\AndroidManifest.xml]
        at brut.androlib.Androlib.buildResourcesFull(Androlib.java:485)
        at brut.androlib.Androlib.buildResources(Androlib.java:419)
        at brut.androlib.Androlib.build(Androlib.java:318)
        at brut.androlib.Androlib.build(Androlib.java:270)
        at brut.apktool.Main.cmdBuild(Main.java:224)
        at brut.apktool.Main.main(Main.java:75)
Caused by: brut.androlib.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [C:\Users\J3F4F~1.DUO\AppData\Local\Temp\brut_util_Jar_1618230815674308433.tmp, p, --forced-package-id, 127, --min-sdk-version, 23, --target-sdk-version, 26, --version-code, 26, --version-name, 8.0.0, --no-version-vectors, -F, C:\Users\J3F4F~1.DUO\AppData\Local\Temp\APKTOOL224544575642684951.tmp, -0, arsc, -0, webp, -0, arsc, -I, C:\Users\J.Duong\AppData\Local\apktool\framework\1.apk, -S, C:\Android\reverse_engineering\SystemUI\res, -M, C:\Android\reverse_engineering\SystemUI\AndroidManifest.xml]
        at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:454)
        at brut.androlib.Androlib.buildResourcesFull(Androlib.java:471)
        ... 5 more
Caused by: brut.common.BrutException: could not exec (exit code = 1): [C:\Users\J3F4F~1.DUO\AppData\Local\Temp\brut_util_Jar_1618230815674308433.tmp, p, --forced-package-id, 127, --min-sdk-version, 23, --target-sdk-version, 26, --version-code, 26, --version-name, 8.0.0, --no-version-vectors, -F, C:\Users\J3F4F~1.DUO\AppData\Local\Temp\APKTOOL224544575642684951.tmp, -0, arsc, -0, webp, -0, arsc, -I, C:\Users\J.Duong\AppData\Local\apktool\framework\1.apk, -S, C:\Android\reverse_engineering\SystemUI\res, -M, C:\Android\reverse_engineering\SystemUI\AndroidManifest.xml]
        at brut.util.OS.exec(OS.java:95)
        at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:448)
        ... 6 more

Steps to Reproduce

  1. apktool d SystemUI.apk

Failes loading resources?

I: Using Apktool 2.3.0 on SystemUI.apk I: Loading resource table... W: null W: null W: null W: null I: Decoding AndroidManifest.xml with resources... I: Loading resource table from file: C:\Users\J.Duong\AppData\Local\apktool\framework\1.apk I: Regular manifest package... I: Decoding file-resources... I: Decoding values / XMLs... I: Baksmaling classes.dex... I: Copying assets and libs... I: Copying unknown files... I: Copying original files...

Had first to delete the .9 suffix to from all pip_dismiss_scrim.9.png because of:

W: ERROR: 9-patch image C:\Android\reverse_engineering\SystemUI\res\drawable-xhdpi\pip_dismiss_scrim.9.png malformed. W: No marked region found along edge. W: Found along left edge.

then apktool b SystemUI brings the not found attributes warnings/errors

files.zip

jtothe4n commented 6 years ago

@iBotPeaches No ideas? :(

iBotPeaches commented 6 years ago

Android 5 added private attr support, which we patched. I'm not sure whats going on in here yet.

jtothe4n commented 6 years ago

The ^ in front has to be there? Looks weird to me.

iBotPeaches commented 6 years ago

Yep, that is valid. See AAPT2

https://android.googlesource.com/platform/frameworks/base/+/b274e35/tools/aapt2/java/JavaClassGenerator_test.cpp

TEST(JavaClassGeneratorTest, AttrPrivateIsWrittenAsAttr) {

std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
.setPackageId(u"android", 0x01)
.addSimple(u"@android:attr/two", ResourceId(0x01010001))
.addSimple(u"@android:^attr-private/one", ResourceId(0x01010000))
.build();

JavaClassGenerator generator(table.get(), {});
std::stringstream out;

ASSERT_TRUE(generator.generate(u"android", &out));

std::string output = out.str();
EXPECT_NE(std::string::npos, output.find("public static final class attr"));
EXPECT_EQ(std::string::npos, output.find("public static final class ^attr-private"));
}
jtothe4n commented 6 years ago

Ah, thx.

Never came across priv attributes since I mod the HTC framework. Seems SystemUI is the only one now. I tried to replace all ^attr-private with attr, threw me an error later, that attr ids expect to be 0x7f010000 instead of 0x7f050000. I could write me a tool doing that: continue the attr ids and replace the old id in all smalis with the new one. Would that work, or waste of time? :D

iBotPeaches commented 6 years ago

Waste of time. Private attributes are just that, private.

A resource ID is a 32 bit number of the form: PPTTNNNN. PP is the package the resource is for; TT is the type of the resource; NNNN is the name of the resource in that type. For applications resources, PP is always 0x7f. credit: https://stackoverflow.com/a/6646113/455008

0x7f050000

7f = PP
05 = TT
0000 = NNNN

Can't go changing types without breaking the resource-id.

jtothe4n commented 6 years ago

I would change the type and the resource id to match the attr type id format.

priv attr: 0x7f050000 > attr: 0x7f01NNNN

jtothe4n commented 6 years ago

Got it to compile indeed now. Can test tomorrow if the .apk still works.

iBotPeaches commented 6 years ago

The useless warning has been removed - https://github.com/iBotPeaches/Apktool/commit/3264f142cbfbf7c20a103ee33823ced5d5cccb10

The malformed 9patch image is probably the same image referenced in this ticket - https://github.com/iBotPeaches/Apktool/issues/1559

I think this is actually a duplicate of #1157 , but I need to research it more to make that connection

fernandosor commented 6 years ago

The malformed image is easily fixed. Usually though if apktool doesn't have the resources to compile then it should not have decompiled in the first place? usually if a large set of ids aren't defined then you can trace it to one mistake. There is no error in that folder. you can even delete the bad images and apktool will not recognize that as an issue. Does this need to be defined in the manifest? the ^attr-Private?

LeeDroid- commented 6 years ago

Any progress on the ^attr-Private issue? :)

Malformed 9 patch is easy to get around

jtothe4n commented 6 years ago

Double penalty for asking such a question, since you are a dev yourself :o)

LeeDroid- commented 6 years ago

Bwuhahaha... Been trying to bend my head around it.. penalty accepted, i see you managed to compile :)

chr0247 commented 6 years ago

framework.apk has ^attr-Private xml file and ids too after compiling, but can be recompiled. @iBotPeaches

fernandosor commented 6 years ago

^^^ this ^^^. framework-res.apk has the ^attr-privates.xml and there is no issue decompiling and recompiling. There is only one translation warning which has nothing to do with this issue.

So there is more going on here. Obviously apktool has the resources to deal with this new xml.

chr0247 commented 6 years ago

Two solutions have developed, SystemUI can be recompiled after modifying by both of them.

fernandosor commented 6 years ago

can you post the solution?

chr0247 commented 6 years ago

Will develop a tool to solve this.

jtothe4n commented 6 years ago

Removed

fernandosor commented 6 years ago

Jan this is Java source. Have you created an app for this?

jtothe4n commented 6 years ago

Thats c#

fernandosor commented 6 years ago

my bad just glanced at it. how does this solve this?

jtothe4n commented 6 years ago

Read the code, Lol.

https://www.dropbox.com/s/2haxs9ckdn0696h/PrivAttributesFixer.exe?dl=0

fernandosor commented 6 years ago

so you are renaming the files. I tried that and it didn't work lol

jtothe4n commented 6 years ago

Well, you need to do more than that you need to rewrite the public.xml. give the renamed attributes new ids incremented by the highest original attribute id. Then replace the old ids with the new ones in the smalis too

fernandosor commented 6 years ago

Thanks. It looks like this tool also changes smali code. Just put all the files in one folder with the tool?

jtothe4n commented 6 years ago

...I wrote that above. How else shall the app be working, if you just change the ressouce ids, but not he ones referenced in the code. decompile systemui, then run the .exe with the decompiled systemui dir as parameter. If its not clear now, I can't help you, lol.

fernandosor commented 6 years ago

Thank you.

iBotPeaches commented 6 years ago

I did some investigating on this. In my understanding ^attr-private was introduced for the system level attributes. It allows a system application to say "this attribute is private" and not exposed.

We encountered a few bugs with this format because non standard OEMs like MIUI and Huawei used non-standard system packageIds which then weren't allowed for that type.

@peter23 helped patched that here: https://github.com/iBotPeaches/platform_frameworks_base/commit/33c2310767810990c2e7af4b5645aa6848f168b8

Which introduced the --forced-package-id parameter, so we could force those non-standard OEMs who used values like (7) for a pkgId. However, this is a local application specific resource as denoted by the 0x7f type for 127.

So if we take a look at our logic in aapt1. That is the APP_PACKAGE_ID value referenced in the aapt1 sourcecode. The hacky patch above by @jtothe4n seems to rewrite the resourceIds from "^attr-private" to "attr" type which then is no longer bound by the constraints surronding the "^attr-private" type. Perhaps @peter23 has a bit more research here while I continue to investigate.

iBotPeaches commented 6 years ago

Okay, research on this is a stand still. I didn't discover anything more, I'm going to work on something crazy and that is #1689 (which is moving to aapt2). More and more applications are built (by default in Android Studio) in aapt2, then decoded by Apktool and attempted to be built in aapt1.

I need to keep patching aapt1 which is proving to be difficult as shit, especially when your low level knowledge of the AOSP build system sucks. Moving dependencies to be static, forcing 32 bit builds etc is not knowledge I have for AOSP.

I fear one day aapt2 will become the only solution and aapt1 no longer functions. I am starting the long, slow battle to moving to aapt2 and hoping this will be resolved when I solve that.

iBotPeaches commented 6 years ago

So we get a different failure in aapt2

W: brut_util_Jar_7457333169979251814.tmp F 04-11 16:50:17 12878 11417869 PrivateAttributeMover.cpp:84] Check failed: priv_attr_type->entries.empty() 

I can remove this check and rebuild aapt2, but it must be there for a reason. I'll do some digging.

Context: https://github.com/aosp-mirror/platform_frameworks_base/commit/ceb9b2f80f853059233cdd29057f39a5960a74ae#diff-b7115508fd7fbd06c6f209d56d0e5e34R84

LeeDroid- commented 6 years ago

Thanks for your persistence with this one @iBotPeaches , its quite frustrating! 👍

IgorEisberg commented 5 years ago

Seems like using AAPT2 was not necessary here. File: frameworks/base/tools/aapt/ResourceTable.cpp Function: status_t ResourceTable::assignResourceIds() Change:

        if (mPackageType == System) {
            p->movePrivateAttrs();
        }

To:

        p->movePrivateAttrs();

This allowed the APK to be recompiled, and another decompile resulted in the same resources, so seems to work fine.

ghost commented 4 years ago

Seems like using AAPT2 was not necessary here. File: frameworks/base/tools/aapt/ResourceTable.cpp Function: status_t ResourceTable::assignResourceIds() Change:

        if (mPackageType == System) {
            p->movePrivateAttrs();
        }

To:

        p->movePrivateAttrs();

This allowed the APK to be recompiled, and another decompile resulted in the same resources, so seems to work fine.

Do you have the aapt.exe file that is modified to do this that you can link a download to by chance?

IgorEisberg commented 4 years ago

Do you have the aapt.exe file that is modified to do this that you can link a download to by chance?

Nope.

ghost commented 4 years ago

@iBotPeaches just tried version version 2.4.1 and this problem still exist. It has been around for 2 years now. Any chance of getting it fixed soon? How much money can I throw at you for it to be fixed :) I have tried clearing frameworks, using aapt2 and still same thing :( I don't know how to package up the aapt2 so I can change

if (mPackageType == System) { p->movePrivateAttrs(); }

to

p->movePrivateAttrs();

And it seems like more and more apps are using ^attr-private now.

MrIkso commented 3 years ago

@iBotPeaches this esay fixed in aapt2 frameworks/base/tools/aapt2/ResourceUtils.cpp //add support private attributes //ignore /* if (!type.empty() && type != "attr") { return false; } */ https://github.com/MrIkso/sdk-tools/blob/6e2008c296093cb1585d91b89fbf9faf296c70cf/android-11/frameworks/base/tools/aapt2/ResourceUtils.cpp#L224

iBotPeaches commented 3 years ago

@iBotPeaches this esay fixed in aapt2 frameworks/base/tools/aapt2/ResourceUtils.cpp //add support private attributes //ignore /* if (!type.empty() && type != "attr") { return false; } */ https://github.com/MrIkso/sdk-tools/blob/6e2008c296093cb1585d91b89fbf9faf296c70cf/android-11/frameworks/base/tools/aapt2/ResourceUtils.cpp#L224

Can you put up a pull request to https://github.com/iBotPeaches/platform_frameworks_base on master? I'm confused a bit on the diff, are private attributes not attributes? I can't follow the if statement.

MrIkso commented 3 years ago

Perhaps I'll try to make a pull request with my own fixes that make resource compilation more confident.

nanjingdaqi commented 3 years ago

@iBotPeaches this esay fixed in aapt2 frameworks/base/tools/aapt2/ResourceUtils.cpp //add support private attributes //ignore /* if (!type.empty() && type != "attr") { return false; } */ https://github.com/MrIkso/sdk-tools/blob/6e2008c296093cb1585d91b89fbf9faf296c70cf/android-11/frameworks/base/tools/aapt2/ResourceUtils.cpp#L224

Can you put up a pull request to https://github.com/iBotPeaches/platform_frameworks_base on master? I'm confused a bit on the diff, are private attributes not attributes? I can't follow the if statement.

@iBotPeaches Excuse me. How to make the aapt binary in the repo: https://github.com/iBotPeaches/platform_frameworks_base ?

iBotPeaches commented 3 years ago

@iBotPeaches this esay fixed in aapt2 frameworks/base/tools/aapt2/ResourceUtils.cpp //add support private attributes //ignore /* if (!type.empty() && type != "attr") { return false; } */ https://github.com/MrIkso/sdk-tools/blob/6e2008c296093cb1585d91b89fbf9faf296c70cf/android-11/frameworks/base/tools/aapt2/ResourceUtils.cpp#L224

Can you put up a pull request to https://github.com/iBotPeaches/platform_frameworks_base on master? I'm confused a bit on the diff, are private attributes not attributes? I can't follow the if statement.

@iBotPeaches Excuse me. How to make the aapt binary in the repo: https://github.com/iBotPeaches/platform_frameworks_base ?

https://github.com/iBotPeaches/Apktool/blob/master/INTERNAL.md#building-aapt-binaries

aoxhwjfoavdlhsvfpzha commented 11 months ago
I: Checking whether sources has changed...
I: Checking whether sources has changed...
I: Checking whether sources has changed...
I: Checking whether sources has changed...
I: Checking whether resources has changed...
I: Building resources...
W: /home/t/Downloads/NfcNci/res/color/nfc_icon.xml:5: error: Error: Attribute is not public. (at 'color' with value '?android:^attr-private/closeItemLayout').
W: 
W: /home/t/Downloads/NfcNci/res/color-night/nfc_icon.xml:5: error: Error: Attribute is not public. (at 'color' with value '?android:^attr-private/clickColor').
W: 
brut.androlib.exceptions.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [/tmp/brut_util_Jar_99407728250395273501252018577728462556.tmp, p, --forced-package-id, 127, --min-sdk-version, 33, --target-sdk-version, 33, --version-code, 33, --version-name, 13, --no-version-vectors, -F, /tmp/APKTOOL4632439189647828713.tmp, -e, /tmp/APKTOOL5399276601489171376.tmp, -0, arsc, -I, /home/t/.local/share/apktool/framework/1.apk, -S, /home/t/Downloads/NfcNci/res, -M, /home/t/Downloads/NfcNci/AndroidManifest.xml]

Running into the same error, any chance there's been a solution?

Apktool v2.8.1 Linux Apk pulled from pixel 7 running Lineageos 20

Apologies if this isn't the right place for this, I don't use github very frequently. Thanks for any help

gr4ybull commented 11 months ago
I: Checking whether sources has changed...
I: Checking whether sources has changed...
I: Checking whether sources has changed...
I: Checking whether sources has changed...
I: Checking whether resources has changed...
I: Building resources...
W: /home/t/Downloads/NfcNci/res/color/nfc_icon.xml:5: error: Error: Attribute is not public. (at 'color' with value '?android:^attr-private/closeItemLayout').
W: 
W: /home/t/Downloads/NfcNci/res/color-night/nfc_icon.xml:5: error: Error: Attribute is not public. (at 'color' with value '?android:^attr-private/clickColor').
W: 
brut.androlib.exceptions.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [/tmp/brut_util_Jar_99407728250395273501252018577728462556.tmp, p, --forced-package-id, 127, --min-sdk-version, 33, --target-sdk-version, 33, --version-code, 33, --version-name, 13, --no-version-vectors, -F, /tmp/APKTOOL4632439189647828713.tmp, -e, /tmp/APKTOOL5399276601489171376.tmp, -0, arsc, -I, /home/t/.local/share/apktool/framework/1.apk, -S, /home/t/Downloads/NfcNci/res, -M, /home/t/Downloads/NfcNci/AndroidManifest.xml]

Running into the same error, any chance there's been a solution?

Apktool v2.8.1 Linux Apk pulled from pixel 7 running Lineageos 20

Apologies if this isn't the right place for this, I don't use github very frequently. Thanks for any help

Anyone? @iBotPeaches

iBotPeaches commented 11 months ago

Please use new ticket. This is 2 years old and I'd prefer to have a new issue with the template filled out for triage.

huhuang03 commented 5 months ago

@iBotPeaches this esay fixed in aapt2 frameworks/base/tools/aapt2/ResourceUtils.cpp //add support private attributes //ignore /* if (!type.empty() && type != "attr") { return false; } */ https://github.com/MrIkso/sdk-tools/blob/6e2008c296093cb1585d91b89fbf9faf296c70cf/android-11/frameworks/base/tools/aapt2/ResourceUtils.cpp#L224

Can you put up a pull request to https://github.com/iBotPeaches/platform_frameworks_base on master? I'm confused a bit on the diff, are private attributes not attributes? I can't follow the if statement.

@iBotPeaches Excuse me. How to make the aapt binary in the repo: https://github.com/iBotPeaches/platform_frameworks_base ?

https://github.com/iBotPeaches/Apktool/blob/master/INTERNAL.md#building-aapt-binaries

So, this already included in latest apktool version 2.9.3?

The new issue is something like this:

W: D:\source\.\apk_bb\res\values\public.xml:634: error: no definition for declared symbol 'com.jilrew.bb:^attr-private/el_contentLayout'.
W: D:\source\.\apk_bb\res\values\public.xml:635: error: no definition for declared symbol 'com.jilrew.bb:^attr-private/el_duration'.

And it's a application(not system) level app.