How can i integrate admob to cocos2dx version 3.17 on android, i tried using the instructions provided by google, applying them to AppActivity.java and then calling the show ad from c++, but it didn’t work and the app just crashes.
I tried using sdkbox too, but it didn’t work either I found some threads that say it is because of the new directory structure of cocos2dx 3.17, is there a non sdkbox way of doing this?
I’m fixing this issue. will be fixed today.
Thanks,
But is there a non sdkbox way, if there is could you send a link to some site that describes how it is done ? Thank you for help.
sdkbox import admob --staging
What’s up with this sdkbox mania.
Could you show your code in *.java and how do you call it? I have integrated admob with cocos without sdkbox so maybe I will be able to help.
private InterstitialAd mInterstitialAd;
void initiateAd(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
void showAd(){
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
I have added these lines to Cocos2dxActivity.java, i call the initiateAd()
method from the onCreate()
method located in Cocos2dxActivity.java
the showAd()
method i call with the following code located in HelloWorldScene.cpp in it’s onTouchBegan function which is called when the screen is touched:
bool Clicker::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event){
JniMethodInfo t;
if (JniHelper::getMethodInfo(t,
"org/cocos2dx/lib/Cocos2dxActivity",
"showAd",
"()V")) {
t.env->CallVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
log("called");
return true;
Thank you for help!
You are calling java UI method from cocos thread. You should call java methods in java UI thread:
void showAd() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
}
If your call to showAd is fine then (I guess :P) it should work now.
Thank you for this, it worked well. I only had to create a static method that calls the non static showAd()
method and then call the static method from c++. This solution really made my day and i will share it with other people so as many people as possible can benefit from it. Thank you.
No problem. Just keep in mind to call UI java code in UI java thread and it should be fine