Thursday, 31 July 2014

New Admob Installation:Google Play Services for Monetization



Recently Google announced that their Ad program for Android device Admob leaves old Admob SDK and moving to new Admob with Google Play Services package. Obsolete Admob publishers will not get impressions if don’t move to the new Admob with Play service. In this  tutorial I am going to unveil How to configure new Admob using Google Play services.


New Admob Installation

 

Problem:  Configure New Admob services for Android


Difficulty: Intermediate

Additional Requirements: copy of Google Play Services package, Admob Account, Admob publisher ID.

Step : 1 Create New Site/App

Note : You can skip this method if have an existing app if so save your Publisher ID for future enhancement.
1.  Go to Admob.com
2. Click on Sites & Apps tab  and Click Add Site/App
3.  Select Android from List
4.  Enter you App Name (example Ad Revenue Calculator)
5.  Enter the Android Package URL (example market://details?id=<com.bigknol.adrevenue.calculator>) make sure that the id points to correct package name of your project.
6. Select your Application category (Example Productivity)
7:  Add your App description
8.  Select Keyword Target Ads
9. Press continue button.
10. You can download the AdMob SDK after completing the above steps. Copy the App publisher ID from the final step.
Note : GoogleAdMobAdsSdk-6.4.1.jar is obsolete, you don’t want to use it on your app. 

Step 2: Install Google Play Services to SDK

1. Before you getting started with new Admob service you must install Google Play Services package in SDK, It will be stored in <android sdk>/extras/google/ directory. Open up your Android SDK Manager from Window menu of Eclipse IDE. Move down and go to Extras tick Google Play services (If you are targeting Android Froyo users you need to tick Google Play services for Froyo also) Install all required packages from Android SDK Manager.
New Admob Installation Step 1
2. Copy the Google Play services package.
Many developers forget to move a copy of extras package to workspace for making a reference to project. So you should take a copy of Google play services from <android sdk>/extras/google/ and paste it on your workspace.
3. Linking Google Play services package to project
If you want to use any class of AdMob package you must reference it properly. Otherwise you may get errors like “Admob  Class Not found Exceptions”.  How to link it?  First you need to import the google-play-services_lib to current workspace.
File > Import > Android > Existing Android Code Into Workspace
Browse it from google_play_services/libproject/google-play-services_lib.
Warning: Don’t browse the original Google play services libproject from <android-sdk>/extras/google/google_play_services. Make sure that you have taken libproject from the copied version of Google play services in the workspace.

Step 3: Referencing to Current project

Right click on your project Select Properties



New Admob Installation Step 3
 


Click on Android, press Add button from Library then choose the google-play-services_lib as library. Click on Apply button then finally OK


Admob Installation Step 4
 

Now you can use com.google.android.gms.ads methods and classes in your project.

Step 4:  Setting up Android Manifest File for New Admob SDK

Your android manifest file should have required permissions for executing AdMob. It includes INTERNET and ACCESS_NETWORK_STATE.
1. Add the following permissions (Before application Tag) to Manifest.xml



 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />







2. Add the meta tag to Manifest
You have to add a meta tag that tells you are using the version of google play. Add the following meta tag before closing the application tag.

<meta-data android:name="com.google.android.gms.version"
 android:value="@integer/google_play_services_version"
 />



3. You must register an AdActivity to Manifest file. It will be appeared inside the application tag.



<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

 
4. Save your Manifest File.

Step 5: Configure Admob Views in XML layout

Assume your main layout file is ad_test.xml (Displaying Ads in this layout) you must implement an xmlns to your main layout it might be relative or linear.

1
xmlns:ads="http://schemas.android.com/apk/res-auto"
ad_test.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:ads="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MyAds"
 android:id="@+id/TestAd"
 >
</RelativeLayout>

Next is to add a custom AdView to the ad_test layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:ads="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MyAds"
 android:id="@+id/TestAd"
 >
<com.google.android.gms.ads.AdView android:id="@+id/MyAdView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 ads:adUnitId="XXXXXXX"
 ads:adSize="BANNER"
 />
 </RelativeLayout>

We have already copied the Publisher ID from the beginning of the tutorial; now replace the adUnitId XXXXXX using your on ID. Don’t forget to add adSize property.
You are done!

Step 6: Make Visible your Ads, and Test it on your Emulator, Device

Open your Main Activity File (example:  MyAds.java)

import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MyAds extends Activity {
private AdView adView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_my_ads);
 adView=(AdView)findViewById(R.id.MyAdView);
 AdRequest adRequest=new AdRequest.Builder().build();
 adView.loadAd(adRequest);
 }
 @Override
 public void onPause() {
 adView.pause();
 super.onPause();
 }
@Override
 public void onResume() {
 super.onResume();
 adView.resume();
 }
@Override
 public void onDestroy() {
 adView.destroy();
 super.onDestroy();
 }
}

You may have noticed that, An instance of AdRequest is instantiated finally build it and load from OnCreate Method.
Warning : I recommend test mode of AdMob when you try the above activity.
How to use Test Mode in Admob ?
You might know Google is very strict for preventing fraud clicks against ads they publish through various program like adsense, admob etc. Invalid impressions of Admob ads may cause your account instantly.
You can use test mode by modifying the above code.

  adView=(AdView)findViewById(R.id.MyAdView);
  AdRequest adRequest=new AdRequest.Builder()
 .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
 .addTestDevice("7XXXXXXXXXXXXXXXXXXXXX").build();
 adView.loadAd(adRequest);


If you observe the above code, addTestDevice() method informs to request that you are using the ad test phase not the real ad serving phase. You may ask Where do I get the Device ID (7XXXXXXXXXXX) ?
You can capture it from your logcat when you test it on your emulator.



Admob Installation Step 5