处理Activity#onResume()遇到java.lang.IllegalArgumentException诡异异常

Posted 新根

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了处理Activity#onResume()遇到java.lang.IllegalArgumentException诡异异常相关的知识,希望对你有一定的参考价值。

记录处理android 较为诡异的问题,Activity#onResume()发生IllegalArgumentException 异常。

1.Activity#onResume()异常

最近在处理Bugly上的量级较大的问题,发现有一个较为奇怪的问题,发生在ActivityThread#performResumeActivity(),即Activity#onResume()方法报错。

先来看下,设备信息

设备信息ROMCPU架构android版本
MP1603Meitu/M8arm64-v8aAndroid 7.1.1,level 25

Bugly上报的异常信息

Caused by:
java.lang.IllegalArgumentException:
android.os.Parcel.readException(Parcel.java:1688)
android.os.Parcel.readException(Parcel.java:1637)
android.app.ActivityManagerProxy.isTopOfTask(ActivityManagerNative.java:5505)
android.app.Activity.isTopOfTask(Activity.java:6008)
android.app.Activity.onResume(Activity.java:1266)
org.appplay.lib.GameBaseActivity.onResume(GameBaseActivity.java)
org.appplay.lib.AppPlayBaseActivity.onResume(AppPlayBaseActivity.java)
com.xxxtech.xxx.MiniWorldActivity.onResume(xxxWorldActivity.java)
android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
android.app.Activity.performResume(Activity.java:6813)
android.app.ActivityThread.performResumeActivity(ActivityThread.java:3407)
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3470)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:154)
android.app.ActivityThread.main(ActivityThread.java:6121)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)

从异常信息上看不到有啥有效信息,因此,考虑从源码入手看下这个流程走向。

2.源码分析

因设备是在android 7.1 中发生异常,因此查看android 7.1的源码进行分析。

先从Activity#onresume()的源码,开始查看:

  protected void onResume() 
        if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
        getApplication().dispatchActivityResumed(this);
        mActivityTransitionState.onResume(this, isTopOfTask());
        mCalled = true;
   

接下来,继续看Activity# isTopOfTask()中的源码:

    private boolean isTopOfTask() 
        //...........
        try 
            return ActivityManagerNative.getDefault().isTopOfTask(getActivityToken());
         catch (RemoteException e) 
            return false;
        
    

接下来,查看下ActivityManagerNative#isTopOfTask()中的源码:

    public boolean isTopOfTask(IBinder token) throws RemoteException 
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IActivityManager.descriptor);
        data.writeStrongBinder(token);
        mRemote.transact(IS_TOP_OF_TASK_TRANSACTION, data, reply, 0);
        reply.readException();
        boolean res = reply.readInt() == 1;
        data.recycle();
        reply.recycle();
        return res;
    

接下来,会从app进程走到system进程中,查看下ActivityManagerService#isTopOfTask()源码中:

    @Override
    public boolean isTopOfTask(IBinder token) 
        synchronized (this) 
            ActivityRecord r = ActivityRecord.isInStackLocked(token);
            if (r == null) 
                throw new IllegalArgumentException();
            
            return r.task.getTopActivity() == r;
        
    

从上面源码可知,当ativity的IBinder对象在ams 中没有记录时,就会抛出IllegalArgumentException异常。

猜测:Activity发生了某种异常状态,导致ams 将Acitivity从Record记录中移除了。在某个时机下,app 又执行了Activity 恢复onResume状态,因此导致了IllegalArgumentException异常。

3.可行性不高的方案

通过谷歌搜索,其他小伙伴也遇到了类似的问题。

3.1 通过捕捉异常,反射修改 mCalled,继续执行逻辑

先来看下谷歌上讨论该问题,其中有人提出一种方案:可能存在不可测的风险

public void onResume() 
                try 
                        super.onResume();
                 catch(IllegalStateException e) 
                        try 
                                Field f = Activity.class.getDeclaredField("mCalled");
                                f.setAccessible(true);
                                f.set(this, true);
                         catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException e1) 
                        
                
              ....
         

更多详情,请阅读谷歌Activity#onResume异常讨论

3.2 通过Hook , 捕捉异常,继续执行逻辑

也有网友提出,从hook ActivityManagerNative 入手,捕捉ams 抛出的异常:可能存在不可测的风险

private class IActivityManagerProxy(private val instance: Any): InvocationHandler 
    override fun invoke(proxy: Any?, method: Method, args: Array<out Any>?): Any? 
        Logger.i(TAG, "invoke: $method.name")
        if (method.name == "isTopOfTask") 
            return try 
                method.invoke(instance, *(args ?: emptyArray())) as Boolean
             catch (e: Exception) 
                // 这里就可以将我们最初要解决的IllegalArgumentException给try-catch住,
                // 这样就达到了我们的目的了。
                Logger.w(TAG, "isTopOfTask invoke exception: $e")
                false
            
        

        return method.invoke(instance, *(args ?: emptyArray()))
    

更多详情,请阅读Hook 处理 Activity#onResume ()

3.3 捕捉异常,根据异常具体分析

这个方案是借鉴了ActivityThread#performResumeActivity():

  public final ActivityClientRecord performResumeActivity(IBinder token,
            boolean clearHide, String reason) 
            //........
            try 
                r.activity.performResume();
             catch (Exception e) 
                if (!mInstrumentation.onException(r.activity, e)) 
                    throw new RuntimeException(
                        "Unable to resume activity "
                        + r.intent.getComponent().toShortString()
                        + ": " + e.toString(), e);
                
            
        return r;
    

根据官方的思路,提出一种可行方案:为Acitivity注册 Instrumentation ,通过onException来获取到异常信息和调用栈。若是有Thread#setUncaughtExceptionHandler 也可以上报该异常详细情况。阅读更多详细信息

归根结底,来时需要搞清楚在 activity stop timeout 这段时间,借助log 分析 activity发生了哪些变化

4.借助Log 信息分析 解决

因项目中集成了Bugly ,bugly会捕捉当时的log信息,省略获取重现的log 过程。

在Bugly官网中发现一段有用的log 信息:

6207-27 10:02:39.243 2935 3420 E CrashReport-Native: not open trace switch JAR_JNI_VERSION is 1 or dump type is sigquit
6307-27 10:02:40.099 2935 3224 E CrashReport: main thread blocked,now begin to upload anr stack
6407-27 10:02:40.251 2935 3224 E CrashReport: #++++++++++Record By Bugly++++++++++#
6507-27 10:02:40.251 2935 3224 E CrashReport: # You can use Bugly(http:\\\\bugly.qq.com) to get more Crash Detail!
6607-27 10:02:40.251 2935 3224 E CrashReport: # PKG NAME: com.tencent.tmgp.xxxtech.xxxworld
6707-27 10:02:40.251 2935 3224 E CrashReport: # APP VER: 1.15.0
6807-27 10:02:40.251 2935 3224 E CrashReport: # SDK VER: 3.4.4-3.9.2
6907-27 10:02:40.252 2935 3224 E CrashReport: # LAUNCH TIME: 2022-07-27 10:02:29
7007-27 10:02:40.252 2935 3224 E CrashReport: # CRASH TYPE: ANR
7107-27 10:02:40.252 2935 3224 E CrashReport: # CRASH TIME: 2022-07-27 10:02:40
7207-27 10:02:40.252 2935 3224 E CrashReport: # CRASH PROCESS: null
7307-27 10:02:40.253 2935 3224 E CrashReport: # CRASH THREAD: main
7407-27 10:02:40.253 2935 3224 E CrashReport: # REPORT ID: 245c625f-943b-4c04-9bb1-d5943aa4cae1
7507-27 10:02:40.253 2935 3224 E CrashReport: # CRASH DEVICE: Z1026G ROOTED
7607-27 10:02:40.253 2935 3224 E CrashReport: # RUNTIME AVAIL RAM:1076092928 ROM:3734093824 SD:3734093824
7707-27 10:02:40.253 2935 3224 E CrashReport: # RUNTIME TOTAL RAM:2070941696 ROM:27512537088 SD:27512537088
7807-27 10:02:40.253 2935 3224 E CrashReport: # EXCEPTION ANR MESSAGE:
7907-27 10:02:40.253 2935 3224 E CrashReport: null
8007-27 10:02:40.253 2935 3224 E CrashReport: # CRASH STACK:
8107-27 10:02:40.253 2935 3224 E CrashReport: java.io.UnixFileSystem.getBooleanAttributes0(Native Method)
8207-27 10:02:40.253 2935 3224 E CrashReport: java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:242)
8307-27 10:02:40.253 2935 3224 E CrashReport: java.io.File.isDirectory(File.java:804)
8407-27 10:02:40.253 2935 3224 E CrashReport: org.appplay.lib.FileUtils.getSize(FileUtils.java)
8507-27 10:02:40.253 2935 3224 E CrashReport: org.appplay.lib.FileUtils.getSize(FileUtils.java)
8607-27 10:02:40.253 2935 3224 E CrashReport: org.appplay.lib.FileUtils.getSize(FileUtils.java)
8707-27 10:02:40.253 2935 3224 E CrashReport: org.appplay.lib.FileUtils.getSize(FileUtils.java)
8807-27 10:02:40.253 2935 3224 E CrashReport: org.appplay.lib.FileUtils.getSize(FileUtils.java)
8907-27 10:02:40.253 2935 3224 E CrashReport: org.appplay.lib.GameBaseActivity.DoSetPackageName(GameBaseActivity.java)
9007-27 10:02:40.253 2935 3224 E CrashReport: org.appplay.lib.AppPlayBaseActivity.Show_GLView(AppPlayBaseActivity.java)
9107-27 10:02:40.253 2935 3224 E CrashReport: com.minitech.miniworld.MiniWorldActivity.onActivityResult(MiniWorldActivity.java)
9207-27 10:02:40.253 2935 3224 E CrashReport: android.app.Activity.dispatchActivityResult(Activity.java:6962)
9307-27 10:02:40.253 2935 3224 E CrashReport: android.app.ActivityThread.deliverResults(ActivityThread.java:4086)
9407-27 10:02:40.253 2935 3224 E CrashReport: android.app.ActivityThread.handleSendResult(ActivityThread.java:4133)
9507-27 10:02:40.253 2935 3224 E CrashReport: android.app.ActivityThread.-wrap20(ActivityThread.java)
9607-27 10:02:40.253 2935 3224 E CrashReport: android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534)
9707-27 10:02:40.253 2935 3224 E CrashReport: android.os.Handler.dispatchMessage(Handler.java:102)
9807-27 10:02:40.253 2935 3224 E CrashReport: android.os.Looper.loop(Looper.ja

以上是关于处理Activity#onResume()遇到java.lang.IllegalArgumentException诡异异常的主要内容,如果未能解决你的问题,请参考以下文章

在 Activity 中重新创建后未调用 onResume

Application + Activity + onResume == NULL POINTER EXCEPTION

android Activity的onPause()与onResume()

Android Activity 生命周期中onStart()和onResume()的区别

Activity在onCreate和onResume中finish时,生命周期的不同表现

Android读书笔记一:假设当前Activity为A,如果这时用户打开一个新的Activity B,那么B的onResume和A的onPause哪个先执行呢?