Google Play Services makes apk too big

后端 未结 2 640
无人及你
无人及你 2021-01-01 16:39

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

相关标签:
2条回答
  • 2021-01-01 17:23

    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:

    1. Go to root of the project at the command line
    2. Run gradlew assembleRelease
    3. Find the signed and obfuscated APK in /app/build/apk folder

    If 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:

    1. In Android Studio, open "View->Tool Windows->Build Variants"
    2. In the window that opens, change "Build Variant" from 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:

    • Google+ com.google.android.gms:play-services-plus:7.8.0
    • Google Account Login com.google.android.gms:play-services-identity:7.8.0
    • Google Actions, Base Client Library com.google.android.gms:play-services-base:7.8.0
    • Google App Indexing com.google.android.gms:play-services-appindexing:7.8.0
    • Google App Invites com.google.android.gms:play-services-appinvite:7.8.0
    • Google Analytics com.google.android.gms:play-services-analytics:7.8.0
    • Google Cast com.google.android.gms:play-services-cast:7.8.0
    • Google Cloud Messaging com.google.android.gms:play-services-gcm:7.8.0
    • Google Drive com.google.android.gms:play-services-drive:7.8.0
    • Google Fit com.google.android.gms:play-services-fitness:7.8.0
    • Google Location, Activity Recognition, and Places com.google.android.gms:playservices-location:7.8.0
    • Google Maps com.google.android.gms:play-services-maps:7.8.0
    • Google Mobile Ads com.google.android.gms:play-services-ads:7.8.0
    • Mobile Vision com.google.android.gms:play-services-vision:7.8.0
    • Google Nearby com.google.android.gms:play-services-nearby:7.8.0
    • Google Panorama Viewer com.google.android.gms:play-services-panorama:7.8.0
    • Google Play Game services com.google.android.gms:play-services-games:7.8.0
    • SafetyNet com.google.android.gms:play-services-safetynet:7.8.0
    • Google Wallet com.google.android.gms:play-services-wallet:7.8.0
    • Android Wear com.google.android.gms:play-services-wearable:7.8.0
    0 讨论(0)
  • 2021-01-01 17:33

    Ok, finally I've encountered the problem (thanks to @Sean Barbeau). It was the build.gradlefile of my module. In the BuildTypessection I had releaseinstead 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, runProguardis set to true in debug mode. If I set falsethen my app is again big (5 MB of size).

    0 讨论(0)
提交回复
热议问题