I have installed Google Play Services and created a Hello World app to test that everything is working fine and I think the application size is too big: 4.98 MB. I\'m using
No, that's not normal - earlier builds of my GPSTest app that included Google Play Services for maps were only 808KB after being obfuscated using Proguard- current version (after adding another library) is around 1,497KB after obfuscation.
I would recommend exporting the APK from the command line using the following steps to avoid potential issues with Android Studio:
gradlew assembleRelease
/app/build/apk
folderIf you are exporting the APK via Android Studio, be aware that there is a known issue where Android Studio will export using the assembleDebug
task instead of the assembleRelease
task by default. As a result, any configurations in your build.gradle
file for running Proguard that are specific to the release buildType won't be executed.
As a workaround for exporting via Android Studio, you can change the default Build Variant via the following steps:
debug
to release
.Now when you do "Build->Generate Signed APK...", Android Studio should run the release Build Variant, which should run Proguard if you have it configured correctly in build.gradle
. You can change back to debug
variant while debugging your app on a normal basis.
If you want to replicate my settings from GPSTest, here's the proguard.cfg
:
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.preference.Preference
-keepclasseswithmembers class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-dontwarn **CompatHoneycomb
-dontwarn **CompatCreatorHoneycombMR2
-dontwarn **AccessibilityServiceInfoCompatJellyBeanMr2
-dontwarn android.support.v4.view.**
-dontwarn android.support.v4.media.**
-dontwarn com.actionbarsherlock.internal.**
-keep class android.support.v4.** { *; }
-keepattributes *Annotation*
-keep public class * extends android.view.View
-keep public class * extends android.view.ViewGroup
-keep public class * extends android.support.v4.app.Fragment
-keepclassmembers class * extends com.actionbarsherlock.ActionBarSherlock {
<init>(android.app.Activity, int);
}
... and build.gradle
:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
if (project.hasProperty("secure.properties")
&& new File(project.property("secure.properties")).exists()) {
Properties props = new Properties()
props.load(new FileInputStream(file(project.property("secure.properties"))))
signingConfigs {
debug {
storeFile file("gpstest.debug.keystore")
}
release {
storeFile file(props['key.store'])
keyAlias props['key.alias']
storePassword "askmelater"
keyPassword "askmelater"
}
}
} else {
signingConfigs {
debug {
storeFile file("gpstest.debug.keystore")
}
release {
// Nothing here
}
}
}
buildTypes {
release {
runProguard true
proguardFile 'proguard.cfg'
signingConfig signingConfigs.release
}
}
}
task askForPasswords << {
// Must create String because System.readPassword() returns char[]
// (and assigning that below fails silently)
def storePw = new String(System.console().readPassword("\nKeystore password: "))
def keyPw = new String(System.console().readPassword("Key password: "))
android.signingConfigs.release.storePassword = storePw
android.signingConfigs.release.keyPassword = keyPw
}
tasks.whenTaskAdded { theTask ->
if (theTask.name.equals("packageRelease")) {
theTask.dependsOn "askForPasswords"
}
}
dependencies {
compile project(':ShowcaseViewLibrary')
compile 'com.google.android.gms:play-services:3.2.65'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile 'org.jraf:android-switch-backport:1.2'
compile 'com.google.maps.android:android-maps-utils:0.2.1'
}
Entire GPSTest source code is available on Github if you want to use it as a sample.
EDIT
Another way to help shrink your APK when using features from Google Play Services v6.5 or higher is to include only the library for the feature in Google Play Services that you're actually using.
For example, if the only Google Play Services API you're using is the Maps API v2, instead of including the entire Google Play Services library in build.gradle
:
compile 'com.google.android.gms:play-services:7.8.0'
...you can just include the Maps API v2 portion:
compile 'com.google.android.gms:play-services-maps:7.8.0'
See the Google Play Services - "Selectively compiling APIs into your executable" section for details on what APIs you can split out. Here's a list as of Sept. 2015:
com.google.android.gms:play-services-plus:7.8.0
com.google.android.gms:play-services-identity:7.8.0
com.google.android.gms:play-services-base:7.8.0
com.google.android.gms:play-services-appindexing:7.8.0
com.google.android.gms:play-services-appinvite:7.8.0
com.google.android.gms:play-services-analytics:7.8.0
com.google.android.gms:play-services-cast:7.8.0
com.google.android.gms:play-services-gcm:7.8.0
com.google.android.gms:play-services-drive:7.8.0
com.google.android.gms:play-services-fitness:7.8.0
com.google.android.gms:playservices-location:7.8.0
com.google.android.gms:play-services-maps:7.8.0
com.google.android.gms:play-services-ads:7.8.0
com.google.android.gms:play-services-vision:7.8.0
com.google.android.gms:play-services-nearby:7.8.0
com.google.android.gms:play-services-panorama:7.8.0
com.google.android.gms:play-services-games:7.8.0
com.google.android.gms:play-services-safetynet:7.8.0
com.google.android.gms:play-services-wallet:7.8.0
com.google.android.gms:play-services-wearable:7.8.0
Ok, finally I've encountered the problem (thanks to @Sean Barbeau). It was the build.gradle
file of my module. In the BuildTypes
section I had release
instead of debug
and I was deploying the debug version ... I've changed that and now my app is only 1.17 MB !
buildTypes {
debug {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
EDIT
As @SeanBarbeau pointed, runProguard
is set to true
in debug mode. If I set false
then my app is again big (5 MB of size).