Google AdMob is a popular advertising platform that allows developers to monetize their Android apps by displaying ads. In this blog post, we will discuss how to implement Google AdMob in Android apps using Kotlin and Java.
Prerequisites
Before we begin, make sure you have the following in place:
- Android Studio installed on your machine.
- An active Google AdMob account.
Step 1: Set up your Google AdMob account
To get started, sign in to your Google AdMob account (or create a new one) at https://admob.google.com. Once you're signed in, create a new app by clicking on the "+ Monetize new app" button. Follow the instructions provided to complete the setup.
Step 2: Configure your app
Add the Google AdMob dependency
In your Android project, open the app-level build.gradle file and add the following dependency:
implementation 'com.google.android.gms:play-services-ads:20.3.0'
Add AdMob app ID
Open your AndroidManifest.xml file and add the following line under the <application>
tag:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/admob_app_id" />
Next, open your strings.xml file and add the following line:
<string name="admob_app_id">YOUR_ADMOB_APP_ID</string>
Replace YOUR_ADMOB_APP_ID
with the app ID you obtained from your AdMob account.
Request app permissions
Make sure your app has the necessary permissions to display ads. Open the AndroidManifest.xml file and add the following permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Add an AdView to your layout
Open the layout file where you want to display the ad, and add the following code:
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
/>
Add ad unit ID
Open your strings.xml file and add the following line:
<string name="banner_ad_unit_id">YOUR_BANNER_AD_UNIT_ID</string>
Replace YOUR_BANNER_AD_UNIT_ID
with the ad unit ID for the banner ad you created in your AdMob account.
Step 3: Load and show ads
To load and show ads in your app, you can use the following code:
Kotlin
// Add these imports at the top of your file
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdView
// In your activity or fragment
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)
}
Java
// Add this import at the top of your file
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
// In your activity or fragment
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
Conclusion
By following the steps outlined in this blog post, you can easily implement Google AdMob in your Android app. Monetizing your app with ads can be a great way to generate revenue and make your app more profitable. Remember to comply with Google's policies and guidelines when implementing ads in your app. Happy coding!
本文来自极简博客,作者:梦想实践者,转载请注明原文链接:Implementing Google AdMob in Android Apps