簡體   English   中英

使用Proguard剝離未使用的Support lib類

[英]Use Proguard for stripping unused Support lib classes

我正在將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.**

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來啟用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM