Giter Site home page Giter Site logo

Comments (7)

AntonPetrov83 avatar AntonPetrov83 commented on July 20, 2024 3

Inside the Unity game loop I tried delaying stuff with System.Threading.Thread.Sleep(6000); but to no avail. Nothing gets triggered. Maybe blocking the Unity game loop does not cause ANRs?

Yeah, ANR triggers only when main thread is blocked and Unity runs on Unity-Main thread which is not main ))
To emulate an ANR you can use code:

public class AndroidShowStopper : MonoBehaviour
{
    private static bool _stop;

    public static void Stop()
    {
        _stop = true;
    }
    
#if UNITY_ANDROID
    void Update()
    {
        if (_stop)
        {
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            activity.Call("runOnUiThread", new AndroidJavaRunnable(runOnUiThread));
            
            _stop= false;
        }
    }

    void runOnUiThread()
    {
        Debug.Log("I'm running on the Java UI thread!");

        while (true);
    }
#endif
}

Just call AndroidShowStopper.Stop(); somewhere.

from anr-watchdog.

AntonPetrov83 avatar AntonPetrov83 commented on July 20, 2024

Okay, this is by design :(

Under the hood Crashlytics trims stack-traces longer then 1024 entries. ANRError collects all thread stacks as a single long stack-trace and it gets trimmed by Crashlytics.

So the solution is either crash with Fatal error to allow Crashlytics collect all threads or collect threads by ourselves and send it somewhere.

from anr-watchdog.

mastef avatar mastef commented on July 20, 2024

@AntonPetrov83 unrelated but would be cool if you could share your solution on how to use ANR-WatchDog with unity. Lots of people have now ANR issues since recently

from anr-watchdog.

AntonPetrov83 avatar AntonPetrov83 commented on July 20, 2024

@AntonPetrov83 unrelated but would be cool if you could share your solution on how to use ANR-WatchDog with unity. Lots of people have now ANR issues since recently

@mastef create a derived class from UnityPlayerActivity like described here: Extending the UnityPlayerActivity Java Code

Then write something like that:

    @Override protected void onCreate(Bundle savedInstanceState)
    {
        ANRWatchDog watchDog = new ANRWatchDog();
        watchDog.setReportMainThreadOnly();
        watchDog.start();

        super.onCreate(savedInstanceState);
    }

from anr-watchdog.

mastef avatar mastef commented on July 20, 2024

@AntonPetrov83 unrelated but would be cool if you could share your solution on how to use ANR-WatchDog with unity. Lots of people have now ANR issues since recently

@mastef create a derived class from UnityPlayerActivity like described here: Extending the UnityPlayerActivity Java Code

Then write something like that:

    @Override protected void onCreate(Bundle savedInstanceState)
    {
        ANRWatchDog watchDog = new ANRWatchDog();
        watchDog.setReportMainThreadOnly();
        watchDog.start();

        super.onCreate(savedInstanceState);
    }

Got there after writing the comment. But the setReportMainThreadOnly is interesting! Thank you for the hint!

from anr-watchdog.

mastef avatar mastef commented on July 20, 2024

@AntonPetrov83 Thanks for the help! For future reference I'm adding the code here. Unfortunately I'm not able to trigger ANRs from Unity.

package com.mycompany.gamepackage;

import com.github.anrwatchdog.ANRError;
import com.github.anrwatchdog.ANRWatchDog;
import com.github.anrwatchdog.ANRWatchDog.ANRListener;
import com.unity3d.player.UnityPlayerActivity;

import android.os.Bundle;
import android.util.Log;

public class GameActivity extends UnityPlayerActivity
{

    // Setup activity layout
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        Log.d("GameActivity", "WatchDog starting");
        new ANRWatchDog(3000)
            .setIgnoreDebugger(true)
            .setReportMainThreadOnly()
            .setANRListener(new ANRWatchDog.ANRListener() {
                @Override
                public void onAppNotResponding(ANRError error) {
                    Log.e("GameActivity", "", error);
                    Log.d("GameActivity", "ANR Caught");
                }
            })
            .start();

        Log.d("GameActivity", "WatchDog started");
        super.onCreate(savedInstanceState);
    }
}

This is confirmed working.

Inside the Unity game loop I tried delaying stuff with System.Threading.Thread.Sleep(6000); but to no avail. Nothing gets triggered. Maybe blocking the Unity game loop does not cause ANRs?

from anr-watchdog.

WaheedRashad avatar WaheedRashad commented on July 20, 2024

Hi!

I integrated ANR-WatchDog to a Unity game using custom implementation of UnityPlayerActivity.

At first I released a game with the default behaviour of the ANR-WatchDog. My app crashed due to unhandled ANRError and Firebase Crashlytics recorded something around 40 threads at this moment including UnityMain thread and others. I used setReportMainThreadOnly() because Crashlytics collects all threads by itself on fatal errors.

Then I turned this crash to a non-fatal using this code:

    @Override protected void onCreate(Bundle savedInstanceState)
    {
        ANRWatchDog watchDog = new ANRWatchDog();
        watchDog.setANRListener(new ANRWatchDog.ANRListener() {
            @Override
            public void onAppNotResponding(ANRError error) {
                FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
                crashlytics.recordException(error);
            }
        });
        watchDog.start();

        super.onCreate(savedInstanceState);
    }

This code records ANRError as a non-fatal exception and Crashlytics attaches stack traces contained in ANRError. That is why you do not see setReportMainThreadOnly() here any more. But now crash reports a very short, containing only 8-10 threads. What is strange there were no UnityMain thread while this non-fatal was triggered in the middle of a gameplay.

Please help, any ideas on how to get full list of stack traces attached?

How to integrate ANR-WatchDog to a Unity game using custom implementation of UnityPlayerActivity?

from anr-watchdog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.