This topic describes how to use Android Studio to integrate GameShield SDK for Android.
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
- Open Android Studio.
- 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.
Note Before you perform the next steps, you must ensure that the new project is running
normally.
- Add a JAR package.
- Copy the yunceng.jar file from the Android SDK package to the libs directory of the project.
- Open Android Studio, choose , click app, and then click the Dependencies tab.
- Click the icon, select jar dependency, and then add the yunceng.jar file.
- Click OK.
- Add an SO file. Go to the 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.
- 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"/>
- 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;
}
- 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: