How to Reduce ANRs when implementing Google Mobile Ads SDK

July 11, 2024 at 6:07:52 AM

TL;DR Google Mobile Ads SDK has updated best practices to reduce Application Not Responding (ANR) errors based on Play Console crash reports. Key recommendations include initializing the Mobile Ads SDK on a background thread and enabling the OPTIMIZE_AD_LOADING manifest flag to offload ad loading tasks to a background thread. These updates aim to minimize ANR occurrences. Updated Android example apps reflect these practices. For more details, refer to the provided resources.

How to Reduce ANRs when implementing Google Mobile Ads SDK

Google received feedback via Play Console crash reports about Application Not Responding (ANRs) errors related to the Google Mobile Ads SDK. After analyzing these reports, Google updated the SDK implementation best practices to reduce ANR rates. The recommended best practices are:

Initialize the Mobile Ads SDK on a Background Thread

Previously, the best practice was to specify the OPTIMIZE_INITIALIZATION manifest flag. However, some work on the calling thread was still required. Now, we recommend calling MobileAds.initialize() on a background thread to handle this work asynchronously. Google provided the Kotlin example below:

import com.google.android.gms.ads.MobileAds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        CoroutineScope(Dispatchers.IO).launch {
            // Initialize the Google Mobile Ads SDK on a background thread.
            MobileAds.initialize(this@MainActivity) {}
            runOnUiThread {
                // Load an ad on the main thread.
                loadAd()
            }
        }
    }
}

Note: When calling MobileAds.initialize() on a background thread, the OPTIMIZE_INITIALIZATION manifest flag is no longer required.

Enable Optimization Flag for Ad Loading

By enabling the OPTIMIZE_AD_LOADING manifest flag, most ad loading tasks can be offloaded to a background thread, reducing the occurrence of ANRs. This flag should be enabled in your app's AndroidManifest.xml file. Google shared the example below:

<manifest>
    ...
    <application>
        ...
        <meta-data
            android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
            android:value="true"/>
    </application>
</manifest>

Google have updated all of the Android example apps to implement these best practices. For more details check out the source below.

Have more questions on this topic? Ask our AI assistant for in-depth insights.

Read more from sources πŸ‘‡

The Only Digital Marketing Feed You'll Ever Need.

Stay informed your way. Tailored updates when and how you want them. 100% Free.

10,000+ Users

500+ Sources

1000+ Tools

Or

Related Posts

Google Ads Dynamic Remarketing no longer requires manual data source setup

Google Ads Dynamic Remarketing no longer requires manual data source setup

Dario Zannoni
Dario Zannoni

Top Creator

Top Paid Media Creator

Dario Zannoni is a Top Paid Media Creator. Part of Swipe Insight Select, a curated list of top creators.

Top Paid Media Creator
Google Ends Conversion Tracking Without Consent Mode v2 Impacting EU Ads Trending ️‍πŸ”₯

Google Ends Conversion Tracking Without Consent Mode v2 Impacting EU Ads

Google Ads launches new tCPC bid strategy to maximize clicks efficiently

Google Ads launches new tCPC bid strategy to maximize clicks efficiently

Thomas Eccel
Thomas Eccel

Top Creator

Top Google Ads Creator

Thomas Eccel is a Top Google Ads Creator. Part of Swipe Insight Select, a curated list of top creators.

Top Google Ads Creator
Marketing Workflow Templates

Marketing Workflow Templates

Featured
Markifact
Markifact

Verified Sponsor

Verified Sponsor

Markifact is a Verified Sponsor. Want to get featured here? Contact us.

Verified Sponsor
Python 3.9 End-of-Life Announcement for Google Ads API Library Support

Python 3.9 End-of-Life Announcement for Google Ads API Library Support

Google for Developers
Google for Developers

Official Source

Official Source

Google for Developers is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Shopping launches AI try-on feature and price alert updates for shoppers

Google Shopping launches AI try-on feature and price alert updates for shoppers

Google Ads AI +1 more
Google
Google

Official Source

Official Source

Google is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Launches Google Trends API for Enhanced Access to Search Trends Data Trending ️‍πŸ”₯

Google Launches Google Trends API for Enhanced Access to Search Trends Data

Google Search Central
Google Search Central

Official Source

Official Source

Google Search Central is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Ads API update allows setting gclid and gbraid fields for ClickConversion imports

Google Ads API update allows setting gclid and gbraid fields for ClickConversion imports

Google for Developers
Google for Developers

Official Source

Official Source

Google for Developers is a Official Source. The source has been verified by Swipe Insight team.

Official Source

Related Tools

Markifact logo

Markifact

Verified Tool

Verified Tool

Markifact is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Marketing Workflows Powered by AI

Featured
Marketing Auditor logo

Marketing Auditor

Verified Tool

Verified Tool

Marketing Auditor is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Automated audits for Google Ads and Analytics.

Get Featured Here

Showcase your tool in this list.

Contact Us
Skai logo

Skai

AI-powered omnichannel marketing and optimization

Ad Management
Branch logo

Branch

Seamless mobile linking and attribution solutions

Smartly.io logo

Smartly.io

Automate and optimize your ad campaigns effortlessly

Ad Management
Grasp logo

Grasp

Automated QA for flawless media buying campaigns

Ad Management
Sembot logo

Sembot

Maximize ROI with AI-driven marketing automation

Ad Management
Zocket logo

Zocket

AI-powered marketing for optimized ad campaigns

Ad Creative
GoDataFeed logo

GoDataFeed

Optimize and manage product feeds across channels

Feed Management
Feedonomics logo

Feedonomics

Optimize and sync product data for more sales

Feed Management

Get Featured Here

Showcase your tool in this list.

Contact Us