I have looked for this so long. So I post here, for the others, in order to find it easier. This worked for me with cocos2d-x 3.1. I think this is the latest fresh step by step tutorial.
This tutorial links are for iOS:
When you will finish it you will get some linker errors. Just add following frameworks to (http://stackoverflow.com/questions/24844766/linking-errors-when-adding-admob-to-ios-cocos2d-x-3-2):
MediaPlayer.framework
GameController.framework
Now the Android part:
Here is official instructions: https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start
Here is what I have done. I don’t know how good is this, but it works:
In AppActivity.java
you add this in onCreate
// AdMob
try {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(screenWidth, 2 * screenHeight - 75);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(“INSERT_YOUR_HASHED_DEVICE_ID_HERE”)
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
// Adding full screen container
addContentView(adView, adParams);
} catch (Exception e) {
Log.d("", "error: " + e);
}
and also define there methods:
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}