我有一个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)
我尝试添加所需的调用(在错误的片段中覆盖onAttach()
),但这没有任何区别。有问题的片段位于下面的代码中,它与库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); } }
我正在使用的proguard配置文件就是这个(只有这个):
#################################################################### 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 (...); }
我已经忘记了我花了多少时间试图解决这个问题,特别是因为所有内容都在调试模式下运行,并且没有对super.onAttach()
的抱怨。只有在使用上面的配置进行模糊处理后它才会失败。
非常感谢您的关注,感谢任何帮助。
最好的问候。
答案 0 :(得分:1)
正如您在调试中所做的那样,问题可能只与模糊处理有关,而与实际代码无关。
您是否尝试将整个课程保留在proguard中:
-keep class com.alxdroiddev.cameragear.utils.FragmentIntro { *; }
答案 1 :(得分:1)
您是否尝试过删除这些行:
@Keep
@TargetApi(23)
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Keep
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
答案 2 :(得分:1)
我得到了它的工作。就像Doron所说的那样,它可能与混淆有关,而与代码本身无关。
我将在这里发布我的方法和最终的proguard.cfg文件,只是为了面对同样问题的人们以及如何[可能]解决它的参考(注意:它涉及大量的肘部油脂! )。
因为我还怀疑它与类或代码无关,所以我做的第一件事就是删除所有不必要的覆盖,让两个类都被混淆并禁用所有优化。这是通过我的ProGuard配置中的这些设置完成的:
-optimizations no_optimizations
说明:no_optimizations
只是一个空的占位符。它对ProGuard没有任何意义。你可以很好地使用"黄瓜"在那里它会产生同样的效果。这只意味着不会运行任何选择。它与不指定-optimizations
指令不同,这意味着将运行所有优化。
我还评论了-keep
类和FragmentIntro
的所有ActivityIntro
。我做了一个发布编译和测试。它优雅地跑了!上面文件中显示的所有其他混淆选项(在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。
无论如何,谢谢你的时间和关注,我希望上面解决这个令人讨厌的问题的方法对某人有所帮助。