android - 使用 Proguard 剥离未使用的支持库类

标签 android proguard

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

我想做的是使用 Proguard 删除库中我不使用的类,例如backword 兼容的 Notification builder 类(事实上,我什至没有使用 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.**

最佳答案

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

此外,Android SDK(r20 或更高版本)通常会在您的项目中查找 proguard-project.txt 而不是 proguard.txt。这个文件一般可以为空,因为build过程还要读取全局文件proguard-android.txt。你可能想更新你的项目

android update project -p MyProjectDirectory

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

关于android - 使用 Proguard 剥离未使用的支持库类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14288769/

相关文章:

android - 优于 FileObserver

android - 使用 Proguard 删除日志记录

Android Studio - 没有这样的属性 : baseName for class

android - 如何从 proguard 构建中排除 R*.class 文件

android - 为什么 Proguard 在 Android 中保留 Activity 类?

android - 格式化按钮内的文本

android - 设备方向的陀螺仪问题

java - android - 动态添加按钮到自定义对话框

android - 无法在Flutter中使用Hive插件

java - 如何让 proguard 将 zip 文件视为资源?