2013-01-12 39 views
3

我正在將Android支持庫添加到我的項目中,並且我注意到所產生的APK文件在大小上膨脹了很多。使用Proguard剝離未使用的支持庫類

我想要做的是使用Proguard刪除我不使用的庫中的類,例如後向兼容的Notification構建器類(實際上,我甚至沒有使用Support lib中的任何東西來測試它,我只是將它放到了我的/ 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

Android SDK只在發佈版本中應用ProGuard,而不是在調試版本中。

此外,Android SDK(r20或更高版本)通常在您的項目中查找proguard-project.txt而不是proguard.txt。這個文件通常可以是空的,因爲構建過程也讀取全局文件proguard-android.txt。你可能想最後與

android update project -p MyProjectDirectory 

更新項目中,Android SDK(R20或更高版本)禁用默認的ProGuard的優化步驟(這樣可以提高其收縮步驟)。您可以在project.properties指向proguard-android-optimize.txt而不是proguard-android.txt來啓用它。

+0

謝謝!剛剛檢查,發佈版本,我的classes.dex大小明顯下降! –