‹ SdkboxAds Doc Home

SdkboxAds Integration Guide

For the Lua version of cocos2d-x v3.x - (all other versions)

Prerequisites

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

Modify your Lua code to init() the plugin. This can be done anyplace, however it must be done before trying to use the plugin's features.

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(function(args)
    if "onAdAction" == args.name then
        local ad_unit_id = args.ad_unit_id;
        local ad_name = args.ad_name;
        local ad_action_type = args.ad_action_type;
    elseif "onRewardAction" ==  args.name then
        local ad_unit_id = args.ad_unit_id;
        local ad_name = args.ad_name;
        local reward_amount = args.reward_amount;
        local reward_success = args.reward_success;
    end
end)

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 a lugin's 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/luabindings/ to your projects Classes folder.

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.

Copy Files

Copy the following jar files from plugin/android/libs folder of this bundle into your project's /libs folder.

PluginSdkboxAds.jar

sdkbox.jar

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/luabindings/ to your projects Classes folder.

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

  1. Find the AppActivity.java
find . -name "AppActivity.java"
  1. Replace extends Cocos2dxActivity with extends 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

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
import android.content.Intent;
import com.sdkbox.plugin.SDKBox;
onLoadNativeLibraries();
SDKBox.init(this);
    @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)

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

Note: Proguard only works with Release builds (i.e cocos run -m release) debug builds do not invoke Proguard rules.