Proguard和RecyclerView项目装饰

时间:2016-01-12 16:47:16

标签: android android-recyclerview proguard android-proguard

ItemDecoration元素的RecyclerView,我遇到了一些意料之外的行为。

在某些手机(三星Android 5设备)上,当使用构建应用proguard时,我的ItemDecoration未显示(minify true)。如果没有proguard / minify它工作正常,你可以看到回收器元素之间的ItemDecoration。

在大多数手机上,问题不存在,您可以看到有或没有应用proguard的项目装饰。

不确定那里会发生什么,但感谢任何输入。

项装饰器代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDivider;

    public DividerItemDecoration(Context context) {
        mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
    }

    @Override
    public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();
            int margin = 30;
            mDivider.setBounds(left + margin, top, right - margin, bottom);
            mDivider.draw(canvas);
        }
    }
}

line_divider.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="1dp"
        android:height="1dp" />
    <solid android:color="#ffc6c6c6" />
</shape>

Proguard规则:

-keepclassmembers class fqcn.of.javascript.interface.for.webview {
   public *;
}

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


#retrofit
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keep class com.myappspackage.catalog.** { *; }

#OKHttp
-dontwarn okio.**
-keep class okio.** { *; }

-dontwarn org.slf4j.**
-keep class org.slf4j.** { *; }

-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

-keepattributes Signature
-keepattributes Exceptions

#Google analytics
 -keep class com.google.android.gms.** { *; }
 -keep public class com.google.android.gms.**
 -dontwarn com.google.android.gms.**

Gradle构建日志:

Executing tasks: [clean, :app:generateReleaseSources]

Configuration on demand is an incubating feature.
:app:clean
:app:preBuild UP-TO-DATE
:app:preReleaseBuild UP-TO-DATE
:app:checkReleaseManifest
:app:preDebugBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72311Library
:app:prepareComAndroidSupportCardviewV72311Library
:app:prepareComAndroidSupportDesign2311Library
:app:prepareComAndroidSupportRecyclerviewV72311Library
:app:prepareComAndroidSupportSupportV42311Library
:app:prepareComCrashlyticsSdkAndroidAnswers131Library
:app:prepareComCrashlyticsSdkAndroidBeta113Library
:app:prepareComCrashlyticsSdkAndroidCrashlytics251Library
:app:prepareComCrashlyticsSdkAndroidCrashlyticsCore234Library
:app:prepareComGoogleAndroidGmsPlayServicesAnalytics840Library
:app:prepareComGoogleAndroidGmsPlayServicesBase840Library
:app:prepareComGoogleAndroidGmsPlayServicesBasement840Library
:app:prepareComGoogleAndroidGmsPlayServicesGcm840Library
:app:prepareComGoogleAndroidGmsPlayServicesMeasurement840Library
:app:prepareDeHdodenhofCircleimageview130Library
:app:prepareIoFabricSdkAndroidFabric135Library
:app:prepareReleaseDependencies
:app:compileReleaseAidl
:app:compileReleaseRenderscript
:app:generateReleaseBuildConfig
:app:generateReleaseAssets UP-TO-DATE
:app:mergeReleaseAssets
:app:processReleaseManifest
:app:fabricGenerateResourcesRelease
:app:generateReleaseResValues UP-TO-DATE
:app:generateReleaseResources
:app:mergeReleaseResources
AAPT: /Users/aviran/Projects/app/src/main/res/drawable-xhdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited
AAPT: /Users/aviran/Projects/app/src/main/res/drawable-hdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited
AAPT: /Users/aviran/Projects/app/src/main/res/drawable-mdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited
:app:processReleaseResources
:app:generateReleaseSources

BUILD SUCCESSFUL

Total time: 30.549 secs

超级奇怪的解决方案

Log.i(TAG, "decorating: " + i);循环中添加for,实际上会为每个项目绘制线条。将它放在for循环之外是行不通的。知道那里到底发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

这不能直接回答你的问题,但可能有效。你可以做的是在你的proguard文件中创建一个条目

-keep class yourpakganame.itemdecorator
-keep public class * extends android.support.v7.widget.RecyclerView.ItemDecoration
-keep class android.support.v7.widget.RecyclerView

如果以上不起作用,请尝试

-keepattributes *Annotation*, InnerClasses, Signature

这样你的整个代码都被混淆了,遗漏了项目装饰器,这不是大代码泄漏:P