I've found a fairly easy way to add Interstitial Admob to Android applications. For me, iOS is easy, as I can code in Objective C, but Android is a really pain for ad networks that only provide a Java interface.
The solution uses EasyNDK, and the author of this excellent bit of code put this blog post together:
http://ideasrefactored.wordpress.com/2013/03/02/chartboost-integration-in-cocos2dx-game-2/
This post just expands slightly on some of the eclipse/Java stuff, just to make it clear for people from an iOS background.
Firstly, download EasyNDK
https://github.com/aajiwani/EasyNDK-for-cocos2dx
Now you need to add two of the files, AndroidNDKHelper.java and NDKHelper.java into your project. Drag the two files into the src folder in Eclipse. Eclipse will add them, but not put them in the right place, so you'll get red crossed next to them.. Open each file, click the error on the Package line at the top, and choose the option to put them into com.easyndk.classes.
Now you need to add the C++ files. There are two folders, jansson and NDKHelper. Drag these into with your C++ classes. I had to change the jansson include in NDKHelper.h to:
#include "../jansson/jansson.h"
You then need to add these to your Android.mk:
../../Classes/jansson/dump.c \ ../../Classes/jansson/error.c \ ../../Classes/jansson/hashtable.c \ ../../Classes/jansson/load.c \ ../../Classes/jansson/memory.c \ ../../Classes/jansson/pack_unpack.c \ ../../Classes/jansson/strbuffer.c \ ../../Classes/jansson/strconv.c \ ../../Classes/jansson/utf.c \ ../../Classes/jansson/value.c \ ../../Classes/NDKHelper/NDKCallbackNode.cpp \ ../../Classes/NDKHelper/NDKHelper.cpp
Now back to the Java side, your need to add these imports to your project's Cocos2dxActivity class:
import org.json.JSONObject; import org.json.JSONException; import org.json.JSONObject; import com.easyndk.classes.AndroidNDKHelper;
Now add:
AndroidNDKHelper.SetNDKReciever(this);
...to your OnCreate method. Don't forget this otherwise it will crash!
You then need to add Admob to your projects as per the Admob instructions. I'm using the AdMob jar, although Google now recommend your use Play services. I'm assuming you know how to do this part (Project Properties, Java Build Path, Libraries, Add External Jar, chooses the Admob SDK jar file. Then go to the Order and Export tab and tick it).
Now add Admob as per the instructions:
import com.google.ads.*;
while you are there, add these as well:
import com.google.ads.AdRequest.ErrorCode; import android.util.Log;
Not sure why, but I find the first one needs to be explicitly imported. The second one is so we can get logs about whether our ads are being received or not.
Add this to your Activity Class:
private InterstitialAd interstitial;
Add a method to create the Ad. Obviously you need to add in your own Ad ID and test devices instead of the XXXX's.
public void CreateAdmob() { // Create the Admob interstitial = new InterstitialAd(this, "ca-app-pub-xxxx"); AdRequest adRequest = new AdRequest(); adRequest.addTestDevice("xxx"); adRequest.addTestDevice("xxx"); interstitial.loadAd(adRequest); interstitial.setAdListener(this); }
Now add the code to deal with the add life cycle. First set your class to implement AdListener:
> public class YourGame extends Cocos2dxActivity implements AdListener {
And add the methods for the AdListener:
@Override public void onReceiveAd(Ad ad) { Log.d("OK", "Received ad"); if (ad == interstitial) { interstitial.show(); } } @Override public void onDismissScreen(Ad arg0) { // TODO Auto-generated method stub } @Override public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) { // TODO Auto-generated method stub } @Override public void onLeaveApplication(Ad arg0) { // TODO Auto-generated method stub } @Override public void onPresentScreen(Ad arg0) { // TODO Auto-generated method stub }
If you want an ad to show when your app first loads add:
CreateAdmob();
.. to your oncreate method.
Finally, you can add the code to display an ad when ever you want from you C++ code,
First, add a method to receive your call for an ad:
public void ShowAdmobInter(JSONObject prms) { CreateAdmob(); }
Now go to your C++ class where you want to show the ad.
#include "NDKHelper/NDKHelper.h"
and in the place that you want to show the ad:
> #if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) SendMessageWithParams(string("ShowAdmobInter"), NULL); #endif
Finally, you need to sort out AndroidManifest.xml.
Add the following activity:
and the following permissions:
>
If you get an error on the activity line you probably need to to your Android version. Go to project.properties and change the android version to 14 or higher.
and that should be it.
hi.. thanks for the tutorial... would this work on ios also???
ReplyDeleteThe info above is just for Android, but EasyNDK does allow you to use the same code for iOS and Android. Have a look at : http://ideasrefactored.wordpress.com/2013/03/02/chartboost-integration-in-cocos2dx-game-2/
ReplyDeleteHI... i managed to get the ios part of admob working... for android do i need to include jasson and ndkhelper into classes folder? also the git has changed... there is no AndroidNDKHelper.java anymore...
ReplyDeletei removed the jasson folder as cocos2d-x said that it was duplicate but is still get errors saying button cannot be resolved in easyndk.java...
Also I dont how to ad listeners.... could you update the post...???
I am using cocos2d-x version 2.2.2 by the way... thanks
I want to do that with windows, please provide solution .
ReplyDeleteHi, I've added banners but not interstitials to Windows phone. The quality of the ads was really poor and the income very low, so I'm probably not going to bother figuring out how to add interstitials at the moment. If anything change I'll post how to do it though.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete