使用Proguard剥离未使用的Support lib类 [英] Use Proguard for stripping unused Support lib classes

查看:81
本文介绍了使用Proguard剥离未使用的Support lib类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Android支持库添加到我的项目中,并且我注意到生成的APK文件的大小膨胀了很多.

I'm adding the Android Support library to my project and I've noticed the resulting APK file inflates in size quite a bit.

我要执行的操作是使用Proguard删除我不使用的库中的类,例如兼容backword的Notification builder类(实际上,我什至没有使用Support lib中的任何东西就对此进行了测试,我只是将其放在了/libs文件夹中).

What I'm trying to do is use Proguard to remove classes in the library that I don't use, e.g. the backword compatible Notification builder class (in fact, I tested this without even using anything from the Support lib, I just dropped it in my /libs folder).

我的proguard.cfg恰好是<sdktools>\tools\proguard\proguard-android.txt中的内容,加上了我自己添加的-dontobfuscate(因为我确实不需要混淆),但是,我没有看到我的.APK文件变小.我的proguard.cfg如下:

My proguard.cfg is exactly what's in <sdktools>\tools\proguard\proguard-android.txt, with my own addition of -dontobfuscate (as I don't really need obfuscation), however, I'm not seeing my .APK file become any smaller. My proguard.cfg is below:


# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontobfuscate
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native ;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
    public static ;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

推荐答案

Android SDK仅在发布版本中应用ProGuard,在调试版本中不应用.

The Android SDK only applies ProGuard in release builds, not in debug builds.

此外,Android SDK(r20或更高版本)通常在项目中查找proguard-project.txt而不是proguard.txt.该文件通常可以为空,因为构建过程还会读取全局文件proguard-android.txt.您可能需要使用

Furthermore, the Android SDK (r20 or higher) typically looks for proguard-project.txt instead of proguard.txt in your project. This file can generally be empty, because the build process also reads the global file proguard-android.txt. You may want to update your project with

android update project -p MyProjectDirectory

最后,默认情况下,Android SDK(r20或更高版本)会禁用ProGuard的优化步骤(可以在缩小步骤上进行改进).您可以通过指向proguard-android-optimize.txt而不是project.properties中的proguard-android.txt来启用它.

Finally, the Android SDK (r20 or higher) disables ProGuard's optimization step by default (which can improve on its shrinking step). You can enable it by pointing to proguard-android-optimize.txt instead of proguard-android.txt in your project.properties.

这篇关于使用Proguard剥离未使用的Support lib类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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