title: Integrating AdMob through Google Play Services with Unity 3D
body: <p>Following <a href="http://fastegggames.com/blog/2013/3/30/tutorial-admob" rel="nofollow">this tutorial</a> I started working on integrating AdMob into Unity, but as this uses an older version I had to make some changes to update it to the Google Play Services version, but I think I may have messed up.</p>
  
  <p>I imported all the required libraries into both Unity 3D's Plugins/Android and Eclipse's Java Build Path and Order and Export.</p>
  
  <p>Every time I call the script in Unity the game crashes and LogCat says it had an error with a tag "AndroidRuntime" "at admob.admob$1.run(admob.java:52) which is this line</p>
  
  <pre><code>adView.loadAd(adRequest);
  </code></pre>
  
  <p>Here's my AdMobController.js from Unity:</p>
  
  <pre><code>#pragma strict
  
  public class AdMobController extends MonoBehaviour {
  
      private static var s_Controller : AdMobController;
      private static var jo:AndroidJavaObject;
  
      function Awake()
      {
          s_Controller = this;
          #if UNITY_ANDROID
          jo = new AndroidJavaObject("admob.admob");
          #endif
      }
  }
  </code></pre>
  
  <h3>File admob.java</h3>
  
  <pre><code>package admob;
  
  import com.google.android.gms.ads.*;
  import com.unity3d.player.UnityPlayer;
  
  import android.app.Activity;
  import android.util.Log;
  import android.view.ViewGroup.LayoutParams;
  import android.widget.LinearLayout;
  
  public class admob{
  
      //  private String pubID = "ADMOB_ID_HERE"; //Your public AdMob ID. Make sure this is correct, or you won't get any credit for the Ad.
      private Activity activity; //Store the android main activity
      private AdView adView; //The AdView we will display to the user
      private LinearLayout layout; //The layout the AdView will sit on
  
      //Constructor
      public admob() {
          activity = UnityPlayer.currentActivity;
  
          activity.runOnUiThread(new Runnable() {
              public void run() {
  
                  /*
                      layout = new LinearLayout(activity);
  
                      activity.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                      adView = new AdView(activity);
                      adView.setAdSize(AdSize.SMART_BANNER);
                      adView.setAdUnitId(pubID);
                      AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
                      adView.loadAd(adRequestBuilder.build());
                  */
  
                  layout = new LinearLayout(activity);
  
                  // Create the adView.
                  adView = new AdView(activity);
                  adView.setAdUnitId("MY_AD_ID"); //Yes, I put the correct ID here. I only deleted it before posting here.
                  adView.setAdSize(AdSize.BANNER);
  
                  // Add the adView to it.
                  layout.addView(adView);
  
                  activity.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  
                  AdRequest adRequest = new AdRequest.Builder()
                      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)       // Emulator
                      .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // Test phone. Not my actual phone because I could
                                                                         // not call AdRequest to get the ID for it.
                      .build();
  
                  // Load the adView with the ad request.
                  Log.d("unity321", "unity321");
                  adView.loadAd(adRequest);
  
                  layout.addView(adView.getRootView(), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                  // Code in here will run on the UI thread of this application
              }
          });
      }
  }
  </code></pre>
  
  <p>AdMob manifest from Eclipse:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.admob.admob"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;meta-data android:name="com.google.android.gms.version"
                     android:value="google_play_services_version"/&gt;
          &lt;activity android:name="com.google.android.gms.ads.AdActivity"
                    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/&gt;
      &lt;/application&gt;
      &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>And finally the manifest in the plugins folder in Unity used to override the Unity built manifest (NOTE: android:value="google_play_services_version" is changed because with the @integer/ both Unity and Eclipse said no value was found and would not export)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.TESTCOMPANY.TESTAPP"
            android:theme="@android:style/Theme.NoTitleBar"
            android:versionName="1.0"
            android:versionCode="1"
            android:installLocation="auto"&gt;
      &lt;supports-screens android:smallScreens="true"
                        android:normalScreens="true"
                        android:largeScreens="true"
                        android:xlargeScreens="true"
                        android:anyDensity="true" /&gt;
      &lt;application android:icon="@drawable/app_icon"
                   android:label="@string/app_name"
                   android:debuggable="true"&gt;
           &lt;activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
                     android:label="@string/app_name"
                     android:screenOrientation="sensorLandscape"
                     android:launchMode="singleTask"
                     android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"&gt;
               &lt;intent-filter&gt;
                   &lt;action android:name="android.intent.action.MAIN" /&gt;
                   &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
               &lt;/intent-filter&gt;
               &lt;meta-data android:name="unityplayer.UnityActivity"
                          android:value="true" /&gt;
               &lt;meta-data android:name="unityplayer.ForwardNativeEventsToDalvik"
                          android:value="true" /&gt;
               &lt;meta-data android:name="com.google.android.gms.version"
                          android:value="google_play_services_version"/&gt;
           &lt;/activity&gt;
           &lt;activity android:name="com.google.android.gms.ads.AdActivity"
                     android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/&gt;
      &lt;/application&gt;
      &lt;uses-sdk android:minSdkVersion="9"
                android:targetSdkVersion="19" /&gt;
      &lt;uses-feature android:glEsVersion="0x00020000" /&gt;
      &lt;uses-feature android:name="android.hardware.sensor.accelerometer" /&gt;
      &lt;uses-feature android:name="android.hardware.touchscreen" /&gt;
      &lt;uses-feature android:name="android.hardware.touchscreen.multitouch"
                    android:required="false" /&gt;
      &lt;uses-feature android:name="android.hardware.touchscreen.multitouch.distinct"
                    android:required="false" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;/manifest&gt;
  </code></pre>