Current location - Quotes Website - Signature design - How to integrate WeChat SDK for Android in Cocos2d-x
How to integrate WeChat SDK for Android in Cocos2d-x
How to integrate WeChat SDK for Android in Cocos2d-x

Methods/steps

1. If we can integrate WeChat SDK in Cocos2d-x and share the game with friends or friends, it will be very beneficial to the promotion of the game. Well, this article is to teach you how to integrate WeChat SDK in Cocos2d-X.

Look at the effect first and share it with your friends. Click jump to link:

Share it in a circle of friends and click the jump link:

2. Environment

Operating system: Apple OS X.

IDE: Xcode v5. 1 1 and Eclipse ADT v22.3

Android SDK:4.3

Adnroid NDK:r9d

Cocos2d-x 3.2 release

3. Creating the Cocos2d-x project is not detailed here. There are many materials on the Internet. I will do it in my own way. (1) create a Cocos2dxWechat project at the command line.

$ cocos new-p com . fusijie . cocos 2 dx we chat-l CPP-d . cocos 2 dx we chat

(2) Cross-compiling to generate so files

$ CD cocos 2 dx we chat/proj . Android/

$ ./build_native.py

(3) Import APK generated by Eclipse, and import libcocos2dx project and Cocos2dxWechat project into Eclipse. Successfully run Cocos2dxWechat.apk in the real machine. 4. Applying to create a WeChat application This step is mainly to apply to create a mobile application on the WeChat open platform and obtain the AppID. Register an account on the website of WeChat Open Platform (1), enter the management center, and click Create Mobile Application. (2) Step 1, fill in the application name, description, icon, etc. As shown below.

(3) Fill in the application official website, check the Android application, and fill in the signature and package name. As shown below.

Note: What needs special attention here is the application signature. Thirdly, after successfully running Cocos2dxWechat.apk in the real machine, we need to download a signature generation software GenSignature from the WeChat open platform.

After generating the installation signature, open the software and enter the game package name to get it.

The signature here is related to the package name and keystore of the game. If the package name is modified or the keystore is changed, the information of WeChat open platform management center must be updated synchronously to prevent invalidation. This example uses debug signatures. After completing the above work, submit it for review. It is said to be 7 days, but it is generally 1 ~ 2 days. 5. The integration of WeChat SDK is only the foundation, and here is the beginning of the theme. If you want to call WeChat Android SDK in Cocos2d-x, Jni can't run away. Here is only a demonstration of how to share with friends and friends. Wechat SDK also has many advanced functions, such as payment, collection and other interfaces. Need to refer to the document (mobile application development) for development.

Here is mainly divided into three parts to explain:

Wechat SDK download, jar package import

Android code

C++ code

(1) Download WeChat SDK

(2) Open the downloaded Android_SDK.zip. Unzip, and copy libammsdk.jar under the lib directory to the libs directory under the Cocos2dxWechat project. (3) Right-click the project and select the configuration Build Path ... In the build path, select the Libraries tab and import the libammsdk.jar file in the libs directory of the project through Add Jars. ...

(4) add member variables in AppActivity.

Private static final string APP _ ID = "wx8115d2aa55710fa"; //AppID, obtained from step 4.

Private static IWXAPI api// WeChat API interface

Private static AppActivity instance; //Class static instance, so as to facilitate the call of static functions later.

(5) Register to WeChat to open AppActivity, and call regToWX () in onCreate () to register to WeChat.

Protected void oncreate (bundle saved instancestate) (

// TODO automatically generated method stub

super . oncreate(savedInstanceState);

Example = this

regToWX();

}

Private void regToWX(){

API = wxapifactory . createwxapi(this,APP_ID,true);

API . register APP(APP _ ID);

}

(6) Because the sharing example used here passes an icon, you need an additional bitmap parsing class Util, which can be downloaded from the official WeChat SDK demonstration or in this example (the early SDK version of this class is integrated in WeChat SDK). Icons are placed in the drawable directory under the res directory. I was lazy here and used the application icon directly. (7) Share with friends

Public static void sendMsgToFriend(){

if(api.openWXApp())

{

WXWebpageObject webpage = newwxWebPageObject ();

Web webpage.webpageUrl =

Wxmediamessage msg = new wxmediamessage (web page);

msg.title = " Tittle

msg.description = " Description

bitmap thumb = bitmapfactory . decode resource(instance . get resources()、r . drawable . icon);

msg . thumb data = util . bmptobytearray(thumb,true);

SendMessageToWX。 Req req = new SendMessageToWX. req();

Req.transaction = buildtransaction ("web page");

req.message = msg

req.scene = SendMessageToWX。 Req . WXSceneSession

API . sendreq(req);

}

other

{

Toast.makeText (example, "WeChat not installed", toast. LENGTH_SHORT)。 show();

}

}

(8) Share with friends.

Public static void sendmsgtomeline () {

if(api.openWXApp())

{

if(API . getwxappsupportapi()& gt; = 0x2 102000 1)

{

WXWebpageObject webpage = newwxWebPageObject ();

Web webpage.webpageUrl =

Wxmediamessage msg = new wxmediamessage (web page);

msg.title = " Tittle

msg.description = " Description

bitmap thumb = bitmapfactory . decode resource(instance . get resources()、r . drawable . icon);

msg . thumb data = util . bmptobytearray(thumb,true);

SendMessageToWX。 Req req = new SendMessageToWX. req();

Req.transaction = buildtransaction ("web page");

req.message = msg

req.scene = SendMessageToWX。 Req . WXSceneTimeline

API . sendreq(req);

}

Otherwise {

Toast.makeText (example, "WeChat version is too low", toast. LENGTH_SHORT)。 show();

}

}

other

{

Toast.makeText (example, "WeChat not installed", toast. LENGTH_SHORT)。 show();

}

}

Where buildTransaction () is used to generate a unique session identifier.

Private static string buildTransaction (final string type)

return (type == null)? string . value of(system . current time millis()):type+system . current time millis();

}

The code is not difficult, and it is no longer explained line by line. You can refer to the SDK documentation. (9)JNI calls to create a new WeixinShare class in the Classes directory, which mainly includes two static functions:

void WeiXinShare::sendtofend()

{

# if (cc _ target _ platform = = cc _ platform _ Android)//Determine whether it is currently an Android platform.

JniMethodInfo minfo

bool isHave = JNI helper::getstatic method info(minfo," org/cocos2dx/cpp/AppActivity "," sendMsgToFriend ","()V));

If (! isHave) {

Log("jni:sendMsgToFriend is null ");

} Otherwise {

//Call this function

minfo . env-& gt; CallStaticVoidMethod(minfo . classid,minfo . method id);

}

#endif

}

void WeiXinShare::sendToTimeLine()

{

# if (cc _ target _ platform = = cc _ platform _ Android)//Determine whether it is currently an Android platform.

JniMethodInfo minfo

bool isHave = JNI helper::getstatic method info(minfo," org/cocos2dx/cpp/AppActivity "," sendMsgToTimeLine ","()V));

If (! isHave) {

Log ("JNI: sendmsgtomeline is empty");

} Otherwise {

//Call this function

minfo . env-& gt; CallStaticVoidMethod(minfo . classid,minfo . method id);

}

#endif

}

(10)Cocos2d-x is relatively simple to use, as long as it contains this WeiXinShare class, just call these two static functions.

void hello world::menuCloseCallback(Ref * PS ender)

{

# if(CC _ target _ platform = = CC _ platform _ Android)

//WeiXinShare::sendtofend();

WeiXinShare::sendToTimeLine();

#endif

}

(11) Modification of Android. MK Finally, don't forget to modify the android.mk file.

Copy code

LOCAL _ SRC _ FILES:= hello CPP/main . CPP \

../../Classes/AppDelegate.cpp \

../../Classes/HelloWorldScene.cpp \

../../Classes/WeiXinShare.cpp

(12) recompile and package it as APK. 6. Summary This article is written in detail, but it also ignores how to receive WeChat requests and return values and how to confuse them. This is an elementary course. Mainly for friends who don't know much about jni calls and the integration of third-party Android sdk. Although only part of the functional integration of WeChat sdk is explained here, the reasons are all * * *. How to integrate Weibo sharing and how to integrate third-party advertisements can be achieved in a similar way.