[Cocos3.0 Tutorial] Integrate Admob ( google play services ) with Cocos2d-x v3.0 Final

@victory144
I use cocos2d-x version 2.2.0. I have no file AppActivity.java, but I have file %my_project_name%.java. I do next things:

  1. In %my_project_name%.java file I change package name from package org.cocos2dx.cpp; to %my_project_package_name% for example: package com.MyCompany.AwesomeGame;

  2. In %my_project_name%.java file added next code

public class %my_project_name% extends Cocos2dxActivity
{
      ...
all code from your instructions
      ...
    static 
    {
        System.loadLibrary("cocos2dcpp");
    }     
}
  1. In AdmobHelper.cpp I change path for classname
//In cocos2d-x version 3.0
const char* AppActivityClassName = "org/cocos2dx/cpp/AppActivity";
//In cocos2d-x version 2.2.0
const char* AppActivityClassName = "%my_project_package_name%";

That’s all, I hope it will be helpful for somebody.

@victory144
It will be cool, if you add in your instructions how to change addmob baner placement (to bottom for example).

1 Like

Very nice tutorial. How can we achieve this through plugin folder provided in cocos2d-x 3.0 folder?

Is it possible to show the Ad with sliding effect?
For instance, the Ad is hidden by default. After player wins the game, it will slide to correct position by calling showAd method.

Sample App: Unblock Me Free

Sorry! I did not realized you were using version 2.2… my fault! :frowning:

Good job all guys!

@den1812 wrote:

@manelizzard
Unfortunately this decision didn’t help me. I understand that the library isn’t loaded, but I don’t understand why.

Yeah, I solved my problem. The problem was in distinction between 3.0 and 2.0 versions cocos2d-x. For version 2.0 at .java file has to be the following code

static 
    {
        System.loadLibrary("cocos2dcpp");
    }     

This tutorial would be helpful

Thanks :slight_smile:

this didn’t work for me. Using Latest cocos build. Latest SDK/NDK. Followed instructions step by step but app crashing.

EDIT :

I just added this to AppActivity nd its worked

static 
{
    System.loadLibrary("cocos2dcpp");
} 

Hi victory144, I feel I am very close to solve this problem and this is thanks to you. I have implemented everything exactly as the tutorial shows, except for the AdHelper.cpp which has some modifications in my case, so I can handle ads on iOS also.

Well, my app is running but ads are not showing in android devices.

The problem I am struggling with now is that my LogCat shows, each X seconds (ad refresh time), this error msg:

06-26 15:30:12.203: I/Ads(14010): Starting ad request.

06-26 15:30:12.223: D/webkit(14010): Firewall not null
06-26 15:30:12.223: D/webkit(14010): euler: isUrlBlocked = false
06-26 15:30:12.233: I/GATE(14010): DEV_ACTION_COMPLETED

06-26 15:30:12.273: W/GooglePlayServicesUtil(14010): Google Play services is missing.
06-26 15:30:12.273: E/GooglePlayServicesUtil(14010): GooglePlayServices not available due to error 1

06-26 15:30:12.363: I/webclipboard(14010): clipservice: android.sec.clipboard.ClipboardExManager@417b9d68
06-26 15:30:12.383: V/(14010): NotificationPresenterImpl::setSettingsValue 0
06-26 15:30:12.383: V/(14010): NotificationPresenterImpl::setSettingsValue 0
06-26 15:30:12.413: V/(14010): NotificationPresenterImpl::setSettingsValue 0
06-26 15:30:12.413: V/(14010): NotificationPresenterImpl::setSettingsValue 0
06-26 15:30:12.413: D/webkit(14010): Firewall not null
06-26 15:30:12.423: D/webkit(14010): euler: isUrlBlocked = false
06-26 15:30:12.423: D/WML_SISO(14010): InitPasteboardJni

06-26 15:30:12.463: W/Ads(14010): AdWebView unable to handle URL: https://10.1.5.254:1003/fgtauth?00001157a94a73f4

It would be awesome if you could give me a hand with this. Thanks!

Its even embarrassing to say but turns out that it was a problem with my internet connection. -.-

Oh mine, really thank you!. I finally solved the problem. It depends on the package name in src folder of the project., so (with cocos2d-x version 2.x.x )AppActivityClassName should be “com/someName/SomeName/projectName”. It took me almost a day!!

Love it! Thanks!

Works a treat in v2.2.x thanks for the good work! Check out my Cocos2d-x release on play store: Micro Gyro :smile:

nice one… Thanks :smile:

Thanks for the great tutorial!

I’m pretty much using exactly this code, but it has the following problem:
If you click and ad and then go back to the game, clicking the back button closes the game.
I think it’s because the webview is catching the key input, instead of it being passed to cocos (key listener).

Do you guys have this issue to?

Thanks for this! :smile:

I’m not able to get the ads to display. I get the following error messages:

Google Play services is missing
There was a problem getting an ad response. ErrorCode: 0
Failed to load ad: 0

Any have any ideas what might be wrong? Testing on 4.4.2 emulator.

Thanks

I followed the tutorial found on the Cocos2d-x wiki (http://www.cocos2d-x.org/wiki/User_Tutorial-Integrate_AdMob) that looks like the same content as this post. I got it working, so I can confirm that the tutorial is valid for this system/software config:
— Cocos2d-x rev. 3.2
— Google Play Services rev. 21
— Android 4.4.2 (API 19)
— Eclipse Luna Release (4.4.0)
— MacBook Pro 10.9.4
— Google Nexus 4 w/ Android 4.4.2

I encountered several speed bumps as I tried to get this to work. I hope this report saves someone a bit of aggravation. This is based on the wiki tutorial.

Documentation error:
http://www.cocos2d-x.org/wiki/User_Tutorial-Integrate_AdMob

The following line in section 2.1 uses the wrong kind of hyphen/dash for one or more of the parameters. If you copy/paste from the tutorial at this location you will get an error when attempting to create your new project.

error:
cocos new admobTest –p com.test.admobTest –l cpp –d PROJECT_LOCATION

corrected:
cocos new admobTest -p com.test.admobTest -l cpp -d PROJECT_LOCATION

resulting error when compiling with build_native.py:
cocos new: error: argument -l/–language is required

Once I added the Google Play Services lib I got an error when building from Eclipse. I did a web search for whatever the error was and found instructions on changing the eclipse.ini settings to allocate a more memory to Eclipse.

  1. After wrestling with the instructions for orienting ads at the bottom of the screen I pasted my final code into the thread on that topic. There are 2 possible solutions:
    http://cocos2d-x.org/forums/6/topics/53945

I encountered some kind of error when building from Eclipse that suggested that there was a conflict between the AndroidManifest.xml file located in the main project and the AndroidManifest.xml file in the Google Play Services lib. That problem eventually went away after I made several changes to Eclipse, but I’m not sure what ultimately fixed it:
— I updated a few of the item sin the Android SDK Manager
— I rebuilt the whole project from zero

  1. I copied AdMobHelper.h/.cpp wholesale from the GigHub repository, but otherwise I tried to leave the default project unchanged and make only the necessary changes to the other files (HelloWorldScene.cpp, AppActivity.java, etc.)

I successfully integrated with com.google.android.gms.ads.*.
Ads works fine.
But I have a problem.

  • touch on the ads from admob, and then drag move to normal area of game screen, now pressing the back key will cause the app dissmiss(going to background?)

  • click on the ads and the view is switched to google play website, press the back key will return the game screen, now in the game screen press the back key again will also cause the app going to background

In both cases, when relaunch app, it crashes.
Does anyone suffer this?

@hzlov: I encountered this as well, you can fix it yourself by giving the cocos activity the focus onResume.

@MFHSchoonbrood Thanks for the tip.

Actually I’m trying to use AdSize.SMART_BANNER.
This smart banner doesn’t open any web site or something at the first touch.
Therefore, first touch Ads with smart banner case doesn’t fire onPause or onResume.

So, I tried to modify Cocos2dxGlSurfaceView.java to catch losing focus and set focus to surface view again.
The modified code worked what I expected - catch losing focus & set focus again -,
but unfortunately It didn’t fix my problem though fixed focus issue :frowning:

@hzlov You could add the same check to dispatchKeyEvent.

I’ll try what you’re doing later, but it sounds like this should catch it always:

@Override
public boolean dispatchKeyEvent(final KeyEvent pKeyEvent)
{
    if (Cocos2dxGLSurfaceView.getInstance() != null && this.getCurrentFocus() != Cocos2dxGLSurfaceView.getInstance())    { Cocos2dxGLSurfaceView.getInstance().requestFocus(); }
    return super.dispatchKeyEvent(pKeyEvent);
}

Kind regards,
Michaël

Our Cocos Helper provides integration help with AdMob and other services https://github.com/SonarSystems/Sonar-Cocos-Helper