Reduce the library of Google Play services for use only with Google Analytics

Is it possible to remove unnecessary packages from the Google Play Services library so that the library contains only everything that is necessary for Google Analytics?

I ask this because the Google Play Services library is bloated with additional packages, images, and resources that I think are not used by Google Analytics.

+6
source share
4 answers

Proguard removes unused code from the default jar files also from the Google Play Services library. But proguard can not can be used to remove (unused) resources, but I found that there are other options for further optimization of the library.

I was able to use method two to compress the application from 1827 KB to 1115 KB . I tested the application and it works great, and the Google Analytics statistics appear in the interactive dashboard, as expected.


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

The first way is probably the easiest.

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

Step 2: Remove the library link to the source google-play-services_lib project from your project.

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

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

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

Explanation: Google-play-services-jar contains links to generated Java files (R.java). These files are usually generated when the google-play-services_lib project is created. Proguard usually warns you (when you exit with an error) that these files are missing when optimizing the google-play-services.jar file. By adding dontwarn flags, proguard simply ignores the fact that these files are missing and will not exit with an error.


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

Method two is a more optimized version of 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 to manually run proguard only in google-play-services.jar. To store only 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 the proguard settings file to the sdk\tools\proguard\lib folder.

Step 3: Run progaurd with your settings file:

In the windows: Open a command window β†’ Go to the sdk\tools\proguard\lib folder β†’ Run proguard using:

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

Step 4: Use the first method described in this post to add optimized google-play-services-analytics.jar to your project. (Remember step 3 in the first method!)


Optimization Results

 Before + proguard = 1827 KB Method 1 = 1206 KB Method 2 = 1115 KB 
+13
source

in Android studio , you can choose which library to use:

UPDATE JUNE 13, 2017

u can use this release of gaming 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 

Sign in to your Google Account

 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 URL 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

 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 and Location Recognition

 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 
+9
source

Update for Google Play Services 6.5+ (gradle only)

With the release of Google Play Services 6.5, you can now simply include part of the library in the application project. At the moment, this only works for gradle-based ones.

Usually you add Play services as a dependency to your gradle file as follows:

 compile 'com.google.android.gms:play-services:9.4.0' 

Starting with version 6.5, you can specify which module you do not want to add. As shown in the example below for the Games and Analytics modules.

 Google+ com.google.android.gms:play-services-plus:9.4.0 Google Account Login com.google.android.gms:play-services-auth:9.4.0 Google Actions, Base Client Library com.google.android.gms:play-services-base:9.4.0 Google Address API com.google.android.gms:play-services-identity:9.4.0 Google App Indexing com.google.android.gms:play-services-appindexing:9.4.0 Google App Invites com.google.android.gms:play-services-appinvite:9.4.0 Google Analytics com.google.android.gms:play-services-analytics:9.4.0 Google Awareness com.google.android.gms:play-services-contextmanager:9.4.0 Google Cast com.google.android.gms:play-services-cast:9.4.0 Google Cloud Messaging com.google.android.gms:play-services-gcm:9.4.0 Google Drive com.google.android.gms:play-services-drive:9.4.0 Google Fit com.google.android.gms:play-services-fitness:9.4.0 Google Location and Activity Recognition com.google.android.gms:play-services-location:9.4.0 Google Maps com.google.android.gms:play-services-maps:9.4.0 Google Mobile Ads com.google.android.gms:play-services-ads:9.4.0 Google Places com.google.android.gms:play-services-places:9.4.0 Mobile Vision com.google.android.gms:play-services-vision:9.4.0 Google Nearby com.google.android.gms:play-services-nearby:9.4.0 Google Panorama Viewer com.google.android.gms:play-services-panorama:9.4.0 Google Play Game services com.google.android.gms:play-services-games:9.4.0 SafetyNet com.google.android.gms:play-services-safetynet:9.4.0 Android Pay com.google.android.gms:play-services-wallet:9.4.0 Android Wear com.google.android.gms:play-services-wearable:9.4.0 

More information and a complete list of all modules can be found here: Setting up Google Play services

+3
source

Nice Rolf !!

here is my settings file for gcm

 -injars google-play-services.jar -outjars google-play-services-gcm.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 -libraryjars [add your path to sdk]/sdk/extras/android/support/v7/mediarouter/libs/android-support-v7-mediarouter.jar -dontoptimize -dontobfuscate -dontwarn com.google.**.R -dontwarn com.google.**.R$* -dontnote -keep public class com.google.android.gms.gcm.**, com.google.android.gms.common.**, com.google.android.gms.iid.** { public protected *; } -keep class com.google.android.gms.common.internal.safeparcel.SafeParcelable { java.lang.String NULL; } 
+2
source

Source: https://habr.com/ru/post/978095/


All Articles