AdMob Integration with Cocos2d-x for Android

Hi. I’ve been struggling with AdMob integration for some days, so let me tell you how I did it.

  1. First make the procedures from AdMob site (Android fundamentals: https://developers.google.com/mobile-ads-sdk/docs/android/fundamentals ). Place the JAR in /proj.android/libs/ and add it to Eclipse. Declare the internet permission at the Manifest too. And set the target. You need a recent SDK as well. BTW, I’m using Cocos2d-2.0-rc2-x-2.0.1.

  2. Place a “main.xml” in /proj.android/res/layout/main.xml like the following:

  3. Then modify your /proj.android/src/com/YOURCOMPANY/YOURAPP/APPNAME.java

Add this #include:

import com.google.ads.AdView;

Declare this private variable in the beginning of the class, just after private Cocos2dxGLSurfaceView mGLView;

private AdView mAdView;

And change the onCreate function:

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        if (detectOpenGLES20()) {
            // get the packageName,it's used to set the resource path
            String packageName = getApplication().getPackageName();
            super.setPackageName(packageName);

            setContentView(R.layout.main);
            mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview);

            mGLView.setEGLContextClientVersion(2);
            mGLView.setCocos2dxRenderer(new Cocos2dxRenderer());

            mAdView = (AdView)this.findViewById(R.id.adView);
            mAdView.setVisibility(AdView.VISIBLE);
        }
        else {
            Log.d("activity", "don't support gles2.0");
            finish();
        }   
    }

And that’s it.

Note: I had to remove the Cocos2dxEditText from the main.xml and the creation of mGLView and don’t know if it’s necessary. But couldn’t solve the error messages. I had a black screen with only the Text Box there. So I removed the text box. Sorry.

Other Note: And I don’t know neither if there is memory leak or something when the ads refreshes. Or if there is a way to remove the ads when you change the scene, for example. You would need to declare a couple of JNI functions, I think. But I’ll wait until there is a nice wrapper in GitHub. And the IOS version is missing. I’ll try it later, but I’ve read it’s way easier. Sorry for that too!

Ok then. Enjoy the money!

Thanks dude this works :slight_smile:

But for the new version of Cocos2dx you need to change the onCreate method in the Cocos2dxActivity to

protected void onCreate(final Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);

        this.mHandler = new Cocos2dxHandler(this);

        // this.mGLSurfaceView = onCreateView();

        Cocos2dxHelper.init(this, this);

        setContentView(R.layout.main);

        this.mGLSurfaceView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview);

        this.mGLSurfaceView.setEGLContextClientVersion(2);
        this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());

        mAdView = (AdView) this.findViewById(R.id.adView);
        mAdView.setVisibility(AdView.VISIBLE);
        mAdView.loadAd(new AdRequest());
}

And the main.xml to

In my Eclipse project, the file /proj.android/res/layout/main.xml does not exist. Do I create it?
I’m using 2.0.4 and I created my project by copy-pasting the HelloCpp sample project.

Yep you will have to create it…

@Lance Gray : is it working? I tried same on 2.0.4 but not working for me. :frowning:

Paresh Indulkar wrote:

@Lance Gray : is it working? I tried same on 2.0.4 but not working for me. :frowning:

Nope it doesn’t. COCOS2D-X 2.0.4 don’t use layout XML files anymore and has replaced the previous layout XML files with coded objects in the Cocos2dxActivity class. You’d have to add your own Views in that class.

Hi, it’s easy to integrate Admob.
I don’t use xml because I don’t like xml.
First, don’t modify Cocos2d-x activty file because this file is used by all your projects, if you modify it, all your projects will be modified too.
Using the HelloCpp project example, modify the hellocpp.java file.

I do it using this simple snippet code that I create(put this inside onCreate method):

                try{
                    LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
                            getWindowManager().getDefaultDisplay().getWidth(),                      
                            getWindowManager().getDefaultDisplay().getHeight()+getWindowManager().getDefaultDisplay().getHeight()-50);

                    adView = new AdView(SimpleGame.this, AdSize.BANNER, "a150f54aa7dab27");
                    adView.setAdListener(SimpleGame.this);                          
                    AdRequest request = new AdRequest();
                    request.addTestDevice("34162F3663F54E8A4B036F61F3667A1A");
                    adView.loadAd(request);             
                    // Adding full screen container
                    addContentView(adView, adParams);   
                }catch (Exception e) {
                    FlurryAgent.logEvent("ADMOB ERROR: "+e);
                }

the code above will align Admob horizontaly in center and vertically below

The example only use the AdSize.BANNER that is 320x50 pixels, if you want another size you will need to modify the code

ps:I use admob version 4.3.1, I don’t know if this code work with the lastest version
don’t forget to add admob jar file to your project and the code below in manifest file

thanks Fabio…
you are fabulous, it’s working like charm! :slight_smile:
I used it with Cocos2d-x 2.0.4.

Fabio Cunha wrote:

Hi, it’s easy to integrate Admob.
I don’t use xml because I don’t like xml.
First, don’t modify Cocos2d-x activty file because this file is used by all your projects, if you modify it, all your projects will be modified too.
Using the HelloCpp project example, modify the hellocpp.java file.
>
I do it using this simple snippet code that I create(put this inside onCreate method):
[…]
the code above will align Admob horizontaly in center and vertically below
>
The example only use the AdSize.BANNER that is 320x50 pixels, if you want another size you will need to modify the code
>
ps:I use admob version 4.3.1, I don’t know if this code work with the lastest version
don’t forget to add admob jar file to your project and the code below in manifest file
[…]

Don’t forget to remove this line when you will publish the app

request.addTestDevice("34162F3663F54E8A4B036F61F3667A1A");

Fabio Cunha wrote:

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

Hi - When I add this to my project’s manifest file I get an error:
Description Resource Path Location Type
error: Error: String types not allowed (at ‘configChanges’ with value ‘keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize’). AndroidManifest.xml /Wrapper line 24 Android AAPT Problem

I tinkered around a bit with this and found that if I remove uiMode, screenSize, and smallestScreenSize from the configChanges it will compile. But unfortunalty once I run the app on a device I see a error message where the banner should be that says "You must have AdActivity declared in AndroidManifest.xml with configChanges.

I am not sure how to get this working in my project here is a copy of my manifest:
@

<?xml version="1.0" encoding="utf-8"?>












@

I am using sdk android 2.1

@Jakub L:

To solve that, change your project’s API level to at least API Level 13 (Android 3.2). You can do this by right-clicking your project and selecting Properties > Android and checking Android 2.2. To support lower versions, open your Manifest and add this line:

Where X is the API Level of the lowest Android your app will support while Y is the preferred Android API Level, or the API Level Admob supports. In your case, it will be:

Hope this helps.

Hi ‘Jakub L’,
if you use the tip that ‘Lance Gray’ said, it will work, but Google recommend always target the last sdk available, I think it’s number 16.

I think the latest is 17 (not sure) but yes, that is true. It even shows a warning in Eclipse.

Lance Gray andFabio Cunha Thanks a lot for your replies it works!

Hi guys…… Unfortunately Admob is not working in my Android build. im getting error in this line “adView.setAdListener(Xtry.this);”.
This is the my implementation

try{

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

int width = size.x;
int height = size.y;
System.out.println(“before Add added”);
adView = new AdView(Xtry.this, AdSize.BANNER, “a150f54aa7dab27”);
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(width,height*2-50);
System.out.println(“Add LAYOUT”);
adView.setAdListener((AdListener) Xtry.this);
//AdRequest request = new AdRequest();
//request.addTestDevice(“dcf1280ac405b427”);
//adView.loadAd(request);
System.out.println(“Add Request”);
// Adding full screen container
addContentView(adView, adParams);
System.out.println(“Add added”);
}catch (Exception e) {
//FlurryAgent.logEvent(“ADMOB ERROR:”+e);
System.out.println(“Error :”+e);
}

getting this error

Error :java.lang.ClassCastException: org.Justgames.Xtry.Xtry cannot be cast to com.google.ads.AdListener

What is ‘Xtry’, it’s the name of your class?
If the answer is yes, you must implement the AdListener class
see the example of my code:

public class SimpleGame extends Cocos2dxActivity implements AdListener {
...//much code
}

You already make that?

Hey Fabio Cunha, Thank u so much… Its working fine now.

Some great info in this thread. Helped me alot.

I used the “no xml” changes tactic. I’m a bit stuck with resizing the GL screen size. I don’t want to put ads on top of the game.
So my question is how to make GL screen smaller by 50px, not using the XML (if possible).

Hi ‘rDevsign .raw’,
I don’t understand when you say GL screen.
What I do is create an AdView and make some calculations to make an AdViw with 50px width and be centered in the bottom of the screen.

Take a look in this post in my blog, there a image that show the AdMob Banner that is localized in the bottom of the screen, is that you want?
[[http://fabiosistemas.com.br/blog/admob-cocos2d-x/]]
If you want to put the admob banner in the bottom of the screen, just copy and paste the necessary code from my blog post.

Hello Fabio.

I did use your solution (in essence). The problem is, that Ads are drawn over the actual game (on top of Cocos2dxGLSurfaceView).
I would like to make the Cocos2dxGLSurfaceView smaller by 50px, so the Ad would reside at the bottom 50px of the screen at all times.
Using some layouts … in code.

Example:

full screen size: 800 x 480 (eg Samsung Galaxy S2)
GL view: 800 x 430 (top/middle landscape mode)
Ads: 800 x 50 (bottom of landscape mode)