Add support for apks with different resources package and manifest package (--rename-manifest-package) #363

Closed
iBotPeaches opened this Issue Mar 18, 2015 · 18 comments

Comments

Projects
None yet
1 participant
@iBotPeaches
Owner

iBotPeaches commented Mar 18, 2015

Original issue 252 created by Brut.alll on 2011-12-06T00:34:25.000Z:

If you use --rename-manifest-package option to aapt when building resources something weird happens. Resources are built for old package name, but manifest uses new one. This makes resources broken a little when using current apktool, because they're built for new package name, so resulting apk is a little different than original one.

In most cases it doesn't matter because resource package name isn't that important. However in some cases it is, e.g.:

  • I think framework apks with changed resource package shouldn't work anymore
  • if apk declares its own attrs and uses them in XMLs, they're referenced by package name, so references are broken after decoding - you won't be able to build apk back.

Problem can be reproduced very easily: just create new Android project, define some attr, use it in XML and then build resources with --rename-manifest-package. You will be able to apktool-decode resulting apk, but you will get "error: No resource identifier found for attribute" errors when building back.

Proper solution to this problem is to revert manifest package back to original one. So:

  • check if resources package is different than the one in AndroidManifest.xml
  • if so then rename android:package to that found in resources.arsc and store original android:package in apktool.yml
  • when building use --rename-manifest-package if it is found in apktool.yml

Also in the case of apk-with-attrs files it should be possible to fix the problem quite easily without apktool support: just replace "http://schemas.android.com/apk/res/${originalPackage}" with "http://schemas.android.com/apk/res/${renamedPackage}" in all XML files. You can do this in unix by e.g.:

find res -type f | xargs perl -pi -e 's/http://schemas.android.com/apk/res/com.touchtype/http://schemas.android.com/apk/res/com.touchtype.swiftkey/g'

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #1 originally posted by connor.tumbleson on 2012-11-18T17:38:43.000Z:

Yep, need to fix this for 4.2. It seems lots of APKs are starting to do this.

Owner

iBotPeaches commented Mar 18, 2015

Comment #1 originally posted by connor.tumbleson on 2012-11-18T17:38:43.000Z:

Yep, need to fix this for 4.2. It seems lots of APKs are starting to do this.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #2 originally posted by zsolt.muller on 2012-11-18T19:15:51.000Z:

It's already there in 4.1.2 APKs. Eg. I just tried to decode GalleryGoogle.apk+GalleryGoogle.odex. So far so good, it didn't produce any errors. Then I tried to build it back into an APK and got tons of the "No resource identifier found for attribute '...' in package '...'" errors.

Owner

iBotPeaches commented Mar 18, 2015

Comment #2 originally posted by zsolt.muller on 2012-11-18T19:15:51.000Z:

It's already there in 4.1.2 APKs. Eg. I just tried to decode GalleryGoogle.apk+GalleryGoogle.odex. So far so good, it didn't produce any errors. Then I tried to build it back into an APK and got tons of the "No resource identifier found for attribute '...' in package '...'" errors.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #3 originally posted by zsolt.muller on 2012-11-18T19:28:10.000Z:

The suggested workaround/fix worked though.

I had to run the following to replace in the resource XML files the "com.android.gallery3d" resource package name with the actual package name (from the manifest), which is "com.google.android.gallery3d":

find res -type f | xargs perl -pi -e 's/http://schemas.android.com/apk/res/com.android.gallery3d/http://schemas.android.com/apk/res/com.google.android.gallery3d/g'

Owner

iBotPeaches commented Mar 18, 2015

Comment #3 originally posted by zsolt.muller on 2012-11-18T19:28:10.000Z:

The suggested workaround/fix worked though.

I had to run the following to replace in the resource XML files the "com.android.gallery3d" resource package name with the actual package name (from the manifest), which is "com.google.android.gallery3d":

find res -type f | xargs perl -pi -e 's/http://schemas.android.com/apk/res/com.android.gallery3d/http://schemas.android.com/apk/res/com.google.android.gallery3d/g'

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #4 originally posted by jvisser13579 on 2012-11-18T21:03:26.000Z:

I can confirm this will work.
Only thing I had to do also is add:

parent="@*android:style/TextAppearance.Medium"

to line 168 of the styles.xml file of the GalleryGoogle output.

Owner

iBotPeaches commented Mar 18, 2015

Comment #4 originally posted by jvisser13579 on 2012-11-18T21:03:26.000Z:

I can confirm this will work.
Only thing I had to do also is add:

parent="@*android:style/TextAppearance.Medium"

to line 168 of the styles.xml file of the GalleryGoogle output.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #5 originally posted by connor.tumbleson on 2012-11-18T21:09:36.000Z:

Issue 227 has been merged into this issue.

Owner

iBotPeaches commented Mar 18, 2015

Comment #5 originally posted by connor.tumbleson on 2012-11-18T21:09:36.000Z:

Issue 227 has been merged into this issue.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #6 originally posted by zsolt.muller on 2012-11-25T18:36:30.000Z:

My previous statement was a bit premature. The search&replace in the decoded resource XML files works only as far as apktool's build command is involved. I.e. the "apktool b GalleryGoogle GalleryGoogle.apk" command completes successfully, but putting the rebuilt APK on the phone and launching the camera app, it crashes, when I switch to camcorder mode.

However grabbing classes.dex from the rebuilt APK and putting it into the factory APK (i.e. unzipping the original APK, putting the classes.dex -that I took from the rebuilt APK- into the unzipped folder, rezip the contents of the folder into a new APK and sign the APK) works indeed.

So I guess I'll have to wait for Brut.alll's proper solution. Until then I'll have to manually do the APK rebuild so I can add the --rename-manifest-package option to the aapt commandline. I just checked the current state (from the GIT repo) and it seems the fix is not yet there. Hopefully it'll make into the 1.5.1 version. :-)

Owner

iBotPeaches commented Mar 18, 2015

Comment #6 originally posted by zsolt.muller on 2012-11-25T18:36:30.000Z:

My previous statement was a bit premature. The search&replace in the decoded resource XML files works only as far as apktool's build command is involved. I.e. the "apktool b GalleryGoogle GalleryGoogle.apk" command completes successfully, but putting the rebuilt APK on the phone and launching the camera app, it crashes, when I switch to camcorder mode.

However grabbing classes.dex from the rebuilt APK and putting it into the factory APK (i.e. unzipping the original APK, putting the classes.dex -that I took from the rebuilt APK- into the unzipped folder, rezip the contents of the folder into a new APK and sign the APK) works indeed.

So I guess I'll have to wait for Brut.alll's proper solution. Until then I'll have to manually do the APK rebuild so I can add the --rename-manifest-package option to the aapt commandline. I just checked the current state (from the GIT repo) and it seems the fix is not yet there. Hopefully it'll make into the 1.5.1 version. :-)

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #7 originally posted by connor.tumbleson on 2012-11-26T03:56:37.000Z:

Issue 357 has been merged into this issue.

Owner

iBotPeaches commented Mar 18, 2015

Comment #7 originally posted by connor.tumbleson on 2012-11-26T03:56:37.000Z:

Issue 357 has been merged into this issue.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #8 originally posted by connor.tumbleson on 2012-11-29T03:07:04.000Z:

About 70% with this little patch. All is going well currently. Just lost a bit of time tracing down an AXmlPullParser problem, which ended up being the fact that the namespace isn't attached to the package name in AndroidManifest.xml which made it quite difficult to grab. Slight oversight on my part.

All that aside. Things are going well. Haven't had as much time as I wish.

Owner

iBotPeaches commented Mar 18, 2015

Comment #8 originally posted by connor.tumbleson on 2012-11-29T03:07:04.000Z:

About 70% with this little patch. All is going well currently. Just lost a bit of time tracing down an AXmlPullParser problem, which ended up being the fact that the namespace isn't attached to the package name in AndroidManifest.xml which made it quite difficult to grab. Slight oversight on my part.

All that aside. Things are going well. Haven't had as much time as I wish.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #9 originally posted by solitaryxheart on 2012-12-08T22:59:46.000Z:

Just a quick question, is there any support yet, or fix/workaround for decompiling android 4.2 framework-res.apk? I posted issue 357 in comment 7 so you can find the errors. It simply errors at "decompiling sources" and thats it.

Thanks!

Owner

iBotPeaches commented Mar 18, 2015

Comment #9 originally posted by solitaryxheart on 2012-12-08T22:59:46.000Z:

Just a quick question, is there any support yet, or fix/workaround for decompiling android 4.2 framework-res.apk? I posted issue 357 in comment 7 so you can find the errors. It simply errors at "decompiling sources" and thats it.

Thanks!

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #10 originally posted by connor.tumbleson on 2012-12-14T03:17:34.000Z:

Apktool correctly grabs the renamed and original package name and stores it in apktool.yml

packageInfo:
cur_package: com.android.inputmethod.latin
orig_package: com.google.android.inputmethod.latin

Now, we simply rename the manifest to cur_package, but during build use --rename w/ orig_package.

Should have a Apktool v1.5.1 Pre Release 2 ready in a few days.

Owner

iBotPeaches commented Mar 18, 2015

Comment #10 originally posted by connor.tumbleson on 2012-12-14T03:17:34.000Z:

Apktool correctly grabs the renamed and original package name and stores it in apktool.yml

packageInfo:
cur_package: com.android.inputmethod.latin
orig_package: com.google.android.inputmethod.latin

Now, we simply rename the manifest to cur_package, but during build use --rename w/ orig_package.

Should have a Apktool v1.5.1 Pre Release 2 ready in a few days.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #11 originally posted by connor.tumbleson on 2012-12-19T02:11:19.000Z:

I believe I have finally fixed this, testing continues but otherwise its Fixed.

Owner

iBotPeaches commented Mar 18, 2015

Comment #11 originally posted by connor.tumbleson on 2012-12-19T02:11:19.000Z:

I believe I have finally fixed this, testing continues but otherwise its Fixed.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #12 originally posted by solitaryxheart on 2012-12-23T13:45:05.000Z:

may i get my hands on it? i would like to do some testing as well :)

Owner

iBotPeaches commented Mar 18, 2015

Comment #12 originally posted by solitaryxheart on 2012-12-23T13:45:05.000Z:

may i get my hands on it? i would like to do some testing as well :)

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #13 originally posted by connor.tumbleson on 2012-12-23T13:57:02.000Z:

http://miui.connortumbleson.com/other/apktool/test_versions/apktool_1.5.1PR2.jar

Apktool v1.5.1 PR 2

Owner

iBotPeaches commented Mar 18, 2015

Comment #13 originally posted by connor.tumbleson on 2012-12-23T13:57:02.000Z:

http://miui.connortumbleson.com/other/apktool/test_versions/apktool_1.5.1PR2.jar

Apktool v1.5.1 PR 2

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #14 originally posted by solitaryxheart on 2012-12-23T14:11:50.000Z:

:o OMG it worked! No errors!! :D OMG i love you BEST CHRISTMAS PRESENT EVERR! :D thanks so much you seriously are amazing!!! :)

Owner

iBotPeaches commented Mar 18, 2015

Comment #14 originally posted by solitaryxheart on 2012-12-23T14:11:50.000Z:

:o OMG it worked! No errors!! :D OMG i love you BEST CHRISTMAS PRESENT EVERR! :D thanks so much you seriously are amazing!!! :)

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #15 originally posted by SvartSyn333 on 2012-12-26T11:46:54.000Z:

So I managed to decompile, change /res/values/colors.xml, then doing find res -type f | xargs perl -pi -e 's/http://schemas.android.com/apk/res/com.touchtype/http://schemas.android.com/apk/res/com.touchtype.swiftkey/g' and finally compile, sign and install. But everytime I want to open SwiftKey's design settings, I get an FC. Anyone knows why? :(

Owner

iBotPeaches commented Mar 18, 2015

Comment #15 originally posted by SvartSyn333 on 2012-12-26T11:46:54.000Z:

So I managed to decompile, change /res/values/colors.xml, then doing find res -type f | xargs perl -pi -e 's/http://schemas.android.com/apk/res/com.touchtype/http://schemas.android.com/apk/res/com.touchtype.swiftkey/g' and finally compile, sign and install. But everytime I want to open SwiftKey's design settings, I get an FC. Anyone knows why? :(

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #16 originally posted by alex.mgu on 2013-01-10T12:18:50.000Z:

I tried to use new version Apktool v1.5.1 PR 2. But I didn't understand where I need to put this string "--rename w/ orig_package".
I used this command:
C:\Users\admin>apktool b --rename w/ com.touchtype.swiftkey c:\1\6

If I build an app as usual (with this command: C:\Users\admin>apktool b c:\1\6) but with new version of apktool, the command works, but there are still errors.

Example:
c:\1\8\res\values-v14\styles.xml:4: error: Error retrieving parent for item: Noresource found that matches the given name @*android:style/Theme.DeviceDefault.Dialog'.

P.S. packageInfo:
cur_package: android
orig_package: com.touchtype.swiftkey

Owner

iBotPeaches commented Mar 18, 2015

Comment #16 originally posted by alex.mgu on 2013-01-10T12:18:50.000Z:

I tried to use new version Apktool v1.5.1 PR 2. But I didn't understand where I need to put this string "--rename w/ orig_package".
I used this command:
C:\Users\admin>apktool b --rename w/ com.touchtype.swiftkey c:\1\6

If I build an app as usual (with this command: C:\Users\admin>apktool b c:\1\6) but with new version of apktool, the command works, but there are still errors.

Example:
c:\1\8\res\values-v14\styles.xml:4: error: Error retrieving parent for item: Noresource found that matches the given name @*android:style/Theme.DeviceDefault.Dialog'.

P.S. packageInfo:
cur_package: android
orig_package: com.touchtype.swiftkey

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #17 originally posted by connor.tumbleson on 2013-01-10T12:41:21.000Z:

That has nothing to do w/ the --rename. Its automatically done. Thats just a bug according to styles.xml.

Post a new bug with apk and full error log.

Owner

iBotPeaches commented Mar 18, 2015

Comment #17 originally posted by connor.tumbleson on 2013-01-10T12:41:21.000Z:

That has nothing to do w/ the --rename. Its automatically done. Thats just a bug according to styles.xml.

Post a new bug with apk and full error log.

@iBotPeaches

This comment has been minimized.

Show comment
Hide comment
@iBotPeaches

iBotPeaches Mar 18, 2015

Owner

Comment #18 originally posted by alex.mgu on 2013-01-10T12:55:54.000Z:

OK, I've attached the log and the folder, which I want to build.
P.S. I zipped the folder for attaching.

Owner

iBotPeaches commented Mar 18, 2015

Comment #18 originally posted by alex.mgu on 2013-01-10T12:55:54.000Z:

OK, I've attached the log and the folder, which I want to build.
P.S. I zipped the folder for attaching.

@eighthave eighthave referenced this issue in androguard/androguard Jul 13, 2018

Open

resources and manifest #503

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment