使用 Proguard 混淆后的 SuperNotCalledException
Posted
技术标签:
【中文标题】使用 Proguard 混淆后的 SuperNotCalledException【英文标题】:SuperNotCalledException after obfuscation with Proguard 【发布时间】:2017-06-12 03:58:23 【问题描述】:我有一个 android 应用,它的调试版本运行良好。然而,它的发布版本,包括对 ProGuard 的混淆(配置如下所示),不起作用,并且总是在启动时崩溃并显示以下日志:
01-26 15:33:34.048 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alxdroiddev.cameragear, PID: 6551
co: Fragment FragmentIntro13af572c 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)
我已经尝试添加所需的调用(覆盖错误片段中的onAttach()
),但这并没有任何区别。有问题的片段在下面的代码中,它与库AppIntro 一起使用。这是FragmentIntro.java
的代码:
我正在使用的 proguard 配置文件是这个(并且只有这个):
################################################# ################## 常规选项 -优化通过 5 -dontusemixedcase类名 -dontskipnonpubliclibraryclasses -dontskipnonpubliclibrary 类成员 -详细 -dontpreverify -重新包装类'' -允许访问修改 -mergeinterfacesaggressively - 积极超载 -keepattributes *Annotation*,Signature,EnclosureMethod,InnerClasses -assumenosideeffects 类 com.alxdroiddev.cameragear.FragmentQueryRunner *; -假设副作用类 com.alxdroiddev.cameragear.FragmentCards *; -假设副作用类 com.alxdroiddev.cameragear.db.InitialSampleData *; # Firebase 数据库必需(模型和 POJO) -keepclassmembers 类 com.alxdroiddev.cameragear.models.** *; ################################################# ################## 删除日志 -assumenosideeffects 类 android.util.Log 公共静态 *** e(...); 公共静态 *** w(...); 公共静态 *** wtf(...); 公共静态 *** d(...); 公共静态 *** v(...); 公共静态 *** i(...); -assumenosideeffects 类 com.alxdroiddev.utils.CLog *; ################################################# ################## IMAGECROPPER -keepnames 类 com.theartofdev.edmodo.cropper.** *; ################################################# ##################APPINTRO -keep class com.github.paolorotolo.** *; -保持公共类 com.alxdroiddev.cameragear.utils.FragmentIntro -keepclassmembers 公共类 com.alxdroiddev.cameragear.utils.FragmentIntro *; -保持公共类 com.alxdroiddev.cameragear.ActivityIntro -keepclassmembers 公共类 com.alxdroiddev.cameragear.ActivityIntro *; ################################################# ################## 元数据提取器 -keep class com.drew.imaging.** *; -keep class com.drew.metadata.** *; -keep class com.drew.lang.** *; ################################################# ################## 杂项 -keep 类 * 扩展 java.util.ListResourceBundle 受保护的 java.lang.Object[][] getContents(); # Parcelable/SafeParcelable 创建者需要不被剥离 -keepnames 类 * 实现 android.os.Parcelable 公共静态最终**创建者; -keepclassmembers 类 * 实现 android.os.Parcelable 静态**创作者; # 针对预棉花糖 SDK 构建时需要。 -dontwarn android.security.NetworkSecurityPolicy -keep class android.support.customtabs.** *; -dontwarn android.support.customtabs.** -keep class com.google.android.gms.** *; -不要警告 com.google.android.gms.** ################################################# ################## 应用内计费 -保持公共接口 com.android.vending.licensing.ILicensingService -保持公共类 com.google.vending.licensing.ILicensingService -保持公共类 com.android.vending.licensing.ILicensingService ################################################# ################## 保持安卓支持 V7 和设计 -不要警告 android.support.design.** -keep class android.support.design.** *; -保持界面 android.support.design.** *; -保持公共类 android.support.design.R$** *; -保持公共类 android.support.v7.widget.** *; -保持公共类 android.support.v7.internal.widget.** *; -保持公共类 android.support.v7.internal.view.menu.** *; -保持公共类 * 扩展 android.support.v4.view.ActionProvider 公共(android.content.Context); -保持公共类 android.support.v14.preference.** *; -保持公共类 android.support.v7.app.** *; -保持公共类 android.support.v7.preference.** *; -保持公共类 android.support.v4.app.** *; -不要警告 android.support.** -保持界面 android.support.v4.** *; -保持界面 android.support.v7.** *; -keep interface android.support.v13.** *; -keep interface android.support.v14.** *; ################################################# ################## ORG.APACHE.HTTP -保持类 org.apache.http.** *; -保持接口 org.apache.http.** -不要警告 org.apache.** ################################################# ################## GOOGLE PLAY 服务库 - 广告 -保持公共类 com.google.android.gms.** public *; #-keep 类 com.google.android.gms.** # 对于 Google Play 服务 -保持公开课 com.google.android.gms.ads.** 民众 *; ################################################# ################## FIREBASE -keep public class com.google.firebase.** *; -保持公共类 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.** *; ################################################# ################## GOOGLE 建议的优化 # 原生方法见 http://proguard.sourceforge.net/manual/examples.html#native -keepclasseswithmembernames 类 * 本国的 ; # 在 Views 中保留 setter 以便动画仍然可以工作。 # 见 http://proguard.sourceforge.net/manual/examples.html#beans -keepclassmembers 公共类 * 扩展 android.view.View 无效集*(***); *** 得到*(); # 我们希望在 Activity 中保留可以在 XML 属性 onClick 中使用的方法 -keepclassmembers 类 * 扩展 android.app.Activity 公共无效*(android.view.View); # 枚举类见http://proguard.sourceforge.net/manual/examples.html#enumerations -keepclassmembers 枚举 * 公共静态 **[] 值(); 公共静态 ** valueOf(java.lang.String); -keepclassmembers 类 **.R$* 公共静态; # 理解@Keep 支持注解。 -keep 类 android.support.annotation.Keep -keep @android.support.annotation.Keep 类 * *; -keepclasseswithmembers 类 * @android.support.annotation.Keep; -keepclasseswithmembers 类 * @android.support.annotation.Keep; -keepclasseswithmembers 类 * @android.support.annotation.Keep (...);我已经忘记了我花了多少小时试图解决这个问题,特别是因为一切都在调试模式下运行,并且没有任何关于 super.onAttach()
的投诉。只有在混淆了上面的配置后才会失败。
非常感谢您的关注,感谢您的帮助。
最好的问候。
【问题讨论】:
【参考方案1】:我让它工作了。就像 Doron 建议的那样,这可能与混淆有关,而不是与代码本身有关。
我将在此处发布我的方法和最终的 proguard.cfg 文件,仅供面临相同问题的人以及如何 [可能] 解决该问题的参考(请注意:这涉及很多麻烦!)。
由于我也怀疑它与类或代码无关,所以我做的第一件事就是删除所有不必要的覆盖,让两个类都被混淆并禁用所有优化。这是通过我的 ProGuard 配置中的这些设置完成的:
-optimizations no_optimizations
解释:no_optimizations
只是一个空的占位符。这对 ProGuard 没有任何意义。你可以在那里很好地使用“黄瓜”这个词,它会产生同样的效果。这只是意味着不会运行任何优化。与不指定-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。
无论如何,感谢您的时间和关注,我希望上述解决这个烦人问题的方法对某人有所帮助。
【讨论】:
【参考方案2】:由于您在调试中工作,问题可能仅与混淆有关,与您的实际代码无关。
您是否尝试将整个班级保持在 proguard 中:
-keep class com.alxdroiddev.cameragear.utils.FragmentIntro *;
【讨论】:
【参考方案3】:您是否尝试过仅删除这些行:
@Keep
@TargetApi(23)
@Override
public void onAttach(Context context)
super.onAttach(context);
@Keep
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
【讨论】:
这些行是试图摆脱错误的失败尝试。由于日志消息抱怨他们不存在,我只是把它们放在那里(连同其他简单地调用它们的超级方法的覆盖)。我认为他们不会造成伤害。以上是关于使用 Proguard 混淆后的 SuperNotCalledException的主要内容,如果未能解决你的问题,请参考以下文章
Maven WEB 项目使用ProGuard进行混淆,最佳解决方案