支持库和 RecyclerView onBindViewHolder 和 onCreateViewHolder 的 Proguard 问题

Proguard issue with Support Library and RecyclerView onBindViewHolder and onCreateViewHolder

Fatal Exception: java.lang.NullPointerException at my.packa.CustomAdapter.onCreateViewHolder(Unknown Source) at my.package.name.CustomAdapter.onBindViewHolder(Unknown Source) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(Unknown Source) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(Unknown Source) at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(Unknown Source) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(Unknown Source) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(Unknown Source) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(Unknown Source) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(Unknown Source) at android.support.v7.widget.LinearLayoutManager.layoutChunk(Unknown Source) at android.support.v7.widget.LinearLayoutManager.fill(Unknown Source) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(Unknown Source) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(Unknown Source) at android.support.v7.widget.RecyclerView.dispatchLayout(Unknown Source) at android.support.v7.widget.RecyclerView.onLayout(Unknown Source) at android.view.View.layout(View.java:14948) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(Unknown Source) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(Unknown Source) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(Unknown Source) at android.support.design.widget.CoordinatorLayout.onLayout(Unknown Source) at android.view.View.layout(View.java:14948) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) at android.view.View.layout(View.java:14948) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.v4.widget.SwipeRefreshLayout.onLayout(Unknown Source) at android.view.View.layout(View.java:14948) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.v4.widget.DrawerLayout.onLayout(Unknown Source) at

ProGuard 规则如下所示:

-dontwarn com.bumptech.glide.load.resource.bitmap.VideoDecoder
-keep class android.support.v7.widget.** { *; }
-keep class android.support.v7.widget.SearchView { *; }
-keep class java.lang.** { *; }
-keep class android.support.** { *; }
-dontwarn android.support.**
-keep class android.support.v7.widget.** { *; }
-keep class android.support.design.widget.** { *; }
-keep class my.package.name.CustomAdapter


# Hide warnings about references to newer platforms in the library
-dontwarn android.support.v7.**
# don't process support library
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }


-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

当 proguard 为 运行

时,该问题仅在发布 apk 时发生

将以下规则添加到您的混淆器文件中:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keepattributes *Annotation*
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keepattributes Exceptions,InnerClasses,Signature

#-keep class au.com.flightcentre.fragment.** { *; }

# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
-keep class * implements android.os.Serializable {
  public static final android.os.Serializable$Creator *;
}
-keepclassmembers class **.R$* {
    public static <fields>;
}

我认为没有必要保留您的自定义适配器,但您必须保留模型 类(您的 java 对象)。像这样:

-keep class com.nnacres.app.model.** { *; }

让我知道进展如何。

我认为以下规则应该适用于您的情况。

-dontwarn com.bumptech.glide.load.resource.bitmap.VideoDecoder
-keep class com.google.** { *; }
-keep class com.android.** { *; }
-keep class android.support.v7.** { *; }
-keep class java.lang.** { *; }

-dontwarn android.support.v7.**

-keepattributes Signature
-keepattributes *Annotation*

只保留 com.googlecom.androidandroid.support.v7 下的所有 class 名称。也删除 keep 接口。

我认为您不需要在 proguard-rules.pro 中添加以下内容。所以你可以考虑省略它。

-keep class my.package.name.CustomAdapter

这是我项目中的工作 progurad-rules.pro 文件。

-adaptclassstrings
-useuniqueclassmembernames
-allowaccessmodification
-keep class com.google.** { *; }
-keep class com.github.** { *; }
-keep class org.apache.** { *; }
-keep class com.android.** { *; }
-keep class junit.** { *; }
-keep class org.mockito.** { *; }
-keep class android.support.v7.widget.SearchView { *; }

-keepattributes Signature
-keepattributes *Annotation*

-dontwarn com.google.**
-dontwarn org.apache.**
-dontwarn android.support.**
-dontwarn org.junit.**
-dontwarn org.mockito.**

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** w(...);
    public static *** i(...);
    public static *** e(...);
}

-printmapping build/outputs/mapping/release/mapping.txt