Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrink Google Play Services library for use with Google Analytics only

Is it possible to remove unnecessary packages from the Google Play Services library so that the library will only contain everything needed for Google Analytics?

I'm asking this because the Google Play Services library is bloated with extra packages, images and resources of which I think are unused by Google Analytics.

like image 839
Rolf ツ Avatar asked Nov 12 '14 15:11

Rolf ツ


People also ask

What is the size of Google Play services?

That's 40MB. 0.04GB. I wouldn't worry about it too much even on a phone with 8GB of storage. I will share with you guys my experience with my 46 GB google services space taking of my phone storage...

Is Google Play services necessary?

Google Play services helps to ensure the security and reliability of an Android device, and keep devices updated with the latest security features. This includes: Google Play Protect, which can warn users if an app contains known malware.

What is Gplay process?

Google Play, formerly known as Android Market, is the official distribution storefront for Android applications and other digital media, such a music, movies and books, from Google. It is available on mobile devices and tablets that run the Android operating system (OS), supported Chrome OS devices and on the web.

What are basement play services?

The library play-services-basement is a dependency of play-services-base . It was introduced in Google Play Service version 8.1. 0 to help to reduce the size of some other libraries like play-services-ads and play-services-analytics .


2 Answers

Proguard removes unused code from jar files by default also from the Google Play Services library. But proguard cant be used to remove (unused) resources, but I have found that there are other options to further optimize the library.

I was able using method two to shrink an app from 1827 KB down to 1115 KB. I tested the app and it works fine and Google Analytics statics are appearing on the online dashboard as expected.


Method 1: Just include the google-play-services.jar file and ignore R warnings.

The first method is probably the most easy.

Step 1: Copy the google-play-services.jar file from the library project to your own project and past it in the libs folder.

Step 2: Remove the library reference to the original google-play-services_lib project from your project.

Right click on your project -> Properties -> Android -> Select library -> Remove

Step 3: Add dontwarn flags to your progruard settings file.

-dontwarn com.google.**.R
-dontwarn com.google.**.R$*

Explanation: The google-play-services-jar contains references to generated Java files (R.java). These files are normally generated when the google-play-services_lib project is being build. Proguard will normally warn you (By exiting with an error) that these files are missing when optimizing the google-play-services.jar file. By adding the dontwarn flags proguard will just ignore the fact that these files are missing and won't exit with an error.


Method 2: Create an optimized google-play-services(-analytics).jar

Method two is an more optimized version of method one. The difference is that instead of copying the google-play-services.jar file from the library project to your own project you first optimize the library using proguard.

So what you want to do is manually run proguard on just the google-play-services.jar file. For keeping just Google Analytics I used this proguard settings file.

-injars google-play-services.jar
-outjars google-play-services-analytics.jar

-libraryjars [add your path to sdk]/sdk/extras/android/support/v4/android-support-v4.jar
-libraryjars [add your path to sdk]/sdk/platforms/android-21/android.jar

-dontoptimize
-dontobfuscate
-dontwarn com.google.**.R
-dontwarn com.google.**.R$*
-dontnote

-keep public class com.google.android.gms.analytics.**, com.google.android.gms.common.**, com.google.android.gms.location.** {
    public protected *;
}

-keep class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    java.lang.String NULL;
}

Step 1: Copy the google-play-services.jar file from the library project to the sdk\tools\proguard\lib folder.

Step 2: Copy your proguard settings file to the sdk\tools\proguard\lib folder.

Step 3: Run progaurd with your settings file:

On windows: Open command window -> Navigate to the sdk\tools\proguard\lib folder -> Run proguard using:

java -jar proguard.jar @analytics-settings.txt

Step 4: Use the first method explained in this post to add the optimized google-play-services-analytics.jar to your project. (Don't forget Step 3 in the first method!)


Optimization results

Before + proguard = 1827 KB
Method 1 = 1206 KB
Method 2 = 1115 KB
like image 112
Rolf ツ Avatar answered Nov 12 '22 14:11

Rolf ツ


in android-studio , you can choose which library like to use :

UPDATE 13 JUNE , 2017

u can use this release of play-services :

  • June 2017 - version 11.0.0
  • May 2017 - verson 10.2.6
  • April 2017 - version 10.2.4
  • March 2017 - version 10.2.1
  • February 2017 - version 10.2
  • November 2016 - version 10.0
  • October 2016 - version 9.8
  • September 2016 - version 9.6
  • August 2016 - version 9.4
  • June 2016 - version 9.2
  • May 2016 - version 9.0
  • December 2015 - version 8.4
  • November 2015 - version 8.3
  • September 2015 - version 8.1
  • August 2015 - version 7.8
  • May 2015 - version 7.5

Google+

com.google.android.gms:play-services-plus:11.0.0

Google Account Login

com.google.android.gms:play-services-auth:11.0.0

Google Actions, Base Client Library

com.google.android.gms:play-services-base:11.0.0

Google Address API

com.google.android.gms:play-services-identity:11.0.0

Google App Indexing

com.google.android.gms:play-services-appindexing:11.0.0

Google App Invites

com.google.android.gms:play-services-appinvite:11.0.0

Google Analytics

com.google.android.gms:play-services-analytics:11.0.0

Google Cast

com.google.android.gms:play-services-cast:11.0.0

Google Cloud Messaging

com.google.android.gms:play-services-gcm:11.0.0

Google Drive

com.google.android.gms:play-services-drive:11.0.0

Google Fit

com.google.android.gms:play-services-fitness:11.0.0

Google Location, Activity Recognition, and Places

com.google.android.gms:play-services-location:11.0.0

Google Maps

com.google.android.gms:play-services-maps:11.0.0

Google Mobile Ads

com.google.android.gms:play-services-ads:11.0.0

Mobile Vision

com.google.android.gms:play-services-vision:11.0.0

Google Nearby

com.google.android.gms:play-services-nearby:11.0.0

Google Panorama Viewer

com.google.android.gms:play-services-panorama:11.0.0

Google Play Game services

com.google.android.gms:play-services-games:11.0.0

SafetyNet

com.google.android.gms:play-services-safetynet:11.0.0

Google Wallet

com.google.android.gms:play-services-wallet:11.0.0

Android Wear

com.google.android.gms:play-services-wearable:11.0.0
like image 32
Adnan Abdollah Zaki Avatar answered Nov 12 '22 14:11

Adnan Abdollah Zaki