Jump to: navigation, search

Quick Start Guide

Revision as of 17:45, 22 October 2014 by Huangyanli (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Quick Start Guide

This article will walk you through the integration workflow of the AnySDK Framework .

Obtain AnySDK Framework

To integrate the AnySDK Framework, you need to first register at the AnySDK website. After registering, download it at http://en.anysdk.com/docs/Download. After decompressing, you can see the directory structure as shown below: (two STL lib frameworks of gnustl_static and stlport_static respectively)

CppTutorial1.jpg

After AnySDK Framework is downloaded, you can see its resource directory as below:

CppTutorial2.jpg

Import AnySDK Framework

For a sample of the code used to import AnySDK to Cocos2d-x, please refer to this link - https://github.com/shujunqiao/anysdk-cpp-sample

Select A Version of AnySDK Framework And Import It into the Project Directory

1.Check the version of the stl lib of the game project

Developers can find stl lib type setting at the first line of the `jni/application.mk` file under `project` directory.

If it is set as APP_STL := gnustl_static, it means that the current project has imported and used stl standard lib as gnu static lib and we should select to integrate the framework resources under protocols_gnustl_static folder; if not, we should select to integrate the framework resources under protocols_stlport_static folder.
2.Create a `protocols` directory under the android project directory and copy both the `include` and `android` folders to this folder. Exactly as shown below:
CppTutorial3.jpg
3.Merge all the files in the protocols/res/ folder into the res/ folder.

Modify the framework compile options of Android.mk file configuration

Modify the framework compile options of Android.mk file configuration
1.Add the protocols directory to your NDK_MODULE_PATH environment variables: below LOCAL_PATH := $(call my-dir), the first line of android.mk, add
LOCAL_PATH := $(call my-dir)
$(call import-add-path,$(LOCAL_PATH)/../)
2. Add static lib statement of AnySDK framework: under the LOCAL_C_INCLUDES statement of android.mk, add:
LOCAL_WHOLE_STATIC_LIBRARIES := PluginProtocolStatic

Note: pay attention to the syntax rules here; if other LOCAL_WHOLE_STATIC_LIBRARIES statement already exists in the mk file, we need to add to it and change “:=” to “+=”

3. Add library path:below the last line of android.mk add: $(call import-module,protocols/android)
$(call import-module,protocols/android)

Import the jar Package that Comes with the Framework and Check the Export Option

Steps: right click your project, select Properties and then Java Build Path. Click Libraries on the panel and import libPluginProtocol.jar into the game project through Add JARs…as shown below:


CppTutorial4.jpg         CppTutorial5.jpg

Note: game project API supports a minimum of 10

Configure the Needed Authorities to Add Framework to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

Most projects would register and apply for these authorizations even when excluding AnySDK Framework.

Initialize AnySDK Framework

Initialize JavaVM

First, we need to set JavaVM quote for AnySDK framework when the game project is loading jni. Then we need to find JNI_OnLoad function, which would be called firstly when jni being loaded.
Using Cocos2d-x 3.0 as example, JNI_OnLoad function definition is in javaactivity.cpp under platform/android, shown as below:

CppTutorial6.jpg CppTutorial7.jpg

1.Import head file and state namespace
#include "PluginJniHelper.h"
using namespace anysdk::framework ;

Note: when importing head file, make sure you stick to the head file definition path set by the project so that corresponding head file can be found when compiling.

2.2.Add javaVM setting code
PluginJniHelper::setJavaVM(vm); // add for plugin
  • If other JavaVM initializing codes, by other engines, already exist leave the code and add: PluginJniHelper::setJavaVM(vm); after and you are done.

Using Cocos2d-x 3.2 as example

#include "AppDelegate.h"
#include "cocos2d.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>
#include "PluginJniHelper.h"

#define  LOG_TAG    "main"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

using namespace cocos2d;
using namespace anysdk::framework;

void cocos_android_app_init (JNIEnv* env, jobject thiz) {
    LOGD("cocos_android_app_init");
    AppDelegate *pAppDelegate = new AppDelegate();
}

extern "C"
{

jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    JniHelper::setJavaVM(vm);
    PluginJniHelper::setJavaVM(vm);

    return JNI_VERSION_1_4;
}
}

Initialize AnySDK Framework at Java Level

1.First find the main Activity of the game project; using Cocos2d-x as example, the main Activity is the Activity that has adopted cocos2dxActivity.
2.Then in the onCreate() method of the main Activity, intialize AnySDK Framework by adding the code below:


import com.anysdk.framework.PluginWrapper;

public class MainActivity extends Activity{
    protected void onCreate(Bundle savedState)
        {
            super.onCreate(savedState);
            PluginWrapper.init(this); // for plugins
        }
3.Rewrite Activity Lifecycle related methods with code as shown below:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
	super.onActivityResult(requestCode, resultCode, data);
	PluginWrapper.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onResume() {
    super.onResume();
    PluginWrapper.onResume();
}
@Override
public void onPause(){
    PluginWrapper.onPause();
    super.onPause();
}
@Override
protected void onNewIntent(Intent intent) {
    PluginWrapper.onNewIntent(intent);
    super.onNewIntent(intent);
}

Note: you no longer need to implement the life circle method of activity manually after integrating cocos2dxActivity with Cocos2d-x 3.0 or above versions, and if you find that the main Activity does not have these methods, you need to rewrite(or just copy the code above).

Intialize AnySDK Framework at C++ Level

You need to call init function to initialize the framewrok first whenever you are calling any AnySDK Framework functions. We recommend you to indicate the intializing framework at C++ level after intializing the java level with code below:
std::string appKey = "BC26F841-OOOO-OOOO-OOOO-OOOOOOOOOOOO";
std::string appSecret = "1dff378a8f254ecOOOOOOOOOOOOO";
std::string privateKey = "696064B29E9A0OOOOOOOOOOOOO";
std::string oauthLoginServer = "http://oauth.anysdk.com/api/OauthLoginDemo/Login.php";

AgentManager::getInstance()->init(appKey,appSecret,privateKey,oauthLoginServer);
Note: appKey, appSecret and privateKey are the only three parameters generated after the packing tool client finishes creating the game. You can view them at the packing tool game management interface, shown as below:

GameParam.png


The oauthLoginServer parameter is the API address provided by the game service and is the only address requested by the framework when logging in under sim sdk test mode(namely when running the mother package directly).When the channel package is ready officially, it will be replaced by the address parameter configured in the packing tool with the corresponding channel.

Loading and Uninstalling SDK Plug-ins

1. Load all integrated SDKs after initializing framework with code as

AgentManager::getInstance()->loadALLPlugin();//Initialize plug-ins, including SDKs.

Note: as initializing some SDKs might involve SDK flash interface related operation, it's strongly recommended to call loading plug-in operation after AnySDK Framework initialization is finished.

import com.anysdk.framework.PluginWrapper;

public class MainActivity extends Activity{
    protected void onCreate(Bundle savedState){
    super.onCreate(savedState);
    PluginWrapper.init(this); // for plugins
    wrapper.nativeInitPlugins();
}
void Java_com_anysdk_sample_wrapper_nativeInitPlugins(JNIEnv* env, jobject thiz)
{
       AgentManager::getInstance()->loadALLPlugin();
}
2.Unistall Plug-ins

You can use the code below to unistall unessary plug-ins:

AgentManager::getInstance()->unloadALLPlugin();

Proguard

If you want to encrypt the Java code with ProGuard, please do it without ProGuard’ing the classes of the Jar package. You can add the classes below to proguard configuration which will exclude them:

-keep class com.anysdk.framework.** {*;}
-keep class com.anysdk.Util.SdkHttpListener {*;}