Monetising mobile applications is a crucial aspect for developers looking to generate revenue. AdMob, Google’s advertising platform, provides a convenient solution for integrating ads into Android applications. In this tutorial, we’ll walk through a step-by-step procedure to integrate AdMob into an Android app using Kotlin in Android Studio.

Prerequisites

  • Basic knowledge of Kotlin programming language.
  • Installed Android Studio with a configured Android Virtual Device (AVD).
  • A Google AdMob account.

Step 1: Set up AdMob Account

Before integrating AdMob into your Android app, you need to create an AdMob account. Visit the AdMob website (https://admob.google.com/) and sign in with your Google account. Follow the steps to create a new AdMob account if you haven’t already.

Step 2: Create an Ad Unit

Once you’ve logged into your AdMob account, navigate to the “Apps” tab and click on “Add App” to register your Android application. Follow the instructions to add your app details. After adding your app, click on “Ad units” and create a new ad unit. Choose the ad format that suits your app (banner, interstitial, etc.) and provide necessary details.

Step 3: Add AdMob Dependency

Open your Android Studio project and navigate to the build.gradle file for your app module. Add the following dependency to integrate AdMob into your app:

implementation("com.google.android.gms:play-services-ads:20.7.0")

Sync your project to download the necessary dependencies.

Step 4: Update AndroidManifest.xml

Add the following permissions and meta-data elements to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    ...
    <!-- Add the following meta-data, replacing "ca-app-pub-XXXXXXXXXXXXXXXX/YYYYYYYYYY" with your AdMob app ID -->
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-XXXXXXXXXXXXXXXX/YYYYYYYYYY"/>
    ...
</application>

Replace "ca-app-pub-XXXXXXXXXXXXXXXX/YYYYYYYYYY" with your AdMob app ID obtained from the AdMob dashboard.

Step 5: Implement AdView in Layout

Open the layout XML file where you want to display the ad (e.g., activity_main.xml). Add the AdView element:

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX/YYYYYYYYYY"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"/>

Replace "ca-app-pub-XXXXXXXXXXXXXXXX/YYYYYYYYYY" with the ad unit ID you created in the AdMob dashboard.

Step 6: Load Ad in Activity

Open the Kotlin file corresponding to your activity (e.g., MainActivity.kt). Initialize the AdView and load the ad in the onCreate method:

import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdView

class MainActivity : AppCompatActivity() {
    private lateinit var adView: AdView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        adView = findViewById(R.id.adView)

        val adRequest = AdRequest.Builder().build()
        adView.loadAd(adRequest)
    }
}

Step 7: Test Your Implementation

It’s recommended to test your AdMob integration before publishing your app. Add test ads during development to avoid violating AdMob policies. You can do this by adding the following line to your AdRequest:

val adRequest = AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .build()

Make sure to remove test ads before releasing your app.

Conclusion:

Integrating AdMob into your Android app using Kotlin and Android Studio is a straightforward process. By following the steps outlined in this tutorial, you can effectively monetize your app through ads and maximize your revenue potential. Happy coding!

Categorized in:

Android, Code,