Last Update November 5, 2020: We improved the overall article.
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
private AdView adView;
adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit); LinearLayout layout = super.root; layout.addView(adView); AdRequest request = new AdRequest();
adView.loadAd(request);
adView.loadAd(request);
and ads will start showing up in the AVDs.request.addTestDevice(AdRequest.TEST_EMULATOR);
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
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"/>
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