How to remove unused Google+ icons imported from Google Play Services library

孤人 提交于 2019-12-21 20:37:02

问题


I've migrated the AdMobs banner ads to Google Play Services in my Android Studio project. The resulting APK mushroomed from 0.7 MB to over 1.6 MB. Fortunately ProGuard knocked it down to just over 1 MB. I poked around the APK and noticed that 280 kB of "g+" images have been added to

res/
 drawable-hdpi
 drawable-mdpi
 drawable-xhdpi
 drawable-xxhdpi

I have no plans to ever use Google+ in this app. It's a utility app and there's no need to deal with social media. 280kB may sound like peanuts compared to multi-Megabyte apps, but being a relatively simple utility, users want it to be as small as possible. It just feels dirty releasing a new version that blows up from ~700 kB to over 1000 kB with no tangible feature improvements for the user.

Ideally I'd like to exclude those icons for just one project, but I could live with a global solution for now.


回答1:


If you are using Gradle you can use the shrinkResources option in combination with ProGuard to exclude resources. This deletes both your resources, as well as resources that your depndencies are using.

buildTypes {
  release {
    minifyEnabled true
    shrinkResources true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  }
}

As of April 2015 there still seems to be a bug because of which some resources (including Google Play icons) would not get removed. You can work around this by creating a keep.xml file and specifying which resources should be removed. Remember to test your APK after that because removing resources that are actually needed might crash your app!

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
   tools:discard="@drawable/common_signin_*,@drawable/ic_plusone_*,@drawable/powered_by_google_*"/>

Removing these images reduces the size of the APK by about 300kB.




回答2:


I had this error:

" ...\Application\build\intermediates\exploded-aar\com.google.android.gms\play-services-base\7.3.0\res\drawable-xxhdpi\common_signin_btn_icon_disabled_focus_light.9.png ERROR: Unable to open PNG file "

Android studio was unable to open the common_signin_btn_icon_disabled_focus_light.9.png file.

To solve this I moved the project folder directory up a level or two to reduce path length. Everything worked perfectly after that.

I used "D:\the_project_folder". Worked for me. No more compile errors.

thanks to this link:

https://github.com/googlesamples/android-XYZTouristAttractions/issues/2

Cheers!




回答3:


So far the two best options are to add tiny bitmaps or aliases to the project named the same as the g+ icons. These supersede the icons in the GPS library. The icons are in each of the drawable-* directories with the following filenames:

res/
 drawable-hdpi/
 drawable-mdpi/
 drawable-xhdpi/
 drawable-xxhdpi/
        common_signin_btn_icon_disabled_dark.9.png
        common_signin_btn_icon_disabled_focus_dark.9.png
        common_signin_btn_icon_disabled_focus_light.9.png
        common_signin_btn_icon_disabled_light.9.png
        common_signin_btn_icon_focus_dark.9.png
        common_signin_btn_icon_focus_light.9.png
        common_signin_btn_icon_normal_dark.9.png
        common_signin_btn_icon_normal_light.9.png
        common_signin_btn_icon_pressed_dark.9.png
        common_signin_btn_icon_pressed_light.9.png
        common_signin_btn_text_disabled_dark.9.png
        common_signin_btn_text_disabled_focus_dark.9.png
        common_signin_btn_text_disabled_focus_light.9.png
        common_signin_btn_text_disabled_light.9.png
        common_signin_btn_text_focus_dark.9.png
        common_signin_btn_text_focus_light.9.png
        common_signin_btn_text_normal_dark.9.png
        common_signin_btn_text_normal_light.9.png
        common_signin_btn_text_pressed_dark.9.png
        common_signin_btn_text_pressed_light.9.png
        ic_plusone_medium_off_client.png
        ic_plusone_small_off_client.png
        ic_plusone_standard_off_client.png
        ic_plusone_tall_off_client.png

I created a tiny 9-patch bitmap copied and named as each of those files in the drawable dirs. You can barely see it here --->

The alternative is to create aliases. One bitmap alias per file so you end up with the same number of files. For example:

res/
 drawable/
        blank.png
 drawable-xxhdpi/
        common_signin_btn_icon_disabled_dark.9.xml
        ...

Each of the alias XMLs references the tiny one:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/blank" />

The size difference is negligible between the tiny bitmap and the alias. Yes it's a hack, but until Google/ProGuard automatically removes unused bitmaps, this may be the only way to recover the wasted space. Thanks to CommonsWare for the assist.




回答4:


As a simpler alternative, just delete the .png files in drawable-xxhdpi, drawable-xhdpi and drawable-hdpi, leaving the ones in drawable-mdpi. Of course, check that the filename also exists in drawable-mdpi, first.

Leaving one valid resolution is safer IMHO and the drawable-mdpi files take up less than 30KB which may be acceptable for many people.



来源:https://stackoverflow.com/questions/22967839/how-to-remove-unused-google-icons-imported-from-google-play-services-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!