Using Proguard to remove unused classes in the Google Play Services library

I am trying to get rid of unused classes from the Google Play Services library. I created a new android project with the only empty activity. The project does not use anything from the Google Play Services library. Therefore, I expect that when I create the release (which includes running proguard in my configuration), I do not see the difference in binary size, comparing the build with the / without play-services dependency. But in fact, I see ~ 700 KB apk size difference.

I found a relatively complicated solution using a gradle script , which includes repackaging the play-services.jar file. In addition, this solution requires explicitly specifying each package that will not be used. But I do not understand why proguard does not work in my case?

build.gradle:

 apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.1" defaultConfig { minSdkVersion 10 targetSdkVersion 21 } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { // !!! when I comment the line below, release APK is 700 KB smaller !!! // compile 'com.google.android.gms:play-services:6.5.87' } 

proguard-rules.pro:

 -assumenosideeffects class android.util.Log { public static *** d(...); } 

AndroidManifest.xml:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.noplayservices"> <application android:allowBackup="true" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme"> <activity android:name=".ui.activities.MainActivity" android:icon="@drawable/ic_launcher"> <intent-filter> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest> 

MainActivity.java:

 package com.test.noplayservices.ui.activities; import android.app.Activity; import android.os.Bundle; import com.test.noplayservices.R; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setContentView(R.layout.main_activity); } } 
+6
source share
1 answer

In Google Play Services version 6.5 and higher, you can choose which APIs you want to use and import only those of them. Perhaps this will help you slightly reduce the size of the APK. Here is the list:

 Google+ com.google.android.gms:play-services-plus:6.5.+ Google Account Login com.google.android.gms:play-services-identity:6.5.+ Google Activity Recognition com.google.android.gms:play-services-location:6.5.+ Google App Indexing com.google.android.gms:play-services-appindexing:6.5.+ Google Cast com.google.android.gms:play-services-cast:6.5.+ Google Drive com.google.android.gms:play-services-drive:6.5.+ Google Fit com.google.android.gms:play-services-fitness:6.5.+ Google Maps com.google.android.gms:play-services-maps:6.5.+ Google Mobile Ads com.google.android.gms:play-services-ads:6.5.+ Google Panorama Viewer com.google.android.gms:play-services-panorama:6.5.+ Google Play Game services com.google.android.gms:play-services-games:6.5.+ Google Wallet com.google.android.gms:play-services-wallet:6.5.+ Android Wear com.google.android.gms:play-services-wearable:6.5.+ Google Actions Google Analytics Google Cloud Messaging com.google.android.gms:play-services-base:6.5.+ 

Here you can see more.

+9
source

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


All Articles