Sunday, February 23, 2014

Integrating Google ads for mobile into an android app

makesure you have ant debug/release runs well with your application root folder with google-play-services-lib and you have creted the adUnitID for Banners/InterstitialAd with an account for your android app to monitize (to show google ads in your app and make money)

Now in your application main file the class which is the etry point of your Activity or which extends CordovaACtivity you should (not necessarily true always) implement the Banners/InterstitialAd.

How to implement Banners


import the following packages

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

declare variables to hold the objects

private static final String AdMob_banner_Ad_Unit = "GOOGLE_ADMOB_BANNER_ID";
private AdView m_adView; 

in  public void onCreate(Bundle savedInstanceState) function 
write the below code

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
                
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
        
        m_adView = new AdView(this);
        m_adView.setAdUnitId(AdMob_banner_Ad_Unit);
        m_adView.setAdSize(AdSize.BANNER);
         
        LinearLayout layout = super.root;
        layout.addView(m_adView); 
        AdRequest request1 = new AdRequest.Builder().build();
        m_adView.loadAd(request1);
}

also update the other override methods as

    @Override
    public void onPause() {
    m_adView.pause();
      super.onPause();
    }

    @Override
    public void onResume() {
      super.onResume();
      m_adView.resume();
    }

    @Override
    public void onDestroy() {
    m_adView.destroy();
      super.onDestroy();
    }


To Implement InterstitialAd

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

declare variables to hold the objects

private InterstitialAd m_interstitial;
private static final String AdMob_interstitial_Ad_Unit = "GOOGLE_ADDMOB_INTERSTITIAL_UNIT_ID";

in  public void onCreate(Bundle savedInstanceState) function 
write the below code

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        super.loadUrl(Config.getStartUrl());
        
        // Create the interstitial.
        m_interstitial = new InterstitialAd(this);
        m_interstitial.setAdUnitId(AdMob_interstitial_Ad_Unit);
        
        m_interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                //super.onAdLoaded();
                displayInterstitial();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                //super.onAdFailedToLoad(errorCode);
            }
        });

     // Create ad request.
        AdRequest adRequest2 = new AdRequest.Builder()
        .build();

        // Begin loading your interstitial.
        m_interstitial.loadAd(adRequest2);         
}

 // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
      if (m_interstitial.isLoaded()) {
     m_interstitial.show();
      }
    }

integrating google_play_service with phone gap android application

It was even very painful foe me while integrating the Google Admobs which requires google_play_services to be integrated ! here is my share of tricks how I solved the play services Build problem.

When you try to publish an app you have to create a release build for which either you have to create release build with below command

  • ant release
  • cordova build --release

If you have not built anytime the play service with ant
You get the following error !

D:\workspace\HTML5\Wordizy>ant debug/release
Buildfile: build.xml does not exist!
Build failed

to fix this (ant must have been setup)
copy the whole google-play-services_lib folder from ANDROID_SDK_PATH\extras\google\google_play_services\libproject\to your project root directory.
copy
build.xml
local.properties
project.properties
files from root folder/Cordova lib folder to google-play-services_lib folder

Change the project.properties file's project target to the right target (very important)

split.density=false
# Project target.
target=android-19 (important)
android.library=true

change android.library.reference.2=google-play-services_lib in your root/project project.properties file

Stand on the google-play-services-lib and in the cmd editor supply
android update project -p . (dot=current folder) you should not be getting any error
then supply the command
ant debug success
ant release success

Now you are done !

You can also call cordova build --release command at the root folder of the application and it should create a unsigned-release.apk file.