谷歌播放服务,使APK太大 [英] Google Play Services makes apk too big

查看:292
本文介绍了谷歌播放服务,使APK太大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了谷歌播放服务,并创建一个Hello World应用程序来测试一切正常,我认为应用程序的大小是太大了:4.98 MB。 我使用的是Android的工作室和我紧跟在Android开发者网站的详细说明。

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 Android Studio and I've followed the instructions detailed in the android developers web.

这是我的摇篮文件:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:4.3.23'
}

修改

这是我ProGuard的文件:

This is my proguard file:

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
   public static final ** CREATOR;
}

**编辑2 **

** EDIT 2 **

我已经安装使用的IntelliJ IDEA的最后一个版本的谷歌播放服务,而现在的APK为3.52 MB。我不知道这是一个可以接受的尺寸。

I've installed Google Play Services using the last version of Intellij Idea, and now the apk is 3.52 MB. I don't know if this is an acceptable size.

这是正常的吗?

推荐答案

没有,那是不正常的 - 早期版本我的 GPSTest应用,其中包括谷歌播放服务的地图只有808KB使用Proguard-当前版本被模糊处理(添加另一个库后)约1,497KB混淆后后。

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.

我会建议使用下列步骤来避免与Android Studio的潜在问题,在命令行导出APK:

I would recommend exporting the APK from the command line using the following steps to avoid potential issues with Android Studio:

  1. 进入该项目的根在命令行
  2. 运行 gradlew assembleRelease
  3. 找到的签署,并在模糊APK /应用程序/编译/ APK 文件夹
  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

如果您通过Android的工作室导出APK,要知道,有一个的已知问题,其中Android的工作室将出口使用 assembleDebug 的任务,而不是 assembleRelease 任务默认情况下。其结果是,在你的 build.gradle 文件运行Proguard的特定于将不被执行释放buildType任何配置。

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.

作为一种变通方法通过Android的工作室导出,您可以更改默认的变化范围通过以下步骤:

As a workaround for exporting via Android Studio, you can change the default Build Variant via the following steps:

  1. 在Android的Studio中,打开查​​看 - >工具Windows的>构建变体
  2. 在打开的窗口中,将变化范围从调试发布

现在,当你做建设 - >生成签名APK ......,机器人工作室应该运行的发布版本变种,它应该运行Proguard的,如果你有它正确地配置 build.gradle 。你可以变回调试变种在调试一个正常的基础上您的应用程序。

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.

如果你想复制我的设置从GPSTest,这里的 proguard.cfg

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);
}

...和 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'
}

全GPSTest源$ C ​​$ c是可在Github上的,如果你想使用它作为一个样本

Entire GPSTest source code is available on Github if you want to use it as a sample.

修改

另一种方式来帮助使用功能时从谷歌播放服务6.5版或更高版本只包括库在谷歌的功能发挥,你实际使用服务缩水的APK。

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.

例如,如果只有谷歌播放服务,你正在使用的API,而不是包括整个谷歌播放服务库 build.gradle 地图API V2,:

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'

...你可以只包括地图API V2部分:

...you can just include the Maps API v2 portion:

compile 'com.google.android.gms:play-services-maps:7.8.0'

请参见谷歌播放服务 - 选择性编译的API到你的可执行文件部分什么的API可以拆分出的细节。下面列出了截至9月到2015年:

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:玩-服务加:7.8.0
  • 在谷歌帐户登录 com.google.android.gms:玩-服务身份:7.8.0
  • 在谷歌的行动,基本客户端库 com.google.android.gms:玩-服务基地:7.8.0
  • 在谷歌应用程序索引 com.google.android.gms:玩-服务-appindexing:7.8.0
  • 在谷歌应用程序邀请 com.google.android.gms:玩-服务-appinvite:7.8.0
  • 在谷歌Analytics(分析) com.google.android.gms:玩 - 服务 - 分析:7.8.0
  • 在谷歌演员 com.google.android.gms:玩-服务铸:7.8.0
  • 在谷歌云消息传递 com.google.android.gms:玩-服务-GCM:7.8.0
  • 在谷歌驱动器 com.google.android.gms:玩-服务驱动:7.8.0
  • 在谷歌飞度 com.google.android.gms:玩-服务,健身:7.8.0
  • 在谷歌的位置,行为识别,并将 com.google.android.gms:playservices地点:7.8.0
  • 在谷歌地图 com.google.android.gms:玩-服务-地图:7.8.0
  • 在谷歌移动广告 com.google.android.gms:玩 - 服务 - 广告:7.8.0
  • 在手机视力 com.google.android.gms:玩-服务视觉:7.8.0
  • 在谷歌最近 com.google.android.gms:玩 - 服务 - 附近:7.8.0
  • 在谷歌全景图查看器 com.google.android.gms:玩-服务全景:7.8.0
  • 在谷歌玩游戏服务 com.google.android.gms:玩-服务游戏:7.8.0
  • 安全网 com.google.android.gms:玩 - 服务 - 安全网:7.8.0
  • 在谷歌钱包 com.google.android.gms:玩-服务钱包:7.8.0
  • Android Wear com.google.android.gms:玩-服务耐磨:7.8.0
  • 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

这篇关于谷歌播放服务,使APK太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆