缩小 Google Play 服务库,仅用于 Google Analytics [英] Shrink Google Play Services library for use with Google Analytics only

查看:73
本文介绍了缩小 Google Play 服务库,仅用于 Google Analytics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 Google Play 服务库中删除不必要的包,以便该库只包含 Google Analytics 所需的一切?

Is it possible to remove unnecessary packages from the Google Play Services library so that the library will only contain everything needed for Google Analytics?

我之所以这么问是因为 Google Play 服务库中充斥着我认为 Google Analytics 未使用的额外包、图像和资源.

I'm asking this because the Google Play Services library is bloated with extra packages, images and resources of which I think are unused by Google Analytics.

推荐答案

Proguard 默认情况下也会从 Google Play 服务库中删除 jar 文件中未使用的代码.但是 proguard 不能用于删除(未使用的)资源,但我发现还有其他选项可以进一步优化库.

Proguard removes unused code from jar files by default also from the Google Play Services library. But proguard cant be used to remove (unused) resources, but I have found that there are other options to further optimize the library.

我能够使用方法二将应用程序从 1827 KB 缩小到 1115 KB.我测试了该应用,它运行良好,Google Analytics 静态数据按预期显示在在线仪表板上.

I was able using method two to shrink an app from 1827 KB down to 1115 KB. I tested the app and it works fine and Google Analytics statics are appearing on the online dashboard as expected.

第一种方法可能是最简单的.

The first method is probably the most easy.

第 1 步:将 google-play-services.jar 文件从库项目复制到您自己的项目,并将其粘贴到 libs 文件夹中.

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

第 2 步:从您的项目中删除对原始 google-play-services_lib 项目的库引用.

Step 2: Remove the library reference to the original google-play-services_lib project from your project.

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

第 3 步:将 dontwarn 标志添加到您的 progruard 设置文件中.

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

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

说明:google-play-services-jar 包含对生成的 Java 文件 (R.java) 的引用.这些文件通常是在构建 google-play-services_lib 项目时生成的.Proguard 通常会在优化 google-play-services.jar 文件时警告您(通过错误退出)这些文件丢失.通过添加 dontwarn 标志,proguard 将忽略这些文件丢失的事实,并且不会出现错误退出.

Explanation: The google-play-services-jar contains references to generated Java files (R.java). These files are normally generated when the google-play-services_lib project is being build. Proguard will normally warn you (By exiting with an error) that these files are missing when optimizing the google-play-services.jar file. By adding the dontwarn flags proguard will just ignore the fact that these files are missing and won't exit with an error.

方法二是方法一的优化版本.不同之处在于,您首先使用 proguard 优化库,而不是将 google-play-services.jar 文件从库项目复制到您自己的项目.

Method two is an more optimized version of method 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.

所以你想要做的就是在 google-play-services.jar 文件上手动运行 proguard.为了只保留 Google Analytics,我使用了这个 proguard 设置文件.

So what you want to do is manually run proguard on just the google-play-services.jar file. For keeping just 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;
}

步骤 1: 将 google-play-services.jar 文件从库项目复制到 sdk oolsproguardlib 文件夹.

Step 1: Copy the google-play-services.jar file from the library project to the sdk oolsproguardlib folder.

第 2 步:将您的 proguard 设置文件复制到 sdk oolsproguardlib 文件夹.

Step 2: Copy your proguard settings file to the sdk oolsproguardlib folder.

第 3 步:使用您的设置文件运行 progaurd:

Step 3: Run progaurd with your settings file:

在 Windows 上:打开命令窗口 ->导航到 sdk oolsproguardlib 文件夹 ->使用以下命令运行 proguard:

On windows: Open command window -> Navigate to the sdk oolsproguardlib folder -> Run proguard using:

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

第 4 步:使用本文中介绍的第一种方法将优化后的 google-play-services-analytics.jar 添加到您的项目中.(不要忘记第一种方法中的第 3 步!)

Step 4: Use the first method explained in this post to add the optimized google-play-services-analytics.jar to your project. (Don't forget Step 3 in the first method!)

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

这篇关于缩小 Google Play 服务库,仅用于 Google Analytics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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