Tech 2 Spark

Spark in the News

Post Page Advertisement [Top]

admob Ć ®•Ć ¯Ć ®•Ć ®¾Ć ®© Ć ®ĀŖĆ ®Ÿ Ć ®®Ć ¯Ć ®ŸĆ ®¿Ć ®ĀµĆ ¯
Android app with AdMob and aren't using Firebase. If you plan to include Firebase in your app (or you're considering it), see the AdMob with Firebase version of this guide instead.
Integrating the Google Mobile Ads SDK into an app is the first step toward displaying ads and earning revenue. Once you've integrated the SDK, you can choose an ad format (such as native or rewarded video) and follow the steps to implement it.

Prerequisites

  • Use Android Studio 1.0 or higher
  • Target Android API level 14 or higher

Import the Mobile Ads SDK

Apps can import the Google Mobile Ads SDK with a Gradle dependency that points to Google's Maven repository. In order to use that repository, you need to reference it in the app's project-level build.gradle file. Open yours and look for an allprojects section:

Example project-level build.gradle (excerpt)







allprojects {
    repositories
{
        google
()
        jcenter
()
   
}
}
Add the google() directive above if it's not already present.
Next, open the app-level build.gradle file for your app, and look for a "dependencies" section.

Example app-level build.gradle (excerpt)







dependencies {
    implementation fileTree
(dir: 'libs', include: ['*.jar'])
    implementation
'com.android.support:appcompat-v7:26.1.0'
   
implementation 'com.google.android.gms:play-services-ads:17.1.1'
}
Add the line in bold above, which instructs Gradle to pull in the latest version of the Mobile Ads SDK. Once that's done, save the file and perform a Gradle sync.

Update your AndroidManifest.xml

Add your AdMob App ID to your app's AndroidManifest.xml file by adding the <meta-data> tag shown below. You can find your App ID in the AdMob UI. For android:value insert your own AdMob App ID in quotes, as shown below.






<manifest>
   
<application>
       
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
       
<meta-data
           
android:name="com.google.android.gms.ads.APPLICATION_ID"
           
android:value="[ADMOB_APP_ID]"/>

   
</application>
</manifest>


Initialize MobileAds

Before loading ads, have your app initialize the Mobile Ads SDK by calling MobileAds.initialize() with your AdMob App ID. This needs to be done only once, ideally at app launch.
Here's an example of how to call the initialize() method in an Activity:

Example MainActivity (excerpt)

JAVA

KOTLIN








package ...
import ...
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
   
...
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView
(R.layout.activity_main);

       
// Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
       
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");

   
}
   
...
}


Select an ad format

The Mobile Ads SDK is now imported and you're ready to implement an ad. AdMob offers a number of different ad formats, so you can choose the one that best fits your app's user experience.

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib