Google’s AdSense program for all web publishers to generate revenue off their content, for mobile app developers there’s Google’s AdMob ads platform to monetize and promote their web app. PhoneGap is a great tool to convert JavaScript rich web apps to native mobile app .i’ll go through a simple process that I came across to integrate the native admob android sdk to your Phonegap/Cordova apps

Admob Native Android SDK Integration

Now you need just follow these few steps:


Install the Admob Android SDKDownload the Android Admob SDK, extract it and copy GoogleAdMobAdsSdk-*.*.*.jar to the PhoneGap project’s libs directory.


project’s libs directory

Select the Project ‘File’ -> ‘Project Structure’ from the  Modules .
Now select  Source Tab  select the ‘+ Add Content Root’ Button and by opening windows Select Your  GoogleAdMobAdsSdk-*.*.*.jar file
Select yourProject/libs/GoogleAdMobAdsSdk-*.*.*.jar


yourProject/libs/GoogleAdMobAdsSdk

Include Admob’s Library

 Add the following lines in your MainActivity.java right below other imports.

import com.google.ads.*;
import android.widget.LinearLayout;

 Here, the first import contains the functions related to ads while the second one will help us in inserting the ads into the screen.

Configure your AdMob ad unit ID

Now, inside the main class of your MainActivity.java file, create a new private string AdMob_Ad_Unit like this:

 
private static final String AdMob_Ad_Unit = "xxxxxxxxxxxxxxx";

Make sure to replace xxxxxxxxxxxxxxx with your own ID as putting incorrect ID there will result in no ads showing up.

Create the Ad View

Below the newly added string, add one more private variable adView of AdView type like this:
private AdView adView;
This will be the container for the ad. Now, inside the onCreate function, add the following lines at the bottom:
 
adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit);
LinearLayout layout = super.root;
layout.addView(adView);
AdRequest request = new AdRequest();
adView.loadAd(request); 
This should be enough for the ads to show up in a real device. However, if you want to test the ads on a Android Virtual Machine, then you’ll have to add this line above adView.loadAd(request); and ads will start showing up in the AVDs.
request.addTestDevice(AdRequest.TEST_EMULATOR);
Make sure to comment out the above added line before publishing the app for obvious reasons!
So the entire code file should look like this:
package com.project.android;

import android.os.Bundle;
import org.apache.cordova.*;
import com.google.ads.*;
import android.widget.LinearLayout;

public class MainActivity extends DroidGap
{
private static final String AdMob_Ad_Unit = "xxxxxxxxxxxxxxx";
private AdView adView;

@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
// Set by in config.xml
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///android_asset/www/index.html")

adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit);
LinearLayout layout = super.root;
layout.addView(adView);
AdRequest request = new AdRequest();

// Comment this out before publishing.
// request.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(request);

}
}

Final Steps

The last step is to define the newly created Ads activity into the AndroidManifest.xml file. Add this line in your manifest file after the closing tag of your main activity.
 
 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
Now you must be able to see the ads when running the app in an emulator or on a real device. If you run into a problem or doesn’t get it to work then drop a comment here and i will  help you! 

HTML5 Hybrid Apps with Admob* Cordova* plug-in

If you want to include AdMob advertisements as part of your HTML5 hybrid mobile app, you will need to use a Cordova plugin.
There are a variety of Cordova plugins available for serving ads; some serve ads from third-party sources. A few serve ads from the AdMob network.
For example, the com.google.admob Cordova plug-in provides the capability to initiate Banner and Interstitial ads as well as handle admob ad events.

how to install google admob using phonegap plugin

Flowing the steep see here https://github.com/admob-google/admob-cordova

SHARE
Previous articlehow to change android studio theme
Next articlehow to add Google Analytics Android Apps & Native Application

Abdullah Al Mahamud is a programmer and founder of Thedevline an Inspiration blog since 2014. live in Bangladesh. He is passionate about Technology and Design. He works on the freelancing marketplace as a freelancer and has also written for Useful Article in this blog

Leave a Reply

avatar
  Subscribe  
Notify of