AdMob – ShiVa Engine

AdMob

Nav Gupta: “The beauty of this integration is that you do not have to change anything inside your ShiVa app! Simply follow the steps in Eclipse and no edits are required inside ShiVa specifically to get this up and running unless you wish to do anything custom.”
Change the screen orientation in ShiVa to match your PC/Mac (In this case, orientation 0). We will be swapping orientation MANUALLY in Eclipse. Should this pose an issue in your app (If you use portrait mode), then that is the only time you’ll be doing anything in ShiVa specifically. This tutorial handles LANDSCAPE orientation. You can easily swap it around to portrait by editing the text in the orientation step later.
Also we are setting up things in TEST MODE. You simply need to comment this out when you are planning to go live.

Video

[video_lightbox_youtube video_id=”JIWPVqKk0Ng” width=”1024″ height=”600″ auto_thumb=”1″]

Eclipse Steps

Before we begin, I am going to assume you already have Eclipse up and running.
– 1. Open Eclipse
– 2. Click File->New->Java Project
– 3. Name the project the title of your App/Game
– 4. Right click on the newly created project folder inside of Package Explorer
– 5. Click on Import
– 6. Select Archive File and Hit Next
– 7. Select Browse and find the Zip file you generated from the ShiVa UAT
– 8. Download the latest Google Admob SDK for Android
– 9. Right click on the project folder again and this time select New->Folder
– 10. Name the folder “libs” (No Quotes) and hit Finish
– 11. Unzip the SDK and from that window click and drag the GoogleAdMobAdsSdk-x.x.x.jar file to the newly created libs folder inside your project.
– 12. Right click on your project folder again, and this time select properties
– 13. Select Java Build Path and then the Libraries Tab
– 14. Click on Add Jar and select the GoogleAdMobAdsSdk-x.x.x.jar file from the libs folder
– 15. Once imported, click on the Order and Export tab. Make sure that you have checked off the GoogleAdMobAdsSdk-x.x.x.jar file and hit OK
– 16. Open the project.properties file and set the target to android-13 (android OS 3.2) and save the file. Update your Android SDK if you get an error doing this.
– 17. Open local.properties and at the very bottom of the file, place this line of code:

renderscript.opt.level=O0

– 18. Open the AndroidManifest.xml. Select the AndroidManifest.xml tab at the bottom so you can edit the code.
– 19. Go down to line 11 and change the orientation. Your line should look like this:

android:screenOrientation="landscape"

– 20. Go down to line 18 and add the following:

– 21. Go down to line 26 and add the following code (NOTE: When you are going to go live, you will be removing the READ_PHONE_STATE line as it is used only when testing the device to obtain the device ID. This ensures that you do not get banned from Admob for clicking your own ads during testing):

   
   
   
   
   

– 22. Now we will go to the main Java file.
– 23. Expand line 7 (Hit the + sign next to it) and add the following imports:

import com.google.ads.AdRequest;
import android.telephony.TelephonyManager;
import com.google.ads.AdSize;
import com.google.ads.AdView;

– 24. Go down to line 125 and add the following code to the list of variables:

private AdView adView;

– 25. Open a browser window and go to http://www.admob.com and create a new android site.
– 26. Setup all the basics about your app there and then you will be given a Publisher ID which you will use in the next step.
– 27. Inside onCreate, go down to line 174 and add the following code:

        // Create an ad (Note: You can choose AdSize.BANNER, AdSize.SMART_BANNER, etc.
        this.adView = new AdView(this, AdSize.SMART_BANNER, "PUBLISHER_ID_GOES_HERE");
        AdView.LayoutParams adViewParams = new AdView.LayoutParams(
        AdView.LayoutParams.WRAP_CONTENT,
        AdView.LayoutParams.WRAP_CONTENT);
        //the next line is the key to putting your ad on the bottom.  If you want it on
        //the top, you simply use ALIGN_PARENT_TOP.
        adViewParams.addRule(AdView.ALIGN_PARENT_BOTTOM);

– 28. Go down a little further to approx. line 186 (~194-195 approx), right after oSplashView and oViewGroup, and paste the following:

      // make sure it is before the first oViewGroup.addView:
      oViewGroup.addView    ( this.adView, adViewParams ) ;

– 29. After that, go down to line 190 (~212) (before the createAsync) add the following:

        AdRequest adRequest = new AdRequest();
        //Comment out the following lines when going LIVE to public
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
        final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
        String deviceid = tm.getDeviceId();
        adRequest.addTestDevice(deviceid);
        //END of Testing Area
        this.adView.loadAd(adRequest);
        this.adView.bringToFront();
        oViewGroup.bringChildToFront(this.adView);

– 30. Go down to line 255 (~285) if you need to disable wakelock – simply change the setting to false instead of true.
– 31. Next, go down to line 299 (~282) and add the following line after the oViewGroup.addView:

oViewGroup.bringChildToFront( adView );

– 32. Go down to line 427 (~511) and add the following code after the IF statement, so you can see your ad again after a resume:

            if ( adView != null )
            {
               oViewGroup.bringChildToFront( adView );
            }

– 33. Go down to line 492 (~573 onDestroy Function) and add the following IF statement after the o3DView IF statement:

        // Destroy the AdView.
        if (adView != null) {
          adView.destroy();
        }

– 34. Comment out the following line (approx. 874 (~1000)) so it looks like this:

//oThis.setRequestedOrientation ( ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ) ;

This makes sure we do not have the screen change on its own after coming back to app.
– 35. Comment out the following line (approx. 905 (~1030)) so it looks like this:

//oThis.setRequestedOrientation ( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ) ; // TODO: restore original orientation

This makes sure we do not have the screen change on its own after coming back to app.
– 36. Compile and test on your device. If successful, simply comment out the test mode lines and you should be ready to go live!

Links

Banner Ad positioning fix
Another Tutorial @ jameselsey.co.uk
Admob Ad Positioning
Another Tutorial @jmsliu.com
Fix for Android Version @stackoverflow
Other Code Help @stackoverflow
Latest Version Code @stackoverflow
Previous Version Youtube Video by Fragagames
Google AdMob Ads SDK TestMode
DeviceID Code @stackoverflow




Need more answers?

  • slackBanner