Closed Bug 1313990 Opened 8 years ago Closed 8 years ago

Convert non-transparent image resources from png to webp for 14kb APK size reduction

Categories

(Firefox Build System :: Android Studio and Gradle Integration, defect, P1)

defect

Tracking

(firefox52 fixed)

RESOLVED FIXED
mozilla52
Iteration:
1.8
Tracking Status
firefox52 --- fixed

People

(Reporter: ahunt, Assigned: ahunt)

References

Details

(Whiteboard: [MobileAS])

Attachments

(1 file)

webp images are reported to be approximately 25% smaller on average than an optimised png [1]

Android supports webp for resource files from 4.0 (although it seems lossless images, and image with transparency, are only available on Android 4.2.1+ - this still needs to be investigated) - [2].

We currently support Fennec on Android 4.0.3+, so depending on the impacts of lossless/transparent image support we may or may not be able to convert to webp images.

Converting all our own resources to webp resulted in a 200kb apk size reduction. Also converting bundled support library resources resulted in a further 200kb saving, but that might not be viable for landing (doing this requires hacking webp conversion into our mach builds, however I do not know if it's possible to add a similar conversion in gradle builds).

Both of those experimental builds run without issues on a 4.4.4 device, I'm yet to test on 4.0.3.

We are also currently investigating using VectorDrawables in place of png/webp, however we won't be able to use VectorDrawable's in all situations (they also aren't recommended for images larger than 200dp x 200 dp), so we should still investigate webp support, and ideally enforce the use of webp in place of png.

[1] https://developers.google.com/speed/webp/docs/webp_lossless_alpha_study
[2] https://developer.android.com/guide/appendix/media-formats.html
Note: the following two image resources can't be converted if we convert just our own resources to webp. Both of these images seem to be intended to override support library resources, however I have a suspicion that the support library resources would actually override these - in which case we should either remove our own copies, or ensure that our own copies override the bundled version:
ic_media_play.png
ic_media_pause.png

We provide our own copies in:
/mobile/android/base/resources/drawable-hdpi/
/mobile/android/base/resources/drawable-xhdpi/
/mobile/android/base/resources/drawable-xxhdpi/

The mediarouter support library bundles its own copies, which would seem to override the Fennec copies, since they add the -v4 qualifier:
./OBJDIR/dist/exploded-aar/mediarouter-v7-23.4.0/res/drawable-hdpi-v4/
./OBJDIR/dist/exploded-aar/mediarouter-v7-23.4.0/res/drawable-mdpi-v4/
./OBJDIR/dist/exploded-aar/mediarouter-v7-23.4.0/res/drawable-xhdpi-v4/
./OBJDIR/dist/exploded-aar/mediarouter-v7-23.4.0/res/drawable-xxhdpi-v4/
Assignee: nobody → ahunt
Whiteboard: [MobileAS]
(In reply to Andrzej Hunt :ahunt from comment #1)
> Note: the following two image resources can't be converted if we convert
> just our own resources to webp. Both of these images seem to be intended to
> override support library resources, however I have a suspicion that the
> support library resources would actually override these - in which case we
> should either remove our own copies, or ensure that our own copies override
> the bundled version:
> ic_media_play.png
> ic_media_pause.png

I added those two for our media notification. If they use the same names than icons in the support library then this isn't intentional. We can rename them. :)
9-patch images don't seem to be supported for conversion either:
https://code.google.com/p/android/issues/detail?id=201704

In a local gradle build I get:
mobile/android/base/resources/drawable-hdpi/color_picker_row_bg.9.webp: Error: '.' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore

(mach build's didn't complain about this, so I don't know if this is an artificial gradle limit, or whether android would also fail to load such images)
Trying to run the webp apk on a 4.0.3 emulator results in a startup crash while inflating some part of the layout, so it seems that the lack of transparency support is going to be an issue.

Looks like this will need to wait until our baseline is 4.2.1 / API 17, or possibly even API 18 just to be safe (4.2.0 doesn't appear to be supported, so we might need to miss the whole API 17 series to be safe).
Assignee: ahunt → nobody
Summary: Convert image resources from png to webp for 200kb APK reduction → Convert image resources from png to webp for 200kb APK reduction once we upgrade to API 17 / 4.2.1 baseline
Whiteboard: [MobileAS]
Android 4.0.3, 4.0.4, 4.1, and 4.2 make up nearly 17% of our install base.

I'd be inclined to do some time-boxed testing to see if webp works at all, anywhere, on < 4.2.1 -- perhaps non-transparent images (first-run tour?).

If not, then WONTFIX this for now, because I would speculate it will be a year until we raise our minimum Android version requirement.
Turns out we only had a total of 6 definitely non-transparent pngs:

find . -name *.png -exec bash -c 'if ! `identify -format "%A" {}`; then echo {};fi' \;
./res/drawable-hdpi-v4/helper_readerview_bookmark.png
./res/drawable-hdpi-v4/notification_media.png
./res/drawable-xhdpi-v4/helper_readerview_bookmark.png
./res/drawable-xhdpi-v4/notification_media.png
./res/drawable-xxhdpi-v4/helper_readerview_bookmark.png
./res/drawable-xxhdpi-v4/notification_media.png

Converting just the non-transparent images appears to work on a 4.0.3 emulator without visible issues, so I'll try to land at least that.

The first-run images (which I believe are the largest images that we bundle) contain a transparent background which precludes us from doing a simple conversion, but I've filed Bug 1314328 to look at those.

While I'm at it, here's my conversion script - I wonder if a derivative thereof should be added to checkstyle?
find . -name *.png -exec bash -c 'if ! `identify -format "%A" {}`; then x={}; cwebp {} -o ${x%.png}.webp; rm {};fi' \;
Assignee: nobody → ahunt
Status: NEW → ASSIGNED
Iteration: --- → 1.8
Priority: -- → P1
Whiteboard: [MobileAS]
Summary: Convert image resources from png to webp for 200kb APK reduction once we upgrade to API 17 / 4.2.1 baseline → Convert non-transparent image resources from png to webp for 14kb APK size reduction
Comment on attachment 8805918 [details]
Bug 1313990 - Convert non-transparent png to webp for APK size reduction

https://reviewboard.mozilla.org/r/89526/#review89664
Attachment #8805918 - Flags: review?(s.kaspari) → review+
Pushed by ahunt@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/86966ebee065
Convert non-transparent png to webp for APK size reduction r=sebastian
https://hg.mozilla.org/mozilla-central/rev/86966ebee065
Status: ASSIGNED → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
Target Milestone: --- → Firefox 52
Product: Firefox for Android → Firefox Build System
Target Milestone: Firefox 52 → mozilla52
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: