gpt4 book ai didi

android - 使用 Proguard 混淆后出现 SuperNotCalledException

转载 作者:行者123 更新时间:2023-12-02 17:58:00 26 4
gpt4 key购买 nike

我有一个 Android 应用程序,其调试版本运行良好。然而,它的发布版本,其中包括使用 ProGuard 进行混淆(配置如下所示),无法正常工作,并且总是在启动时崩溃,并显示以下日志:

01-26 15:33:34.048 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alxdroiddev.cameragear, PID: 6551
co: Fragment FragmentIntro{13af572c id=0x7f100177 android:switcher:2131755383:0} did not call through to super.onAttach()
at android.support.v4.app.FragmentManagerImpl.a(SourceFile:1232)
at android.support.v4.app.FragmentManagerImpl.v(SourceFile:2323)
at android.support.v4.app.FragmentManagerImpl.a(SourceFile:2136)
at android.support.v4.app.FragmentManagerImpl.b(SourceFile:2092)
at android.support.v4.app.FragmentManagerImpl.b(SourceFile:1969)
at bu.commitNowAllowingStateLoss(SourceFile:620)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(SourceFile:143)
at android.support.v4.view.ViewPager.populate(SourceFile:1268)
at android.support.v4.view.ViewPager.populate(SourceFile:1116)
at android.support.v4.view.ViewPager.onMeasure(SourceFile:1642)
at android.view.View.measure(View.java:17430)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(SourceFile:139)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我尝试添加所需的调用(覆盖错误 fragment 中的 onAttach() ),但这没有任何区别。有问题的 fragment 位于下面的代码中,它与库 AppIntro 一起使用。这是 FragmentIntro.java 的代码:

    public class FragmentIntro extends Fragment implements ISlideBackgroundColorHolder {        /*    Static stuff     */        private static final String ARG_LAYOUT_TITLE_ID = "titleId";        private static final String ARG_LAYOUT_MESSAGE_ID = "messageId";        private static final String ARG_LAYOUT_DRAWABLE_ID = "drawableId";        private static final String ARG_LAYOUT_COLOR_ID = "colorId";        //private static final int layoutResId = R.layout.intro_fragment;        /*    Private instance variables     */        private int titleRedId, messageResId, drawableId, colorResId, colorCode;        private RelativeLayout mainLayout;        public static FragmentIntro newInstance(@StringRes  int titleId, @StringRes int messageId, @DrawableRes int drawableId, @ColorRes int colorId) {            FragmentIntro fragSlide = new FragmentIntro();            Bundle args = new Bundle();            args.putInt(ARG_LAYOUT_TITLE_ID, titleId);            args.putInt(ARG_LAYOUT_MESSAGE_ID , messageId);            args.putInt(ARG_LAYOUT_DRAWABLE_ID , drawableId );            args.putInt(ARG_LAYOUT_COLOR_ID, colorId );            fragSlide.setArguments(args);            return fragSlide;        }        @Override        public void onCreate(@Nullable Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            if (getArguments() != null) {                this.titleRedId = getArguments().getInt(ARG_LAYOUT_TITLE_ID);                this.messageResId = getArguments().getInt(ARG_LAYOUT_MESSAGE_ID);                this.drawableId = getArguments().getInt(ARG_LAYOUT_DRAWABLE_ID);                this.colorResId = getArguments().getInt(ARG_LAYOUT_COLOR_ID);            }        }        @Nullable        @Override        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {            //super.onCreateView(inflater, container, savedInstanceState);            //if (container == null) return null;            View view = inflater.inflate(R.layout.intro_fragment, container, false);            Utils.globalAppContext = getContext().getApplicationContext();            colorCode = ContextCompat.getColor(getContext().getApplicationContext(), colorResId);            mainLayout = (RelativeLayout) view.findViewById(R.id.introLayout);            mainLayout.setBackgroundColor(colorCode);            ((TextView) view.findViewById(R.id.textViewIntroTitle)).setText(titleRedId);            ((TextView) view.findViewById(R.id.textViewIntroMessage)).setText(messageResId);            ((ImageView) view.findViewById(R.id.imageViewIntro)).setImageDrawable(Utils.getDrawableFromResource(drawableId));            return view;        }        @Override        public void onSaveInstanceState(Bundle outState) {            outState.putInt(ARG_LAYOUT_TITLE_ID, titleRedId);            outState.putInt(ARG_LAYOUT_MESSAGE_ID, messageResId);            outState.putInt(ARG_LAYOUT_DRAWABLE_ID, drawableId);            outState.putInt(ARG_LAYOUT_COLOR_ID, colorResId);            super.onSaveInstanceState(outState);        }        @Keep        @TargetApi(23)        @Override        public void onAttach(Context context) {            super.onAttach(context);        }        @Keep        @SuppressWarnings("deprecation")        @Override        public void onAttach(Activity activity) {            super.onAttach(activity);        }        @Override        public void onDestroy() {            super.onDestroy();        }        @Override        public void onPause() {            super.onPause();        }        @Override        public void onResume() {            super.onResume();        }        @Override        public void onStart() {            super.onStart();        }        @Override        public void onStop() {            super.onStop();        }        // Implementation of ISlideBackgroundColorHolder        @Override        public int getDefaultBackgroundColor() {            return colorCode;        }        @Override        public void setBackgroundColor(@ColorInt int backgroundColor) {            mainLayout.setBackgroundColor(backgroundColor);        }    }

我正在使用的混淆器配置文件是这个(并且只有这个):

    ####################################################################  GENERAL OPTIONS    -optimizationpasses 5    -dontusemixedcaseclassnames    -dontskipnonpubliclibraryclasses    -dontskipnonpubliclibraryclassmembers    -verbose    -dontpreverify    -repackageclasses ''    -allowaccessmodification    -mergeinterfacesaggressively    -overloadaggressively    -keepattributes *Annotation*,Signature,EnclosingMethod,InnerClasses    -assumenosideeffects class com.alxdroiddev.cameragear.FragmentQueryRunner { *; }    -assumenosideeffects class com.alxdroiddev.cameragear.FragmentCards { *; }    -assumenosideeffects class com.alxdroiddev.cameragear.db.InitialSampleData { *; }    # Required for Firebase Database (Models and POJOs)    -keepclassmembers class com.alxdroiddev.cameragear.models.** {      *;    }    ####################################################################  REMOVE LOGGING    -assumenosideeffects class android.util.Log {        public static *** e(...);        public static *** w(...);        public static *** wtf(...);        public static *** d(...);        public static *** v(...);        public static *** i(...);    }    -assumenosideeffects class com.alxdroiddev.utils.CLog { *; }    ####################################################################  IMAGECROPPER    -keepnames class com.theartofdev.edmodo.cropper.** { *; }    ####################################################################  APPINTRO    -keep class com.github.paolorotolo.** {*;}    -keep public class com.alxdroiddev.cameragear.utils.FragmentIntro    -keepclassmembers public class com.alxdroiddev.cameragear.utils.FragmentIntro {*;}    -keep public class com.alxdroiddev.cameragear.ActivityIntro    -keepclassmembers public class com.alxdroiddev.cameragear.ActivityIntro {*;}    ####################################################################  METADATA-EXTRACTOR    -keep class com.drew.imaging.** { *; }    -keep class com.drew.metadata.** { *; }    -keep class com.drew.lang.** { *; }    ####################################################################  MISC STUFF    -keep class * extends java.util.ListResourceBundle {        protected java.lang.Object[][] getContents();    }    # Needed for Parcelable/SafeParcelable Creators to not get stripped    -keepnames class * implements android.os.Parcelable {        public static final ** CREATOR;    }    -keepclassmembers class * implements android.os.Parcelable {        static ** CREATOR;    }    # Needed when building against pre-Marshmallow SDK.    -dontwarn android.security.NetworkSecurityPolicy    -keep class android.support.customtabs.** { *; }    -dontwarn android.support.customtabs.**     -keep class com.google.android.gms.** { *; }    -dontwarn com.google.android.gms.**    #################################################################### IN-APP BILLING    -keep public interface com.android.vending.licensing.ILicensingService    -keep public class com.google.vending.licensing.ILicensingService    -keep public class com.android.vending.licensing.ILicensingService    ####################################################################  KEEP ANDROID SUPPORT V7 AND DESIGN    -dontwarn android.support.design.**    -keep class android.support.design.** { *; }    -keep interface android.support.design.** { *; }    -keep public class android.support.design.R$** { *; }    -keep public class android.support.v7.widget.** { *; }    -keep public class android.support.v7.internal.widget.** { *; }    -keep public class android.support.v7.internal.view.menu.** { *; }    -keep public class * extends android.support.v4.view.ActionProvider {        public (android.content.Context);    }    -keep public class android.support.v14.preference.** { *; }    -keep public class android.support.v7.app.** { *; }    -keep public class android.support.v7.preference.** { *; }    -keep public class android.support.v4.app.** { *; }    -dontwarn android.support.**    -keep interface android.support.v4.** { *; }    -keep interface android.support.v7.** { *; }    -keep interface android.support.v13.** {*; }    -keep interface android.support.v14.** {*; }    ####################################################################  ORG.APACHE.HTTP    -keep class org.apache.http.** { *; }    -keep interface org.apache.http.**    -dontwarn org.apache.**    ####################################################################  GOOGLE PLAY SERVICES LIB - ADS    -keep public class com.google.android.gms.** { public *; }    #-keep class com.google.android.gms.**    # For Google Play Services    -keep public class com.google.android.gms.ads.**{       public *;    }    ####################################################################  FIREBASE    -keep public class com.google.firebase.** { *; }    -keep public class com.google.firebase.analytics.** { *; }    -keep public class com.google.firebase.provider.** { *; }    -keep public class com.google.firebase.auth.** { *; }    -keep interface com.google.firebase.** {*; }    -keep class com.firebase.** { *; }    #################################################################### SUGGESTED OPTIMIZATIONS BY GOOGLE    # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native    -keepclasseswithmembernames class * {        native ;    }    # keep setters in Views so that animations can still work.    # see http://proguard.sourceforge.net/manual/examples.html#beans    -keepclassmembers public class * extends android.view.View {       void set*(***);       *** get*();    }    # We want to keep methods in Activity that could be used in the XML attribute onClick    -keepclassmembers class * extends android.app.Activity {       public void *(android.view.View);    }    # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations    -keepclassmembers enum * {        public static **[] values();        public static ** valueOf(java.lang.String);    }    -keepclassmembers class **.R$* {        public static ;    }    # Understand the @Keep support annotation.    -keep class android.support.annotation.Keep    -keep @android.support.annotation.Keep class * {*;}    -keepclasseswithmembers class * {        @android.support.annotation.Keep ;    }    -keepclasseswithmembers class * {        @android.support.annotation.Keep ;    }    -keepclasseswithmembers class * {        @android.support.annotation.Keep (...);    }

我已经记不清自己花了多少时间来解决这个问题,特别是因为一切都在 Debug模式下运行,并且没有对 super.onAttach() 提出任何提示。仅在使用上面的配置进行混淆后才会失败。

非常感谢您的关注,并感谢您的帮助。

致以诚挚的问候。

最佳答案

我成功了。正如多伦所说,这可能与混淆有关,而不是与代码本身有关。

我将在这里发布我的方法和最终的 proguard.cfg 文件,只是为面临同样问题的人们以及如何[可能]解决它提供引用(注意:它涉及大量的苦力!)。

因为我也怀疑它与类或代码无关,所以我做的第一件事就是删除所有不必要的覆盖,让这两个类被混淆并禁用所有优化。这是通过我的 ProGuard 配置中的这些设置完成的:

-optimizations no_optimizations

说明:no_optimizations 只是一个空占位符。这对 ProGuard 来说毫无意义。你完全可以在其中使用“ cucumber ”这个词,它会产生相同的效果。这只是意味着不会运行任何优化。它与不指定 -optimizations 指令不同,这意味着将运行所有优化。

我还注释掉了 FragmentIntro 类和 ActivityIntro 的所有 -keep。我做了一个发布编译和测试。它跑得很优雅!上面文件(OP中)中显示的所有其他混淆选项均已打开并保持原样。

然后苦劳开始了:我创建了一条线,其中每一个 ProGuard 优化都被明确拒绝:

-optimizations !class/marking/final,!class/unboxing/enum,!class/merging/vertical,!class/merging/horizontal,!field/removal/writeonly,!field/marking/private,!field/propagation/value,!method/marking/private,!method/marking/static,!method/marking/final,!method/removal/parameter,!method/propagation/parameter,!method/propagation/returnvalue,!method/inlining/short,!method/inlining/unique,!method/inlining/tailrecursion,!code/merging,!code/simplification/variable,!code/simplification/arithmetic,!code/simplification/cast,!code/simplification/field,!code/simplification/branch,!code/simplification/string,!code/simplification/advanced,!code/removal/advanced,!code/removal/simple,!code/removal/variable,!code/removal/exception,!code/allocation/variable

然后,我开始一次允许 1 次优化,每次进行发布构建时,将其安装在模拟器上并查看应用程序是否运行。

如果该优化导致应用程序正在运行,我将其不带感叹号(!),这意味着它是允许的,否则我会将感叹号放回去并继续进行下一个优化:删除该标记,发布构建、安装、测试。

我这样做了 29 次,每次优化一次,只是为了找出哪些优化导致我的代码崩溃。

最后,我弄清楚了所有不需要的优化并得到了这个:

-optimizations class/marking/final,class/unboxing/enum,class/merging/vertical,class/merging/horizontal,!field/removal/writeonly,field/marking/private,field/propagation/value,method/marking/private,!method/marking/static,method/marking/final,!method/removal/parameter,method/propagation/parameter,method/propagation/returnvalue,method/inlining/short,method/inlining/unique,method/inlining/tailrecursion,code/merging,code/simplification/variable,code/simplification/arithmetic,code/simplification/cast,code/simplification/field,code/simplification/branch,code/simplification/string,code/simplification/advanced,!code/removal/advanced,code/removal/simple,code/removal/variable,code/removal/exception,!code/allocation/variable

上面的行经过清理并仅拒绝(感叹号)不需要的优化,相当于:

-optimizations !field/removal/writeonly,!method/marking/static,!method/removal/parameter,!code/removal/advanced,!code/allocation/variable

如果没有完成上述优化,我的代码编译并运行良好。另一方面,如果我允许上述任何优化,它将在启动时崩溃。

这就是我清理后的工作 proguard.cfg 文件的最终结果:

####################################################################  GENERAL OPTIONS

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-verbose
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-mergeinterfacesaggressively
-overloadaggressively
-keepattributes *Annotation*,Signature,EnclosingMethod,InnerClasses


-optimizations !field/removal/writeonly,!method/marking/static,!method/removal/parameter,!code/removal/advanced,!code/allocation/variable


-assumenosideeffects class com.alxdroiddev.cameragear.FragmentQueryRunner { *; }
-assumenosideeffects class com.alxdroiddev.cameragear.FragmentCards { *; }
-assumenosideeffects class com.alxdroiddev.cameragear.db.InitialSampleData { *; }

# Required for Firebase Database (Models and POJOs)
-keepclassmembers class com.alxdroiddev.cameragear.models.** {
*;
}


#################################################################### REMOVE LOGGING

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

-assumenosideeffects class com.alxdroiddev.utils.CLog { *; }


#################################################################### IMAGECROPPER

-keepnames class com.theartofdev.edmodo.cropper.** { *; }

#################################################################### APPINTRO

-keep class com.github.paolorotolo.** {*;}

#################################################################### METADATA-EXTRACTOR

-keep class com.drew.imaging.** { *; }
-keep class com.drew.metadata.** { *; }
-keep class com.drew.lang.** { *; }

#################################################################### MISC STUFF


-keep class * extends java.util.ListResourceBundle {
protected java.lang.Object[][] getContents();
}

# Needed for Parcelable/SafeParcelable Creators to not get stripped
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}

-keepclassmembers class * implements android.os.Parcelable {
static ** CREATOR;
}

# Needed when building against pre-Marshmallow SDK.
-dontwarn android.security.NetworkSecurityPolicy


-keep class android.support.customtabs.** { *; }
-dontwarn android.support.customtabs.**

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

#################################################################### IN-APP BILLING

-keep public interface com.android.vending.licensing.ILicensingService
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

#################################################################### KEEP ANDROID SUPPORT V7 AND DESIGN

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

-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }

-keep public class * extends android.support.v4.view.ActionProvider {
public <init>(android.content.Context);
}

-keep public class android.support.v14.preference.** { *; }
-keep public class android.support.v7.app.** { *; }
-keep public class android.support.v7.preference.** { *; }
-keep public class android.support.v4.app.** { *; }



-dontwarn android.support.**
-keep interface android.support.v4.** { *; }
-keep interface android.support.v7.** { *; }
-keep interface android.support.v13.** {*; }
-keep interface android.support.v14.** {*; }

#################################################################### ORG.APACHE.HTTP

-keep class org.apache.http.** { *; }
-keep interface org.apache.http.**
-dontwarn org.apache.**

#################################################################### GOOGLE PLAY SERVICES LIB - ADS

-keep public class com.google.android.gms.** { public *; }

# For Google Play Services
-keep public class com.google.android.gms.ads.**{
public *;
}

#################################################################### FIREBASE

-keep public class com.google.firebase.** { *; }
-keep public class com.google.firebase.analytics.** { *; }
-keep public class com.google.firebase.provider.** { *; }
-keep public class com.google.firebase.auth.** { *; }
-keep interface com.google.firebase.** {*; }
-keep class com.firebase.** { *; }


#################################################################### SUGGESTED OPTIMIZATIONS BY GOOGLE


# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keepclassmembers class **.R$* {
public static <fields>;
}

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
@android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
@android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
@android.support.annotation.Keep <init>(...);
}

我不知道为什么提到的标记优化会破坏我的代码,尽管我怀疑它优化了 v4.app.Fragment 相关类中的某些内容(特别是 v4.app.FragmentManager,第 1230 行,这阻止了它正确调用 onAttach() 并将 f.mCalled 设置为 true)。顺便说一句,我正在使用 Android 支持库 25.1.0。

无论如何,感谢您的时间和关注,我希望上述解决这个恼人问题的方法对某人有所帮助。

关于android - 使用 Proguard 混淆后出现 SuperNotCalledException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41877548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com