How to connect native Xsolla SDK for Android to your project
You can use native Xsolla SDK for Android to implement features, such as social login and returning to applications after purchase for mobile applications.
To connect the native SDK to a Cocos Creator project, follow the steps below:
- Generate a project for Android Studio.
- Connect the Xsolla SDK for Android to the project.
- Set up the project in Android Studio.
- Initialize the Xsolla SDK for Android on the Cocos Creator side.
Generate project for Android Studio
- In the Cocos Creator editor, go to
Project > Build in the main menu. If the application wasn’t built for platforms before, a window for creating a new configuration for build will open. If the editor already has build configurations, clickNew Build Task to create a new configuration. - In the
New Build Configuration window, make the following changes:
- In the
Platform field, selectAndroid from the list of available build platforms. - In the
APP ABI field, specifyarm64-v8a (recommended). If you plan to run a mobile application on an emulator, then specifyx86 . - In the
Target API Level field, specifyandroid-28 or newer.
- In the
- Click
Build .
As a result, a project for Android Studio is generated in the <CocosProjectPath>/build/android/proj
directory. Files that are used for all Android builds are generated in the <CocosProjectPath>/native/engine/android
directory.
Connect Xsolla SDK for Android to project
- Add custom Activities from Xsolla SDK to the source code. To do that:
- Copy and replace the existing files from the
<CocosProjectPath>/extensions/xsolla-commerce-sdk/native/android/Activities
folder to the<CocosProjectPath>/native/engine/android/app/src/com/cocos/game
folder. - Open the
<CocosProjectPath>/native/engine/android/app/AndroidManifest.xml
file with any text editor and add the following lines to the list of activities:
- Copy and replace the existing files from the
- kotlin
<activity
android:name="com.cocos.game.XsollaNativeAuthActivity"
android:configChanges="orientation|screenSize"
android:theme="@android:style/Theme.NoTitleBar"/>
<activity
android:name="com.cocos.game.wxapi.WXEntryActivity"
android:exported="true"
android:theme="@android:style/Theme.NoTitleBar"/>
- Add the Xsolla SDK libraries for Android as dependencies to your project’s build settings:
- In Android Studio, open your project’s
build.gradle
file. - Add the following lines to the dependencies section:
- In Android Studio, open your project’s
- kotlin
implementation 'com.xsolla.android:login:1.2.1'
implementation 'com.xsolla.android:login-facebook:1.2.1'
implementation 'com.xsolla.android:login-google:1.2.1'
implementation 'com.xsolla.android:login-wechat:1.2.1'
implementation 'com.xsolla.android:login-qq:1.2.1'
- For the changes to take effect, run the
Sync Now
command.
- For the changes to take effect, run the
Android Studio will download the corresponding libraries, and you can use its methods in the project.
Set up project in Android Studio
- In Android Studio, open your project from the
<CocosProjectPath>/build/android/proj
directory. - Add the AndroidX native libraries required for the Xsolla SDK for Android to work:
- In the
gradle.properties
file, add the following lines:
- In the
- kotlin
android.useAndroidX=true
android.enableJetifier=true
- For the changes to take effect, run the
Sync Now
command.
- For the changes to take effect, run the
Initialize Xsolla SDK for Android on the Cocos Creator side
Before you use any of the Xsolla SDK for Android features, you need to initialize the SDK. To do this, add an initialization script to the project using the IDE or by copying the file to the project directory.
In Cocos Creator, calls to native Java methods are made in TypeScript using the built-in reflection mechanism.
In the Cocos Creator project, you should call the xLoginInit
method to initialize the Xsolla SDK.
Example of initialization script:
- typescript
if(sys.platform.toLowerCase() == 'android') {
jsb.reflection.callStaticMethod("com/cocos/game/XsollaNativeAuth", "xLoginInit",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
Xsolla.settings.loginId, Xsolla.settings.clientId.toString(), "https://login.xsolla.com/api/blank",
this.facebookAppId, this.googleAppId, this.wechatAppId, this.qqAppId);
}
}
start
method of a component attached to the scene.Found a typo or other text error? Select the text and press Ctrl+Enter.