Monetizing Apps using Google Mobile Ads SDK for Flutter

In this guide, we will learn how to monetize a Flutter app πŸ“±(AdMob Ads) using google_mobile_ads package.

Integrating Google Mobile Ads SDK into a Flutter app is the first step towards displaying AdMob ads and earning revenue. Currently the plugin supports loading and displaying banner, interstitial (full-screen), native ads, and rewarded 🎁 video ads .

Monetizing Apps using Google Mobile Ads SDK
Monetizing Apps using Google Mobile Ads SDK

In this guide, we will learn how to integrate banners ads, interstitial (full-screen) ads and rewarded video ads into a Flutter app.

We will build a sample application which will display test ads using sample ad units. These ad units are not associated with AdMob account and are sample ad units that are used for building and testing your apps.

Rewarded ads provide an opportunity for users to watch a video or engage with a playable ad in exchange for a reward within the app.

🎁 Reward Coins increases after watching the Rewarded Video Ad

How do I display AdMob ads in my Flutter app and start earning revenue πŸ’°?

This guide explains how to enable test ads in Flutter app. These ad units are not associated with your AdMob account and are sample ad units that are used for building and testing your apps.

If you want to publish your app to Play Store or App Store and start displaying AdMob ads and earn revenue then you will have to add your AdMob app ID in your AndroidManifest.xml file. You can find your AdMob app ID in the AdMob UI add it to your AndroidManifest.xml file. Also replace testAdUnitId with an ad unit ids from AdMob to display real ads to your app users.

Recommended: Create an AdMob account and register an Android app

Prerequisites πŸ“

Make sure you have installed/upgraded the Prerequisites : Developer Guide

  1. Flutter 1.22.0 or higher βœ…
  2. Android
  • Android Studio 3.2 or higher βœ…
  • Target Android API level 19 or higher βœ…
  • Set compileSdkVersion to 28 or higher βœ…
  • Android Gradle Plugin 4.1 or higher βœ…

step 1 : Adding the package in pubspec.yaml

Flutter Package : google_mobile_ads

It is a plugin for flutter that supports loading and displaying banner, interstitial(full-screen), and rewarded video ads.

dependencies:google_mobile_ads: ^0.11.0+4
Adding Flutter Package
Adding Flutter Package
Adding Flutter Package

To learn how to use packages in flutter refer our (Using Flutter Packages Guide)

step 2 : Add meta data to Android-Manifest.xml

  • Navigate to Android Manifest.xml file
  • Path to file : android/app/src/main/AndroidManifext.xml
  • Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713
<manifest>
<application>
<!-- TODO: Replace ca-app-pub-################~########## with your AdMob App ID or Sample Admob App ID -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-################~##########"/>
</application>
</manifest>
Add meta data to Android-Manifest.xml
Add meta data to Android-Manifest.xml
Add meta data to Android-Manifest.xml

For more info : Developer Guide - Android Manifest xml

step 3 : Coding β€” Sample Flutter App Code

Note: Enable Wi-Fi or mobile network on your test device or emulator to see test ads

πŸ“ŒHow to Integrate Banner Ads into a Flutter app ?

Flutter Mobile Ads AdMob Banner Ad
Flutter Mobile Ads AdMob Banner Ad
Flutter Banner Ad

πŸ“ŒHow to Integrate Interstitial Ads into a Flutter app ?

Flutter Mobile Ads AdMob Interstitial Ad
Flutter Mobile Ads AdMob Interstitial Ad
Flutter Interstitial Ad

πŸ“ŒHow to Integrate Rewarded Video Ads into a Flutter app ?

Flutter Mobile Ads AdMob Rewarded Video Ad
Flutter Mobile Ads AdMob Rewarded Video Ad
Flutter Rewarded Video Ad

πŸ’»This is a simple app demo to learn how to integrate admob ads in your flutter app.

Flutter Google Mobile Ads SDK - Dart Code
$ flutter clean
$ flutter run

GitHub Repository :πŸ“‚ flutter_test_ads_app

πŸ”¨ Top 3 Common Error Messages and Guide to Fix them

You can skip this section if you are not facing any issues/errors

ERROR 1 : Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:google_mobile_ads]

Running Gradle task 'assembleDebug'...                                  
/android/app/src/debug/AndroidManifest.xml Error:
uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:google_mobile_ads] /build/google_mobile_ads/intermediates/library_manifest/debug/AndroidManifest.xml as the library might be using APIs not available in 16
Suggestion: use a compatible library with a minSdk of at most 16,
or increase this project's minSdk version to at least 19,
or use tools:overrideLibrary="io.flutter.plugins.googlemobileads" to force usage (may lead to runtime failures)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:google_mobile_ads] build/google_mobile_ads/intermediates/library_manifest/debug/AndroidManifest.xml as the library might be using APIs not available in 16
Suggestion: use a compatible library with a minSdk of at most 16,
or increase this project's minSdk version to at least 19,
or use tools:overrideLibrary="io.flutter.plugins.googlemobileads" to force usage (may lead to runtime failures)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 35s
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done 156.1s (!)
Exception: Gradle task assembleDebug failed with exit code 1

How to FIX this error :

  • Navigate to build.gradle file
  • Path to file : android/app/build.gradle

Check for the following versions :

  • compileSdkVersion 29
  • minSdkVersion 19 βœ…
  • targetSdkVersion 29
minSdkVersion 19 or higher
minSdkVersion 19 or higher
minSdkVersion 19 or higher

ERROR 2 : The built failed likely due to AndroidX incompatibilities in a plugin. The tool is about to try using Jetfier to solve the incompatibility.

FAILURE: Build failed with an exception.                                

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
> Android resource linking failed
/home/.gradle/caches/transforms-2/files-2.1/554200784d0157df45da8212b6e09519/play-services-ads-lite-19.7.0/AndroidManifest.xml:27:5-43:15: AAPT: error: unexpected element <queries> found in <manifest>.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done 9.9s
The built failed likely due to AndroidX incompatibilities in a plugin. The tool is about to try using Jetfier to solve the incompatibility.
Building plugin google_mobile_ads...
Exception: The plugin google_mobile_ads could not be built due to the issue above.

How to FIX this error :

  • Path to file : /android/build.gradle
dependencies { classpath 'com.android.tools.build:gradle:3.5.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
  • Change version to 4.1.1
dependencies { classpath 'com.android.tools.build:gradle:4.1.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
build gradle version 4.1.1
build gradle version 4.1.1
build gradle version 4.1.1

ERROR 3 : Minimum supported Gradle version is 6.5. Current version is 5.6.2. If using the gradle wrapper, try editing the distributionUrl in /android/gradle/wrapper/gradle-wrapper.properties to gradle-6.5-all.zip

FAILURE: Build failed with an exception.                                

* Where:
Build file '/android/app/build.gradle' line: 24

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin [id 'com.android.internal.version-check']
> Minimum supported Gradle version is 6.5. Current version is 5.6.2. If using the gradle wrapper, try editing the distributionUrl in /android/gradle/wrapper/gradle-wrapper.properties to gradle-6.5-all.zip

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 425ms
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done 0.9s
Exception: Gradle task assembleDebug failed with exit code 1

How to FIX this error :

Edit the distributionUrl in gradle-wrapper.properties file

  • Path to file : /android/gradle/wrapper/gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
  • Change the distributionUrl zip package to gradle-6.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
Gradle wrapper properties distribution urls - gradle-6.5-all.zip
Gradle wrapper properties distribution urls - gradle-6.5-all.zip
Gradle wrapper properties distribution urls - gradle-6.5-all.zip

πŸ“šUseful Resources

Originally published at https://blog.akshatapp.com on March 29, 2021.

Helping designers and developers build high quality websites and cross platform mobile apps with our step-by-step developer guides πŸ“š at akshatapp.com

Helping designers and developers build high quality websites and cross platform mobile apps with our step-by-step developer guides πŸ“š at akshatapp.com