ANR in CocosActivity.java on Android 11 and 12 (Creator 3.4.1)

Hi.

After upgrading from v3.3.2 to 3.4.1 we saw a huge spike of ANR in our game.

Operating systems: only Android 11 and 12
Device states: 60% background

Log:

main (native): tid=1 systid=11328
#00 pc 0x4deb0 libc.so 
#01 pc 0x52954 libc.so 
#02 pc 0xb6070 libc.so 
#03 pc 0x17cbdec split_config.arm64_v8a.apk!libcocos.so 
#04 pc 0x82ab64 split_config.arm64_v8a.apk!libcocos.so 
#05 pc 0x2d7644 libart.so 
#06 pc 0x2cdd64 libart.so 
#07 pc 0x2f23d0 libart.so 
#08 pc 0x2f121c libart.so 
#09 pc 0x3feaf0 libart.so 
#10 pc 0x2c8514 libart.so 
#11 pc 0x2b24a2 base.vdex 
#12 pc 0x27d748 libart.so 
#13 pc 0x27c8f0 libart.so 
#14 pc 0x2d7778 libart.so 
       at com.cocos.lib.CocosActivity.onSurfaceDestroyNative(CocosActivity.java)
       at com.cocos.lib.CocosActivity.surfaceDestroyed(CocosActivity.java:240)
       at android.view.SurfaceView.notifySurfaceDestroyed(SurfaceView.java:2161)
       at android.view.SurfaceView.updateSurface(SurfaceView.java:1309)
       at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:415)
       at android.view.View.dispatchWindowVisibilityChanged(View.java:15475)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1637)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1637)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1637)
       at com.android.internal.policy.DecorView.dispatchWindowVisibilityChanged(DecorView.java:4667)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3367)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2819)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:10304)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1063)
       at android.view.Choreographer.doCallbacks(Choreographer.java:845)
       at android.view.Choreographer.doFrame(Choreographer.java:780)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1048)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8582)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)

I can ask engineering to have a look.

Thanks for the feedback, we are following up on this issue.

FYI, this is our dependencies from Gradle:

dependencies {
    implementation fileTree(dir: '../libs', include: ['*.jar','*.aar'])
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    implementation fileTree(dir: "${COCOS_ENGINE_PATH}/cocos/platform/android/java/libs", include: ['*.jar'])
    implementation project(':libservice')
    implementation project(':libcocos')
    implementation platform('com.google.firebase:firebase-bom:29.0.4')
    implementation 'com.google.firebase:firebase-config'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.facebook.android:facebook-android-sdk:latest.release'
    implementation 'com.google.android.gms:play-services-ads:20.5.0'
    implementation 'com.google.android.gms:play-services-games:22.0.1'
    implementation 'com.google.android.gms:play-services-auth:20.1.0'
    implementation 'com.google.android.play:core:+'
    implementation 'com.google.ads.mediation:facebook:6.8.0.0'
    implementation 'com.google.ads.mediation:applovin:10.3.5.0'
    implementation 'com.google.ads.mediation:unity:4.0.0.0'
    implementation 'com.unity3d.ads:unity-ads:4.0.0'
    implementation 'com.appsflyer:af-android-sdk:6.5.2'
    implementation 'androidx.work:work-runtime:2.7.1'
}

I suppose it might be issue with AdMob banner.

@stark2022 We have made some optimizations for this issue in version 3.4.2 and recommend you to upgrade to version 3.4.2 to test it.

recommend you to upgrade to version 3.4.2 to test it.
Thank you. We did and as we see in the 3.4.2 the issue appears less often under Android 12, but still exists:

v3.4.1 (1117 ANR events):

Android 12: 56%
Android 11: 44%

v3.4.2 (581 ANR events):

Android 12: 16%
Android 11: 84%

Also the issue always happens right away when you start the game.

All logs from Firebase Crashlytics looks like this:

1. 04:33:34.311 AppActivity::onCreate start
2. 04:33:34.312 AppActivity::onCreate done
3. 04:33:34.313 AppActivity::onStart
4. 04:33:34.315 AppActivity::onResume
5. 04:33:34.637 session_start
6. 04:33:34.637 screen_view (firebase_screen_class:AppActivity)
7. 04:33:44.529 AppActivity::onPause

I suppose it might be issue with AdMob banner.

I can confirm, that the issue is not related to the AdMob

1 Like

Hi, can you please provide us with a CocosCreator test project? For troubleshooting this issue.

Hi, we will try to prepare test project.

What we found:

  1. The ANR occurs only on the app start and even before we can see the splash screen.
  2. And this happens very randomly!

We catched it several times on our test device. We assume the problem is related to DeviceAgent::doInit(const DeviceInfo &info) in the source file: /resources/3d/engine-native/cocos/renderer/gfx-agent/DeviceAgent.cpp and method setMultithreaded(true);

if (multithreaded) {
        _mainMessageQueue->setImmediateMode(false);
        _actor->bindContext(false);
        _mainMessageQueue->runConsumerThread();
        ENQUEUE_MESSAGE_1(
            _mainMessageQueue, DeviceMakeCurrentTrue,
            actor, _actor,
            {
                actor->bindContext(true);
                CC_LOG_INFO("Device thread detached.");
            });
        for (CommandBufferAgent *cmdBuff : _cmdBuffRefs) {
            cmdBuff->_messageQueue->setImmediateMode(false);
        }
    }

When the ANR occurs, we don’t see message Device thread detached. in debug logs, so we assume that this code is never executed actor->bindContext(true);

Hope it can help.

Hi. Finally we solved the issue!

We did two things:

2 Likes

It’s great, and it gives us experience in solving similar problems.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.