片段计数与ProGuard无法正常工作

时间:2017-03-18 12:05:38

标签: android android-fragments proguard android-proguard fragment-backstack

我的AppCompatActivity上有一个函数,它确定片段列表中的片段数量 - 下面是代码:

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {

            List<Fragment> fragmentList = getSupportFragmentManager().getFragments();

            if (fragmentList != null) {
                for (Fragment fragment : fragmentList) {
                    if (fragment != null) {
                        if (fragment.getClass().getSimpleName().equals("SelfCBaseFragment")) {

                            if (fragment.getChildFragmentManager().getBackStackEntryCount() > 1) {
                                fragment.getChildFragmentManager().popBackStack();

                            } else {
                                getSupportFragmentManager().popBackStack();
                                hideActionBar();
                            }
                        } else if (fragment.getClass().getSimpleName().equals("ABCFragment")) {

                            getSupportFragmentManager().popBackStackImmediate();
                        } else {
                            MyLog.d("FragmentName:", fragment.getClass().getSimpleName());
                            getSupportFragmentManager().popBackStack();
                        }
                    }

                }
            }
        } else {
            hideActionBar();
        }

代码在调试时工作正常,即ProGuard关闭。但是在导出签名的APK时,它没有按预期工作,即它没有点击hideActionBar。当我回到初始屏幕时,我可以看到它是hideActionBar()未调用的。我需要再次单击操作栏中的然后它隐藏操作栏。可能导致这种情况的任何提示?

我的ProGuard文件中是否需要添加哪些内容?

附上ProGuard文件:

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}


## Square Picasso specific rules ##
## https://square.github.io/picasso/ ##

-dontwarn com.squareup.okhttp.**
#json
# gson
-keepattributes *Annotation*
-keepattributes Signature
-keepattributes Exceptions

# Gson specific classes
-keep class sun.misc.Unsafe { *; }

-keep class com.flurry.** { *; }
-dontwarn com.flurry.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**
-dontwarn android.support.**

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}

2 个答案:

答案 0 :(得分:2)

这是使用Proguard和类名称的预期行为。 你在做:

fragment.getClass().getSimpleName().equals("MyFragmentClassName")

但使用MyFragmentClassName更改Proguard,因此如果要解析原始类名,则必须保留它。

最优化的版本是仅保留需要它的类的名称。 在这种特定情况下,您必须添加到Proguard文件中:

-keepnames package.of.this.fragment.SelfCBaseFragment
-keepnames package.of.this.other.fragment.ABCFragment

答案 1 :(得分:1)

解决此问题的更好方法可能是用类的混淆名称替换硬编码字符串。例如:

fragment.getClass().getName().equals(SelfCBaseFragment.class.getName())

通过这种方式,您仍然可以对SelfCBaseFragment类的名称进行模糊处理,因为fragment.getClass().getName()将返回与SelfCBaseFragment.class.getName()相同的内容。