Integration Steps for Cocos2d-x Android using C++

 

Integrate Chocolate Platform SDK into your Cocos2dx project using C++

 

Chocolate Platform SDK Cocos2dx plugin supports the following ad units:

• Full Screen Interstitial

• Rewarded Video

 

Integration Steps

The easiest way is by cloning our working sample: https://github.com/chocolateplatform/cocos2dx-cpp-android-sample

 

Step 1:

git clone https://github.com/chocolateplatform/cocos2dx-cpp-android-sample

cocos2dx-cpp-android-sample/proj.android-studio  is the Android Studio project folder that was created/exported using the Cocos2dx tools.  We are assuming you are familiar with Cocos2dx environment and C++.

 

Briefly examine the following github folder and return to this documentation page:

https://github.com/chocolateplatform/cocos2dx-cpp-android-sample/tree/master/Classes

 

VdopiaAdNativeAPI.h – Notice our bridge header that declares our interstitial and rewarded methods.

We place a copy of VdopiaAdNative.h to your local cocos2dx-cpp-android-sample/Classes  folder.

 

Briefly examine the following github folder and return to this documentation page:

https://github.com/chocolateplatform/cocos2dx-cpp-android-sample/tree/master/proj.android-studio/app/jni/hellocpp

Based on your requirements, you only need to use ONE of the following:

VdopiaJNI_Interstitial_Reward.cpp – Implementation of BOTH interstitial and rewarded ads.

• VdopiaJNI_Interstitial.cpp – Implementation of ONLY interstitial ads.

• VdopiaJNI_Rewarded.cpp – Implementation of ONLY rewarded ads.

We placed a copy of these files to cocos2dx-cpp-android-sample/proj.android-studio/app/jni  folder.

We EDIT cocos2dx-cpp-android-sample/proj.android-studio/app/jni/Android.mk and include the cpp file you need:

LOCAL_SRC_FILES := hellocpp/main.cpp \
hellocpp/VdopiaJNI_Interstitial.cpp\
../../../Classes/AppDelegate.cpp \
../../../Classes/HelloWorldScene.cpp

 

Step 2:

Open this folder:

https://github.com/chocolateplatform/cocos2dx-cpp-android-sample/tree/master/Classes

HelloWorldScene.cpp – simple screen that shows how to initialize Chocolate ads and show them, including how to prefetch.  We will edit

HelloWorldScene.h and HelloWorldScene.cpp.

The following link explains what callbacks to declare and implement in HelloWorldScene.h/.cpp

https://github.com/chocolateplatform/cocos2dx-cpp-android-sample/blob/master/proj.android-studio/app/jni/hellocpp/readme.txt

 

Show Interstitial Ads

Please refer to HelloWorldScene.cpp:

loadInterstitialAd("API KEY");

When interstitial ad is ready or failed:

void HelloWorld::interstitialAdLoaded() {
  CCLOG("interstitialAdLoaded");
  this->setButtonVisible(true, ShowInterestialAdButtonTag);
  this->setButtonVisible(false, LoadInterestialAdButtonTag);
}

void HelloWorld::interstitialAdFailed() {
  CCLOG("interstitialAdFailed");
  this->setButtonVisible(false, ShowInterestialAdButtonTag);
  this->setButtonVisible(true, LoadInterestialAdButtonTag);
}

To show the interstitial ad that is now ready:

showInterstitialAd();

 

 Show Rewarded Ads
loadRewardedAd("API KEY");

When rewarded ad is ready or failed:

void HelloWorld::rewardedAdLoaded() {
  CCLOG("rewardedAdLoaded");
  this->setButtonVisible(true, ShowRewardedAdButtonTag);
  this->setButtonVisible(false, LoadRewardedAdButtonTag);
}

void HelloWorld::rewardedAdFailed() {
  CCLOG("rewardedAdFailed");
  this->setButtonVisible(false, ShowRewardedAdButtonTag);
  this->setButtonVisible(true, LoadRewardedAdButtonTag);
}

To show the rewarded ad that is now ready:

showRewardedAd();
Prefetch

You can prefetch interstitial and/or rewarded ads very easily:

 

prefetchInterstitialAd("API KEY");

prefetchRewardedAd("API KEY");

A recommended place to prefetch is right after you make the call to show an ad:

showRewardedAd();
//prefetch next ad, while user watching ad.  Your next load will fetch quickly, if prefetch is successful.
prefetchRewardedAd("API KEY");

 

Step 3:

Please make sure your Android Studio is setup with NDK, which allows Android Studio to build C/C++ and integrate those functionalities into the Android project.  As you know, Android applications are fundamentally written in Java or Kotlin.

Now, look at build.gradle and AndroidManifest.xml

build.gradle is the Android equivalent to the makefile in C++.  Just much easier and more powerful than makefile.

AndroidManifest.xml is the application manifest which contains definitions, declarations, and permissions.

These files are already setup per the steps outlined in our Android project setup document:

https://chocolateplatform.com/android/?app_dev_framework=android

 

There is a small section where it talks about including our Cocos2dx plugin.

 

That’s it.  You can build and run the new Android app to a physical device.   If you run on emulator, make sure the backed cpu is armeabi-7a.

 

How to turn on ads on your ad unit

Please send us your sample app with the integrated code. We will help you validate the SDK setup and turn on the demand.

You can contact us via email: sdk-support@chocolateplatform.com