SdkboxAds Integration Guide
For the C++ version of cocos2d-x v3.x - (all other versions)
Prerequisites
- None
Integration
Open a terminal and use the following command to install the SDKBOX SdkboxAds plugin. Make sure you setup the SDKBOX installer correctly.
$ sdkbox import sdkboxadsplus
JSON Configuration
SDKBOX Installer will automatically create a sample configuration sdkbox_config.json
for you
Here is an example of the SdkboxAds configuration:
"SdkboxAds": {
"units": [
"AdColony",
"Fyber",
"Chartboost",
"Vungle"
],
"placements": [
{
"id" : "placement-1",
"strategy" : "round-robin",
"units" : [
{
"unit": "AdColony",
"name": "video"
},
{
"unit": "Chartboost",
"name": "Default"
}
]
},
{
"id" : "placement-2",
"strategy" : "round-robin",
"units" : [
{
"unit": "Vungle",
"name": "reward"
},
{
"unit": "AdColony",
"name": "v4vc"
},
{
"unit": "Chartboost",
"name": "Next Level"
}
]
}
]
}
Usage
Initialize SdkboxAds
Initialize the plugin in your code. We recommend to do it in the AppDelegate::applicationDidFinishLaunching()
or AppController:didFinishLaunchingWithOptions()
. Make sure to include the appropriate headers:
#include "PluginSdkboxAds/PluginSdkboxAds.h"
AppDelegate::applicationDidFinishLaunching()
{
sdkbox::PluginSdkboxAds::init();
}
Using SdkboxAds
Configuration
SdkboxAds is an ad mediation service. It manages a set of predefined AdUnits at runtime. The configuration includes two parts: AdUnits, and Placements.
- Each AdUnit entry references to a Sdkbox plugin that can be managed as an AdUnit.
- A Placement is a collection of pairs of AdUnit and ad name ordered by a strategy.
Please read the (SdkboxAds Configuration Overview) for more details.
Usage
A call to sdkbox::SdkboxAds::init()
will load and instantiate all referenced AdUnits in the configuration
file.
To request a default Ad for the default AdUnit and have a fast integration test:
sdkbox::PluginSdkboxAds::playAd()
To request Ads in the Default AdUnit:
sdkbox::PluginSdkboxAds::playAd( const std::string& zone_place_location );
sdkbox::PluginSdkboxAds::playAd( const std::string& zone_place_location, const AdUnitParams& params );
// AdUnitParams is a typedef for std::map<std::string,std::string>
Note: the params are defined per AdUnit, and should be checked in each AdUnit's documentation.
To request Ads for an specific AdUnit:
sdkbox::PluginSdkboxAds::playAd(
const std::string& ad_unit_name,
const std::string& zone_place_location );
sdkbox::PluginSdkboxAds::playAd(
const std::string& ad_unit_name,
const std::string& zone_place_location,
const AdUnitParams& params );
To request ads for a placement defined in the sdkbox_config.json file:
sdkbox::PluginSdkboxAds::placement( const str::string& placement );
To have fine grained cache control (for AdUnits that expose it):
sdkbox::PluginSdkboxAds::cacheControl(
const std::string& ad_unit,
const std::map<std::string, std::string>& cacheOpts );
Cache options are AdUnit specific and must be checked in each AdUnit's documentation.
SdkboxAds events
This allows you to catch SdkboxAds
' events.
- Allow your class to extend
sdkbox::SdkboxAdsListener
and override the functions listed:
#include "PluginSdkboxAds/PluginSdkboxAds.h"
class MyClass : public sdkbox::SdkboxAdsListener
{
private:
void onAdAction(
const std::string& ad_unit_id,
const std::string& zone_location_place_you_name_it,
sdkbox::AdActionType action_type);
void onRewardAction(
const std::string& ad_unit_id,
const std::string& zone_id,
float reward_amount,
bool reward_succeed);
};
sdkbox::AdActionType
is an enum with these values:
enum AdActionType {
LOADED=0, // content loaded
LOAD_FAILED, // content failed to load
CLICKED, // clicked on content
REWARD_STARTED, // reward started
REWARD_ENDED, // reward achieved
REWARD_CANCELED, // reward aborted
AD_STARTED, // start showing.
AD_CANCELED, // start showing.
AD_ENDED, // content shown
ADACTIONTYPE_UNKNOWN // mostly on error situations.
};
- Create a listener that handles callbacks:
sdkbox::PluginSdkboxAds::setListener( new MyClass() );
API Reference
Methods
static void init ( ) ;
Initialize the plugin instance. The plugin initializes from the sdkbox_config.json file and reads configuration of the form:
"SdkboxAds": { "units": [ "AdColony", "Fyber" ], "placements": [ {} ] }
The "units" array references other plugins' configuration. Sdkboxads mediates between other plugins and/or simplifies interaction with them. The "placements" block will be of the form:{ "id": string, // placement's id "strategy" : "round-robin", // only value by now. "units": [
and each UnitDefinition as:] } { "Unit": string, // result_of_AdUnit.getId(), "name": string, // a zone, place, or location name from AdUnit's plugin config, "params": json_object }
For a sample Sdkboxads config, check the example at Sdkbox github public repository. The "params" configuration block will allow to pass in specific information to play ads like location, position, etc. Check each AdUnit's documentation to find specifics on its configuration.
static void setListener ( sdkbox::PluginSdkboxAdsListener * listener ) ;
Set Sdkboxads' plugin listener. This listener will expose for each registered AdUnits, events related to Ads and Rewards. In SdkboxAds, an Ad refers generally to VIDEO, INTERSTITIAL and BANNER. Note that some ad units may have only Ads.
static PluginSdkboxAdsListener * getListener ( ) ;
Retrieve plugin's listener.
static void playAd ( const std::string & ad_unit ,
const std::string & zone_place_location ,
const AdUnitParams & params ) ;
Play an Ad identified by its zone/location/place with optional parameters. AdUnits like Fyber which don't have zones, will use common placeholders like "INTERSTITIAL" or "REWARDED". Some AdUnits may require extra information to play an Ad, and should use the params for that purpose. You should refer to the documentation of each specific AdUnit about what parameters will accept.
The ad will be played for a specific AdUnit based on its identifier. The identifiers are the values in the "units" node of the sdkbox_config.json file. For example: "AdColony" or "Fyber".
static void playAd ( const std::string & ad_unit ,
const std::string & zone_place_location ) ;
static void playAd ( const std::string & , const AdUnitParams & params ) ;
Like the 3 parameter playAd method, this one plays an Ad for the default AdUnit. Currently the default AdUnit is the first defined one in the sdkbox_config.json file.
static void playAd ( const std::string & ) ;
static void playAd ( ) ;
Play a default Ad with the default AdUnit. Each AdUnit knows how to play an Ad by default. For example, AdColony will play the first video zone, or the first Reward if there's no one.
static void placement ( const std::string & placement ) ;
A placement is a collection of mediated AdUnits. When you want to invoke a placement, just call this method. If the placement does not exist, the call will just be ignored. A placement will take care of AdUnit’s cache control, so if the current AdUnit has no cached content, or the AdUnit fails to load an ad, the next adUnit will be used.
The placement will cycle throughout all the AdUnits it references, in a round robin fashion. In the short term, new placement strategies will be added.
static void cacheControl ( const std::string & ad_unit ,
const std::map <std::string ,
std::string> & cacheOpts ) ;
Manage cache control policies. Not all AdUnits expose cache control while some others expose fine-grained cache control. For example Chartboost offers specific cache control for each location, as well as general Ads cache control.
This method interfaces with the AdUnit’s cache mechanism. If no cache control is exposed
for a given AdUnit, the call will silently be ignored.
Each AdUnit will document what valid values to pass to the cacheOpts parameter.
E.g. for Chartboost, these are valid values:
cacheOpts for Chartboost:
element : bool
Element corresponds to an identifiable CBLocation (ChartBoost)
e.g:
{
"Default": true, // a configuration location
"Level Complete": true, // a configuration location
"ADS": true // a general placeholder chartboost specific
}
static void cache ( const std::string & ad_unit ,
const std::string & ad_name ) ;
Cache AdUnits' ad Example: sdkbox::PluginSdkboxAds::cache("AdMob", "reward")
static bool isAvailable ( const std::string & placement ) ;
check if placement available
static void hide ( const std::string & placement ) ;
hide placement
static void hideAd ( const std::string & ad_unit ,
const std::string & zone_place_location ) ;
Listeners
Manual Integration
If the SDKBOX Installer fails to complete successfully, it is possible to integrate SDKBOX manually. If the installer complete successfully, please do not complete anymore of this document. It is not necessary.
These steps are listed last in this document on purpose as they are seldom needed. If you find yourself using these steps, please, after completing, double back and re-read the steps above for other integration items.
Manual Integration For iOS
Drag and drop the following frameworks from the plugins/ios folder of
the SdkboxAds
bundle into your Xcode project, check Copy items if needed
when adding frameworks:
sdkbox.framework
PluginSdkboxAds.framework
The above frameworks depend upon a large number of other frameworks. You also need to add the following system frameworks, if you don't already have them:
Security.framework
AdSupport.framework
Also, all frameworks needed by each AdUnit you will be adding to SdkboxAds.
Manual Integration For Android
SDKBOX supports three different kinds of Android projects command-line, eclipse and Android Studio.
proj.android
will be used as our<project_root>
for command-line and eclipse projectproj.android-studio
will be used as our<project_root>
for Android Studio project.
Copy Files
Copy the following jar files from plugin/android/libs
folder of this
bundle into your project's
PluginSdkboxAds.jar
sdkbox.jar
-
If you're using cocos2d-x from source copy the jar files to:
Android command-line:
cocos2d/cocos/platform/android/java/libs
Android Studio:
cocos2d/cocos/platform/android/java/libs cocos2d/cocos/platform/android/libcocos2dx/libs
-
If you're using cocos2d-js or lua copy the jar files to:
Android command-line:
frameworks/cocos2d-x/cocos/platform/android/java/libs
Android Studio:
frameworks/cocos2d-x/cocos/platform/android/java/libs frameworks/cocos2d-x/cocos/platform/android/libcocos2dx/libs
-
If you're using prebuilt cocos2d-x copy the jar files to:
Android command-line:
proj.android/libs
Copy the sdkboxads_lib
directories from plugin/android/libs
to your <project_root>/libs/
directory.
Copy jni libs
Copy and overwrite all the folders from plugin/android/jni
to your <project_root>/jni/
directory.
Edit AndroidManifest.xml
Include the following permissions above the application tag:
<uses-permission android:name="android.permission.INTERNET" />
Edit Android.mk
Edit <project_root>/jni/Android.mk
to:
Add additional requirements to LOCAL_WHOLE_STATIC_LIBRARIES:
LOCAL_WHOLE_STATIC_LIBRARIES += PluginSdkboxAds
LOCAL_WHOLE_STATIC_LIBRARIES += sdkbox
Add a call to:
$(call import-add-path,$(LOCAL_PATH))
before any import-module statements.
Add additional import-module statements at the end:
$(call import-module, ./sdkbox)
$(call import-module, ./pluginsdkboxads)
This means that your ordering should look similar to this:
$(call import-add-path,$(LOCAL_PATH))
$(call import-module, ./sdkbox)
$(call import-module, ./pluginsdkboxads)
Note: It is important to make sure these statements are above the existing $(call import-module,./prebuilt-mk)
statement, if you are using the pre-built libraries.
Modify Application.mk
(Cocos2d-x v3.0 to v3.2 only)
Edit <project_root>/jni/Application.mk
to make sure APP_STL is defined
correctly. If Application.mk
contains APP_STL := c++_static
, it should be
changed to:
APP_STL := gnustl_static
Important
For each AdUnit, you have to add:
JNI:
All AdUnit's JNI library contents.
Manifest:
All changes needed by each AdUnit's plugin.
jar:
Jar files necessary for included AdUnits. Basically all jar files present in the Plugin's lib folder.
LOCAL_WHOLE_STATIC_LIBRARIES the module references for each of them, e.g.:
LOCAL_WHOLE_STATIC_LIBRARIES += pluginvungle
LOCAL_WHOLE_STATIC_LIBRARIES += pluginadcolony
Android.mk import-module:
$(call import-module, ./pluginadcolony)
$(call import-module, ./pluginvungle)
Modify AppActivity.java
Plugin >= 2.4.0.3
- Find the AppActivity.java
find . -name "AppActivity.java"
- Replace
extends Cocos2dxActivity
withextends com.sdkbox.plugin.SDKBoxActivity
Example of the directory where the AppActivity.java file is located:
cpp
- proj.android/src/org/cocos2dx/cpp/AppActivity.java
- proj.android-studio/app/src/org/cocos2dx/cpp/AppActivity.java
- proj.android/app/src/org/cocos2dx/cpp/AppActivity.java ( from cocos2d-x 3.17)
lua
- frameworks/runtime-src/proj.android/src/org/cocos2dx/lua/AppActivity.java
- frameworks/runtime-src/proj.android-studio/app/src/org/cocos2dx/lua/AppActivity.java
- frameworks/runtime-src/proj.android/app/src/org/cocos2dx/lua/AppActivity.java (from cocos2d-x 3.17)
js
- frameworks/runtime-src/proj.android/src/org/cocos2dx/javascript/AppActivity.java
- frameworks/runtime-src/proj.android/app/src/org/cocos2dx/javascript/AppActivity.java ( from cocos2d-x 3.17)
Plugin < 2.4.0.3
-
If you're using cocos2d-x from source, assuming you are in the proj.android directory, Cocos2dxActivity.java is located:
../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/ lib/Cocos2dxActivity.java
-
If you're using the prebuilt cocos2d-x libraries assuming you are in the
proj.android
directory, Cocos2dxActivity.java is located:./src/org/cocos2dx/lib/Cocos2dxActivity.java
Note: When using Cocos2d-x from source, different versions have Cocos2dxActivity.java in a different location. One way to find the location is to look in proj.android/project.properties. Example:
android.library.reference.1=../../cocos2d-x/cocos/platform/android/java
In this case, Cocos2dxActivity.java should be located at:
../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
- Modify Cocos2dxActivity.java to add the following imports:
import android.content.Intent;
import com.sdkbox.plugin.SDKBox;
- Second, modify Cocos2dxActivity.java to edit the
onCreate(final Bundle savedInstanceState)
function to add a call toSDKBox.init(this);
. The placement of this call is important. It must be done after the call toonLoadNativeLibraries();
. Example:
onLoadNativeLibraries();
SDKBox.init(this);
-
Last, we need to insert the proper overrides code. There are a few rules here.
-
If the method listed has not been defined, add it.
-
If the method listed has been defined, add the calls to
SDKBox
in the same existing function.
-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!SDKBox.onActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onStart() {
super.onStart();
SDKBox.onStart();
}
@Override
protected void onStop() {
super.onStop();
SDKBox.onStop();
}
@Override
protected void onResume() {
super.onResume();
SDKBox.onResume();
}
@Override
protected void onPause() {
super.onPause();
SDKBox.onPause();
}
@Override
public void onBackPressed() {
if(!SDKBox.onBackPressed()) {
super.onBackPressed();
}
}
Proguard (optional)
- Edit
project.properties
to specify aProguard
configuration file. Example:
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
- Edit the file you specified to include each AdUnit's proguard configuration.
Note: Proguard only works with Release builds (i.e cocos run -m release
) debug builds do not invoke Proguard rules.