<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pavel </title>
    <description>The latest articles on DEV Community by Pavel  (@pavelb).</description>
    <link>https://dev.to/pavelb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1403652%2F07df1b50-2a81-4e2d-a521-26fa131a3b6f.png</url>
      <title>DEV Community: Pavel </title>
      <link>https://dev.to/pavelb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pavelb"/>
    <language>en</language>
    <item>
      <title>Unity and ANR</title>
      <dc:creator>Pavel </dc:creator>
      <pubDate>Wed, 03 Apr 2024 18:10:21 +0000</pubDate>
      <link>https://dev.to/pavelb/unity-and-anr-oal</link>
      <guid>https://dev.to/pavelb/unity-and-anr-oal</guid>
      <description>&lt;p&gt;My story is about how, by using a closed component, in simple terms a "black box" in the form of an SDK, frameworks, or any other module in our product, we end up in a tricky circle because we are dependent on this "black box".&lt;/p&gt;

&lt;p&gt;In my products, I encountered ANR issues, as I use Unity. Therefore, I will further discuss the problems specifically related to Unity within the Android ecosystem.&lt;/p&gt;

&lt;p&gt;In 99% of Unity projects, when exporting an Android project, the default &lt;code&gt;UnityPlayerActivity&lt;/code&gt; is used. The proxy engine part is located in &lt;code&gt;com.unity3d.player.UnityPlayer&lt;/code&gt;. So, using Android Studio, we can inspect what's inside &lt;code&gt;UnityPlayer&lt;/code&gt;, let's go dipper.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public class UnityPlayer extends FrameLayout implements IUnityPlayerLifecycleEvents&lt;/code&gt; and another 1500 lines of code.&lt;/p&gt;

&lt;p&gt;Let's start with something interesting, an example of ANR from &lt;strong&gt;UnityPlayer&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5ks3t839mpctfif07m5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5ks3t839mpctfif07m5.jpg" alt="Image description" width="800" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the code from Android Studio:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi5nermdzo6r0bnfcxgqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi5nermdzo6r0bnfcxgqq.png" alt="Image description" width="800" height="536"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    private void pauseUnity() {
        this.reportSoftInputStr((String)null, 1, true);
        if (this.mState.f()) {
            if (m.c()) {
                final Semaphore var1 = new Semaphore(0);
                Runnable var2;
                if (this.isFinishing()) {
                    var2 = new Runnable() {
                        public final void run() {
                            UnityPlayer.this.shutdown();
                            var1.release();
                        }
                    };
                } else {
                    var2 = new Runnable() {
                        public final void run() {
                            if (UnityPlayer.this.nativePause()) {
                                UnityPlayer.this.mQuitting = true;
                                UnityPlayer.this.shutdown();
                                var1.release(2);
                            } else {
                                var1.release();
                            }
                        }
                    };
                }

                this.m_MainThread.a(var2);

                try {
                    if (!var1.tryAcquire(4L, TimeUnit.SECONDS)) {
                        com.unity3d.player.f.Log(5, "Timeout while trying to pause the Unity Engine.");
                    }
                } catch (InterruptedException var3) {
                    com.unity3d.player.f.Log(5, "UI thread got interrupted while trying to pause the Unity Engine.");
                }

                if (var1.drainPermits() &amp;gt; 0) {
                    this.destroy();
                }
            }

            this.mState.d(false);
            this.mState.b(true);
            if (this.m_AddPhoneCallListener) {
                this.m_TelephonyManager.listen(this.m_PhoneCallListener, 0);
            }

        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; The first thing that catches your eye is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!var1.tryAcquire(4L, TimeUnit.SECONDS)) {
    com.unity3d.player.f.Log(5, "Timeout while trying to pause the Unity Engine.");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;tryAcquire()&lt;/strong&gt; method with a timeout of 4 seconds is a blocking call. If this operation takes longer than the specified timeout, it can cause an ANR.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; If the &lt;strong&gt;destroy()&lt;/strong&gt; method involves heavy operations, it can also cause an ANR.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (var1.drainPermits() &amp;gt; 0) {
    this.destroy();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3)&lt;/strong&gt; In short, each line of code below performs a heavy operation on the main thread.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var2 = new Runnable() {
                        public final void run() {
                            if (UnityPlayer.this.nativePause()) {
                                UnityPlayer.this.mQuitting = true;
                                UnityPlayer.this.shutdown();
                                var1.release(2);
                            } else {
                                var1.release();
                            }
                        }
                    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
As you may have noticed, the main issue in this code is the handling of events in the main thread and proxying calls from the main thread to native code. Please note, this is not about writing to a file or downloading data from the internet. The way to solve these issues can be through &lt;code&gt;CountDownLatch&lt;/code&gt;, &lt;code&gt;Thread&lt;/code&gt;, &lt;code&gt;HandlerThread&lt;/code&gt;, and many other built-in mechanisms.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;br&gt;
If you enjoyed this article you can like it by clicking on the👏 button (up to 100 times!), also you can share this article to help others.&lt;/p&gt;

&lt;p&gt;Have you any feedback, feel free to reach me on &lt;a href="https://twitter.com/pavel_pbs"&gt;twitter&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
