SdkboxAds Integration Guide
For the Javascript 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
Register Javascript Functions
You need to register all the SdkboxAds JS functions with cocos2d-x before using them.
To do this:
* Modify ./frameworks/runtime-src/Classes/AppDelegate.cpp
to include the following headers:
#include "PluginSdkboxAdsJS.hpp"
#include "PluginSdkboxAdsJSHelper.h"
- Modify
./frameworks/runtime-src/Classes/AppDelegate.cpp
make sure to call:
sc->addRegisterCallback(register_all_PluginSdkboxAdsJS);
sc->addRegisterCallback(register_all_PluginSdkboxAdsJS_helper);
Initialize SdkboxAds
Initialize the plugin by calling init()
where appropriate in your code. We
recommend to do this in the app.js
. Example:
sdkbox.PluginSdkboxAds.init();
Using SdkboxAds
To request a default Ad for the default AdUnit and have a fast integration test:
sdkbox.PluginSdkboxAds.play()
To request Ads in the Default AdUnit:
sdkbox.PluginSdkboxAds.play( zone_place_location, params );
// params is an object with string keys and values.
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.play( ad_unit_name, zone_place_location, params );
To request ads for a placement defined in the sdkbox_config.json file:
sdkbox.PluginSdkboxAds.placement( placement_name );
To have fine grained cache control (for AdUnits that expose it):
sdkbox.PluginSdkboxAds.cacheControl( ad_unit, cacheOpts );
// cacheOpts is an object with keys and values as strings.
Cache options are AdUnit specific and must be checked in each AdUnit's documentation.
SdkboxAds events
This allows you to catch SdkboxAds
events.
sdkbox.PluginSdkboxAds.setListener({
onAdAction : function( ad_unit_id, zone_location_place_you_name_it, action_type),
onRewardAction : function( ad_unit_id, zone_id, reward_amount, reward_succeed )
});
refer to the cpp documentation for action_type values.
API Reference
Methods
sdkbox.PluginSdkboxAds.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" : "placement_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 from AdUnit's lugin 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.
sdkbox.PluginSdkboxAds.setListener(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.
sdkbox.PluginSdkboxAds.placement(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.
sdkbox.PluginSdkboxAds.cache(ad_unit, ad_name);
Cache AdUnits' ad Example: sdkbox::PluginSdkboxAds::cache("AdMob", "reward")
sdkbox.PluginSdkboxAds.isAvailable(placement);
check if placement available
sdkbox.PluginSdkboxAds.hide(placement);
hide placement
sdkbox.PluginSdkboxAds.hideAd(ad_unit, 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.
Copy all source and header files from plugin/jsbindings/
to your projects Classes
folder.
NOTE: plugin/jsbindings/jsb2
for creator 1.7.
Add these same files, that you just copied, to Xcode by either dragging and dropping them into Xcode or by using File -> Add files to....
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)
Copy all source and header files from plugin/jsbindings/
to your projects Classes
folder.
NOTE: plugin/jsbindings/jsb2
for creator 1.7.
Add all .cpp
files, that you just copied, to Android.mk
in the LOCAL_SRC_FILES section. Example
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp \
../../Classes/NewSourceFile.cpp
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.