2024年最全Google App Crash 参考解决方案,项目实践

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

当绑定服务已经解除绑定,再次解除绑定,会出现此异常

3.解决方案:


由于没有GMS源码,我们不能从根源上处理问题,只能尝试修改Framework代码规避此问题。

解决此问题需要修改一下两个类:


frameworks/base/core/java/android/app/ContextImpl.java

frameworks/base/core/java/android/content/ContextWrapper.java



  • 1.修改1:ContextWrapper.java

ContextWrapperunbindService 方法中try-catch住代码中抛出的异常IllegalArgumentException,抓住异常,不让异常抛出。

修改方法如下:


   

public class ContextWrapper extends Context {

    ... ... 

    

    @Override

      public void unbindService(ServiceConnection conn) {

          try {

                 mBase.unbindService(conn);

              } catch (IllegalArgumentException e) {

                //com.google.android.gms.ui Service not registered Crash

                android.util.Log.e("wjwj","---ContextWrapper GMS Crash---");

                e.printStackTrace();

          }

      }

   

    ... ...

    

}



在ContextWrapper的 unbindService 方法中try-catch IllegalArgumentException

  • 2.修改2:ContextImpl.java

修改方法如下:




 class ReceiverRestrictedContext extends ContextWrapper {

      ... ...

      

      @Override

      public void unbindService(ServiceConnection conn) {

          if (conn == null) {

              throw new IllegalArgumentException("connection is null");

          }

          if (mPackageInfo != null) {

              IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(

                      getOuterContext(), conn);

              try {

                  ActivityManager.getService().unbindService(sd);

              } catch (RemoteException e) {

                  throw e.rethrowFromSystemServer();

              } catch (IllegalArgumentException e) {

                  //com.google.android.gms.ui Service not registered Crash

                                  android.util.Log.e("wjwj","---ContextImpl GMS Crash---");

                  e.printStackTrace();

              }

  

          } else {

              throw new RuntimeException("Not supported in system context");

          }

      }

      ... ... 



}     



  • 3.修改思路:

    ContextImplunbindService 方法中try-catch抓住代码中抛出的异常IllegalArgumentException

在ContextImpl的unbindService 方法中try-catch IllegalArgumentException

二、gms.ui BadTokenException Crash

================================

1.Crash Log如下:



12-31 21:01:26.711  3776  3776 E AndroidRuntime: FATAL EXCEPTION: main

12-31 21:01:26.711  3776  3776 E AndroidRuntime: Process: com.google.android.gms.ui, PID: 3776

12-31 21:01:26.711  3776  3776 E AndroidRuntime: android.view.WindowManager$BadTokenException:

                                                  Unable to add window -- token android.os.BinderProxy@f176911 is not valid; is your activity running?

12-31 21:01:26.711  3776  3776 E AndroidRuntime:    at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)

12-31 21:01:26.711  3776  3776 E AndroidRuntime:    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)

12-31 21:01:26.711  3776  3776 E AndroidRuntime:    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)

12-31 21:01:26.711  3776  3776 E AndroidRuntime:    at android.app.Dialog.show(Dialog.java:319)

12-31 21:01:26.711  3776  3776 E AndroidRuntime:    at com.google.android.location.network.ConfirmAlertChimeraActivity.a(:com.google.android.gms:164)

12-31 21:01:26.711  3776  3776 E AndroidRuntime:    at rzo.onServiceConnected(:com.google.android.gms:71)

12-31 21:01:26.711  3776  3776 E AndroidRuntime:    at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1223)





Unable to add window – token android.os.BinderProxy@f176911 is not valid; is your activity running

2.解决方案如下


主要修改类:

frameworks/base/core/java/android/view/ViewRootImpl.java

在 ViewRootImpl类中,解决恢复出厂设置后的问题,修改方案如下:


   

 public class ViewRootImpl extends AbsViewRootImpl implements ViewParent,

           View.AttachInfo.Callbacks, ThreadedRenderer.DrawCallbacks {

 

            ... ...

         

        public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {

          synchronized (this) {

               if (mView == null) {

                   mView = view;

              ... ...

 

                if (res < WindowManagerGlobal.ADD_OKAY) {

                       mAttachInfo.mRootView = null;

                       mAdded = false;

                       mFallbackEventHandler.setView(null);

                       unscheduleTraversals();

                       setAccessibilityFocus(null, null);

                       switch (res) {

                           case WindowManagerGlobal.ADD_BAD_APP_TOKEN:

                           case WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN:

                               // add by wangjie for com.google.android.gms.ui crash

                               int deviceProvisioned = android.provider.Settings.Global.getInt(mContext.getContentResolver(), android.provider.Settings.Global.DEVICE_PROVISIONED,0);

                               if(deviceProvisioned==0){

                                  android.util.Log.e("wjwj","---ViewRootImpl GMS Crash---");

                                  return;

                               }else{

                                   throw new WindowManager.BadTokenException(

                                        "Unable to add window -- token " + attrs.token

                                        + " is not valid; is your activity running?");

                               }

                               // add by wangjie for com.google.android.gms.ui crash

                           case WindowManagerGlobal.ADD_NOT_APP_TOKEN:

                               throw new WindowManager.BadTokenException(

                                       "Unable to add window -- token " + attrs.token

                                       + " is not for an application");

                      ... ...

               }

           }

       }

                ... ...

}



修改差异点如下

Unable to add window – token android.os.BinderProxy@f176911 is not valid; is your activity running?

三、setupwizard ConcurrentModificationException Crash

===================================================

1.Crash Log如下:



--------- beginning of crash

01-01 12:00:00.918  1583  1583 E AndroidRuntime: FATAL EXCEPTION: main

01-01 12:00:00.918  1583  1583 E AndroidRuntime: Process: com.google.android.setupwizard, PID: 1583

01-01 12:00:00.918  1583  1583 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.TIME_SET flg=0x25200010 } in com.google.android.setupwizard.time.DateTimeMonitor$1@7465601

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52497(LoadedApk.java:1323)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.$m$7(Unknown Source:4)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.run(Unknown Source:39)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:790)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:99)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:164)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:6523)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)

01-01 12:00:00.918  1583  1583 E AndroidRuntime: Caused by: java.util.ConcurrentModificationException

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at java.util.ArrayList$Itr.next(ArrayList.java:860)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at com.google.android.setupwizard.time.DateTimeMonitor.updateStatus(DateTimeMonitor.java:134)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at com.google.android.setupwizard.time.DateTimeMonitor.-wrap0(Unknown Source:0)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at com.google.android.setupwizard.time.DateTimeMonitor$1.onReceive(DateTimeMonitor.java:73)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52497(LoadedApk.java:1313)

01-01 12:00:00.918  1583  1583 E AndroidRuntime:    ... 9 more

--------- beginning of system



ConcurrentModificationException

2.解决方案如下:


此问题关键在于解决ConcurrentModificationException,但是没有GMS源码,只能尝试规避方案。

修改类如下:

/frameworks/base/core/java/android/app/LoadedApk.java

修改点如下:


 public final class LoadedApk {

     ... ... 



        final class Args extends BroadcastReceiver.PendingResult {





              public final Runnable getRunnable() {



               ... ...

  

                      Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveReg");

                      try {

                          ClassLoader cl = mReceiver.getClass().getClassLoader();

                          intent.setExtrasClassLoader(cl);

                          intent.prepareToEnterProcess();

                          setExtrasClassLoader(cl);

                          receiver.setPendingResult(this);

                          receiver.onReceive(mContext, intent);

                      } catch (Exception e) {

                          if (mRegistered && ordered) {

                              if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,

                                      "Finishing failed broadcast to " + mReceiver);

                              sendFinished(mgr);

                          }

                          if (mInstrumentation == null ||

                                  !mInstrumentation.onException(mReceiver, e)) {

                              Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

                              //setupwizard TIME_SET RuntimeException Crash

                              if("android.intent.action.TIME_SET".equals(intent.getAction())){

                                  if(mReceiver.toString().contains("com.google.android.setupwizard")){

                                      android.util.Log.e("wjwj","---LoadedApk GMS  setupwizard Crash---");

                                      return;

                                  }

                              }

                              //setupwizard TIME_SET RuntimeException Crash

                              throw new RuntimeException(

                                      "Error receiving broadcast " + intent

                                              + " in " + mReceiver, e);

                          }

                      }

                             ... ... 

                  };

              }

          }  

     

     ... ...



}



修改差异点如下:

throw new RuntimeException 异常没有被try-catch 导致crash

四、setupwizard ActivityNotFoundException Crash

=============================================

1.Crash Log如下:



--------- beginning of crash

12-12 15:19:11.984  1471  1471 E AndroidRuntime: FATAL EXCEPTION: main

12-12 15:19:11.984  1471  1471 E AndroidRuntime: Process: com.google.android.setupwizard, PID: 1471

12-12 15:19:11.984  1471  1471 E AndroidRuntime: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.wizard.NEXT (has extras) }

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1843)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1557)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at android.app.Activity.startActivityForResult(Activity.java:4228)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at android.app.Activity.startActivityForResult(Activity.java:4187)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at com.google.android.setupwizard.BaseActivity.startActivityForResult(BaseActivity.java:665)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at com.google.android.setupwizard.BaseActivity.nextAction(BaseActivity.java:651)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at com.google.android.setupwizard.BaseActivity.nextAction(BaseActivity.java:637)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at com.google.android.setupwizard.user.SuggestedActionsActivity.onItemSelected(SuggestedActionsActivity.java:226)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at com.android.setupwizardlib.items.RecyclerItemAdapter$1.onClick(RecyclerItemAdapter.java:106)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at android.view.View.performClick(View.java:5624)

12-12 15:19:11.984  1471  1471 E AndroidRuntime:    at android.view.View$PerformClick.run(View.java:22285)



com.google.android.setupwizard 报错log

2.解决方案如下:


Instrumentation类中对Google开机向导进行特殊处理。

主要修改类如下:

/frameworks/base/core/java/android/app/Instrumentation.java

主要修改方案如下:


public class Instrumentation {



      ... ... 



      /** @hide */

      public static void checkStartActivityResult(int res, Object intent) {

          if (!ActivityManager.isStartResultFatalError(res)) {

              return;

          }

  

          switch (res) {

              case ActivityManager.START_INTENT_NOT_RESOLVED:

              case ActivityManager.START_CLASS_NOT_FOUND:

                  if (intent instanceof Intent && ((Intent)intent).getComponent() != null)

                      throw new ActivityNotFoundException(

                              "Unable to find explicit activity class "

                              + ((Intent)intent).getComponent().toShortString()

                              + "; have you declared this activity in your AndroidManifest.xml?");

                                  //GMS  Setupwizard ActivityNotFoundException Crash

                  if("com.android.wizard.NEXT".equals(((Intent)intent).getAction())){

                      android.util.Log.e("wjwj","Instrumentation  GMS  Setupwizard Crash ");

                      return;

                  }

                  //GMS  Setupwizard ActivityNotFoundException Crash

                  throw new ActivityNotFoundException(

                          "No Activity found to handle " + intent);

              case ActivityManager.START_PERMISSION_DENIED:

                  throw new SecurityException("Not allowed to start activity "

                          + intent);

              case ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT:

                  throw new AndroidRuntimeException(

                          "FORWARD_RESULT_FLAG used while also requesting a result");

              case ActivityManager.START_NOT_ACTIVITY:

                  throw new IllegalArgumentException(

                          "PendingIntent is not an activity");

                 ... ...

                 }

            }

           ... ... 

}




![img](https://img-blog.csdnimg.cn/img_convert/28196b16deeab701f12e5eb267a666da.png)
![img](https://img-blog.csdnimg.cn/img_convert/64f36effe7d3794cd123ad8a54aa5399.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618658159)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

       throw new AndroidRuntimeException(

                          "FORWARD_RESULT_FLAG used while also requesting a result");

              case ActivityManager.START_NOT_ACTIVITY:

                  throw new IllegalArgumentException(

                          "PendingIntent is not an activity");

                 ... ...

                 }

            }

           ... ... 

}




[外链图片转存中...(img-5DlTsZnB-1715715302204)]
[外链图片转存中...(img-CXPgxJVP-1715715302204)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618658159)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 21
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值