使用Proguard剥离未使用的Support lib类

时间:2013-01-12 00:17:56

标签: android proguard

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

我要做的是使用Proguard删除我不使用的库中的类,例如backword兼容的Notification构建器类(事实上,我测试了这个,甚至没有使用支持库中的任何东西,我把它放在我的/ libs文件夹中)。

我的proguard.cfg正是<sdktools>\tools\proguard\proguard-android.txt中的内容,我自己添加-dontobfuscate(因为我真的不需要混淆),但是,我没有看到我的.APK文件变成任何更小的。我的proguard.cfg如下:

# 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.**

1 个答案:

答案 0 :(得分:1)

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

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

更新项目
android update project -p MyProjectDirectory

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