【问题标题】:Fragment count not working as expected with ProGuard片段计数无法按预期使用 ProGuard
【发布时间】:2017-03-18 12:05:38
【问题描述】:

我的 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(...);
}

【问题讨论】:

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


    【解决方案1】:

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

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

    但是MyFragmentClassName改成了Proguard,所以要解析原来的类名就得保留。

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

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

    【讨论】:

      【解决方案2】:

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

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

      这样您仍然可以混淆SelfCBaseFragment 类的名称,因为fragment.getClass().getName() 将返回与SelfCBaseFragment.class.getName() 相同的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        相关资源
        最近更新 更多