首页 文章

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

提问于
浏览
4

我正在将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 回答

  • 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来启用它 .

相关问题