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

时间:2018-03-22 19:52:05

标签: android android-recyclerview proguard android-support-library android-proguard

  

致命异常:java.lang.NullPointerException at   my.packa.CustomAdapter.onCreateViewHolder(未知   来源)at   my.package.name.CustomAdapter.onBindViewHolder(未知   来源)at   android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(未知   来源)at   android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(未知   来源)at   android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(未知   来源)at   android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(未知   来源)at   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(未知   来源)at   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(未知   来源)at   android.support.v7.widget.LinearLayoutManager $ LayoutState.next(未知   来源)at   android.support.v7.widget.LinearLayoutManager.layoutChunk(未知   来自)android.support.v7.widget.LinearLayoutManager.fill(未知   来源)at   android.support.v7.widget.LinearLayoutManager.onLayoutChildren(未知   来源)at   android.support.v7.widget.RecyclerView.dispatchLayoutStep2(未知   来源)at   android.support.v7.widget.RecyclerView.dispatchLayout(未知来源)   在android.support.v7.widget.RecyclerView.onLayout(未知来源)at   android.view.View.layout(View.java:14948)at   android.view.ViewGroup.layout(ViewGroup.java:4631)at   android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(未知   来源)at   android.support.design.widget.ViewOffsetBehavior.onLayoutChild(未知   来源)at   android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(未知   来源)at   android.support.design.widget.CoordinatorLayout.onLayout(未知   来自)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(未知来源)   在android.view.View.layout(View.java:14948)处   android.view.ViewGroup.layout(ViewGroup.java:4631)at   

的android.support.v4.widget.DrawerLayout.onLayout(未知来源)

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中

2 个答案:

答案 0 :(得分:2)

将以下规则添加到proguard文件中:

-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.** { *; }

让我知道它是怎么回事。

答案 1 :(得分:0)

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

-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下保持所有类名完好无损。也删除保持接口。

我认为您不需要在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