This topic describes how to use Android Studio to integrate GameShield SDK for Android.

Prerequisites

The SDK package and AccessKey for Android is obtained from the GameShield console. The AccessKey is required when you set the appkey parameter. For more information, see Obtain an SDK package and AccessKey pair. Obtain the SDK package and AccessKey

Background information

A GameShield SDK includes two import methods: initEx and getProxyTcpDomain. For more information, see Introduction to core methods.

If you want to obtain a demo, contact after-sales technical support of GameShield.

Procedure

  1. Open Android Studio.
  2. Create a project and use the default settings to complete the creation of the project. In this example, the project is named yxd_sdk_test.
    The following figure shows the structure of the project directory. Directory of the new project
    Note Before you perform the next steps, you must ensure that the new project is running normally.
    The new project is running normally
  3. Add a JAR package.
    1. Copy the yunceng.jar file from the Android SDK package to the libs directory of the project. Copy yunceng.jar
    2. Open Android Studio, choose File > Project Structure, click app, and then click the Dependencies tab. dependencies
    3. Click the Add icon, select jar dependency, and then add the yunceng.jar file. Add yunceng.jar
    4. Click OK.
  4. Add an SO file. Go to the src > main directory and create a directory named jniLibs in the main directory. Then, copy the SO file from the Android SDK package to the jniLibs directory. Copy libyunceng.so
  5. Configure access permissions. Open the AndroidManifest.xml file and add the following code to the file, as shown in the following figure.
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    Access permissions
  6. Initialize the SDK.
    Note You need to complete the initialization of the SDK only once.
    Before you run the following code, you must replace the following variables:
    • Replace <yourAppKey> with the AccessKey that you obtained in the GameShield console.
    • Replace <token> with the unique ID of a player in your game. If the ID is unavailable, you can use the default value. You cannot leave <token> unspecified.
    public int sdk_init() {
        String appkey = "<yourAppKey>";
        String token = "<token>";
        int ret = YunCeng.initEx(appkey, game);
        if (ret == 0) {
            Log.d("yuncengsdk", "sdk init success");
        } else {
            Log.e("yuncengsdk", "sdk init failed, ret " + ret);
        }
        return ret;
    }
  7. Optional:Configure ProGuard.
    If you use ProGuard to implement obfuscation, you must add the following code to the ProGuard configuration file.
    -keep class com.aliyun.security.yunceng.** {*;}

What to do next

After you add your application, you can use the SDK to retrieve the IP address and the port that are mapped by GameShield for your application. The method that GameShield uses to map an IP address and a port for your application varies based on your service type. For details about the methods, see the following topics: