添加 Glide v4 后的 Proguard 错误

Proguard error after adding Glide v4

添加 glide v4 库后,我无法生成启用了 proguard 的 apk。 我在 proguard-rules

中添加了以下几行
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

构建时出现以下错误。

Warning:com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool: can't find referenced field 'android.graphics.Bitmap$Config HARDWARE' in library class android.graphics.Bitmap$Config

使用以下方法忽略这些警告是省事的方法:

-dontwarn com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool
-dontwarn com.bumptech.glide.load.resource.bitmap.Downsampler
-dontwarn com.bumptech.glide.load.resource.bitmap.HardwareConfigState

看到这个post

您也可以将 compileSdkVersion 增加到 26。这会处理 Glide v4 使用的新功能。

我假设您从 this glide page 中获取了混淆规则,但它写错了。替换下面的行

-keep public class * extends com.bumptech.glide.AppGlideModule

有了这个:

-keep public class * extends com.bumptech.glide.module.AppGlideModule

如您所见,module 包丢失。它也被正确记录 on README page。您还可以从 外部图书馆 .

检查您的 AppGlideModule class 路径

Proguard

如果您使用混淆器,您可能需要将以下行添加到您的proguard.cfg:

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

如果您是 targeting any API level less than Android API 27,还包括:

-dontwarn com.bumptech.glide.load.resource.bitmap.VideoDecoder

VideoDecoder 使用 API 27 APIs 这可能会导致 proguard 警告,即使较新的 APIs 不会在具有旧版本 Android 的设备上调用.