App Services(HMS)
Introduction
This package enables parts of Hms Core service in Unity for Android Build, which is implemented by wrapping Hms SDK. Name and usage of corresponding classes and functions are the same. You can write code by referring Hms SDK document. But still there are some differences during development. We highly recommend downloading example project as a reference.
There are 4 Kits included.
- Ads Kit: reference and param
- Rewarded Ads
- Interstitial Ads
- Push Kit: reference and param
- all
- Location Kit: reference and param
- all
- Analytics Kit: reference and param
- all
How to Integrating HMS into Your Unity Project
1. Before doing this step, you need to create HMS Account, project and App according to the HMS document.
Now you have your Huawei HMS Project and App information.
2. Create Unity project, and we will also need to setup build environment for building Android apk with HMS SDK. After finish the following preparation.
Editor -> Build Settings -> Switch platform to Andriod and Connect your Android device
Player Settings -> Publishing Settings -> Enable the following environment checkboxs
3. According to HMS integration process introduction, we still need to add some configurations to the gradle files for development preparations.
You can follow Huawei documentation guidance to do the configuration, you can also refer and compare with the files in our Unity example project.
3.1 Enable and add configurations to AndroidManifest.xml
Edit -> Project Settings -> Player -> Android(icon) -> Publishing Settings -> Build
enable `Custom Main Manifest`
We should enable AndroidManifest when we use push or location sdk.
If Unity Version is 2019.2 or before, there is not AndroidManifest checkbox, but you can put AndroidManifest.xml
under Assets/Plugins/Android
.
<?xml version="1.0" encoding="utf-8"?>
<!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<application>
<activity android:name="com.hms.hms_analytic_activity.HmsAnalyticActivity"
android:theme="@style/UnityThemeSelector">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<service
android:name="com.unity.hms.push.MyPushService"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
</intent-filter>
</service>
<receiver
android:name="com.unity.hms.location.LocationBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.huawei.hmssample.location.LocationBroadcastReceiver.ACTION_PROCESS_LOCATION" />
</intent-filter>
</receiver>
<receiver
android:name="com.unity.hms.location.GeoFenceBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION" />
</intent-filter>
</receiver>
</application>
</manifest>
3.2 Enable and add configurations to project gradle
Edit -> Project Settings -> Player -> Android(icon) -> Publishing Settings -> Build
enable `Custom Base Gradle Template` and add the AppGallery Connect plugin and the Maven repository
If unity version is 2019.2 or before, you should add implement and other configuration on MainGradleTemplate
.
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
allprojects {
buildscript {
repositories {**ARTIFACTORYREPOSITORY**
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
// If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
// See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
// See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
// To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
**BUILD_SCRIPT_DEPS**
}
}
repositories {**ARTIFACTORYREPOSITORY**
google()
jcenter()
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
maven { url 'https://developer.huawei.com/repo/' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
3.3 Enable and add configurations to app gradle
Edit -> Project Settings -> Player -> Android(icon) -> Publishing Settings -> Build
enable `Custom Launcher Gradle Template`and add build dependencies
If unity version is 2019.2 or before, you should add implement and other configuration on MainGradleTemplate
.
dependencies {
implementation project(':unityLibrary')
implementation 'com.huawei.hms:ads-lite:13.4.29.303'
implementation 'com.huawei.hms:ads-consent:3.4.30.301'
implementation 'com.huawei.hms:push:4.0.3.301'
implementation 'com.huawei.hms:location:5.0.0.302'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.huawei.hms:hianalytics:5.0.0.301'
implementation 'com.huawei.agconnect:agconnect-core:1.2.0.300'
implementation 'com.huawei.hms:base:4.0.1.300'
implementation 'com.huawei.hms:hwid:4.0.1.300'
implementation 'com.huawei.hms:game:4.0.1.300'
}
3.4 Enable and add configurations to unity library gradle
Edit -> Project Settings -> Player -> Android(icon) -> Publishing Settings -> Build
enable `Custom Main Gradle Template`and add build dependencies
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.huawei.hms:hianalytics:5.0.0.301'
implementation 'com.huawei.agconnect:agconnect-core:1.2.0.300'
implementation 'com.huawei.hms:base:4.0.1.300'
implementation 'com.huawei.hms:hwid:4.0.1.300'
implementation 'com.huawei.hms:game:4.0.1.300'
**DEPS**}
3.5 Signature
Edit -> Project Settings -> Player -> Android(icon) -> Publishing Settings -> Keystore Manager
Keytore... -> create new
We should enter password when you open unity, otherwise We cannot build.
We donnot need add signature on gradle
3.6 Signing Certificate Fingerprint
Refer to Huawei preparation documentation step 4 for generating a SHA256 Certificate Fingerprint.
Refer to Huawei preparation documentation step 5 to add Fingerprint to AppGallery Connect.
3.7 Package name
Edit -> Project Settings -> Player
package name is `com.${Company Name}.${Product Name}`
You can also complete the rest of the settings here, such as version number, icons,resolution, etc.
3.8 Agconnect-services.json
We should put json under `Assets/Plugins/Android`
Add this following message into the json file that download from your Huawei developer dashboard, also, you can refer to that file in our Unity example project.
"agcgw":{
"backurl":"connect-drcn.dbankcloud.cn",
"url":"connect-drcn.hispace.hicloud.com"
},
We must generate agconnect-services.json if we use analytic, push or location sdk.
Reference Link to create agconnect-services.json
3.9 Get activity
For getting the activity, you can use the following code.
Common.GetActivity()
4. SDK
There are corresponding example scene in example project for all 4 Kits. For test, you need to build it onto Android mobile build by HMS. Make sure you have created HMS account and project. Then, you can change the configuration and test different functions.
4.1 Ads
In example project, corresponding scene is Assets/HuaweiHmsDemo/Scenes/HmsAdsSampleScene.unity
and code is Assets/HuaweiHmsDemo/demo/test/ads/AdsTest.cs
.
4.2 Analytic
In example project, corresponding scene is Assets/HuaweiHmsDemo/Scenes/HmsAnalyticSampleScene.unity
and code is Assets/HuaweiHmsDemo/demo/test/AnalyticTest.cs
.
Analytic initialize must initialize on onCreate
of the first activity .
You can extend the UnityPlayerActivity file, and put java file on Plugins/Android
.
public class HmsAnalyticActivity extends UnityPlayerActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HiAnalyticsTools.enableLog();//analytic initialize
HiAnalytics.getInstance(this);
}
}
Then you should set activity of AndroidManifest.xml
<application>
<activity android:name="com.hms.hms_analytic_activity.HmsAnalyticActivity"
android:theme="@style/UnityThemeSelector">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
...
</application>
if you initialize analytic, you should add analytic implentment on mainTemplate.gradle
4.3 Push
In example project, corresponding scene is Assets/HuaweiHmsDemo/Scenes/HmsPushSampleScene.unity
and code is Assets/HuaweiHmsDemo/demo/test/PushTest.cs
.
We must set service.
- Implement interface
IPushServiceListener
- Get Receiver by method
PushListenerRegister.RegisterListener
- add to AndroidManifest.xml
<application> ... <service android:name="com.unity.hms.push.MyPushService" android:exported="false"> <intent-filter> <action android:name="com.huawei.push.action.MESSAGING_EVENT"/> </intent-filter> </service> ... </application>
android:name
is fixed.
4.4 Location
We should add implentment com.android.support:appcompat-v7:28.0.0
to launcherTemplate
In example project, corresponding scene is Assets/HuaweiHmsDemo/Scenes/HmsLocationSampleScene.unity
and code is Assets/HuaweiHmsDemo/demo/test/location/LocationTest.cs
.
We must set receiver.
LocationBroadcastReceiver
- Implement interface
IBroadcastReceiver
- Get Receiver by method
BroadcastRegister.CreateLocationReceiver
- add to AndroidManifest.xml
<application> ... <receiver android:name="com.unity.hms.location.LocationBroadcastReceiver" android:exported="true"> <intent-filter> <action android:name=`${Your action name}` /> </intent-filter> </receiver> ... </application>
android:name
is fixed.
You can set value of action android:name
. But the value should be the same as the parameter of intent.setAction
.
GeoFenceReceiver
- Implement interface
IBroadcastReceiver
- Get Receiver by method
BroadcastRegister.CreateGeoFenceReceiver
- add to AndroidManifest.xml
<application> ... <receiver android:name="com.unity.hms.location.GeoFenceBroadcastReceiver" android:exported="true"> <intent-filter> <action android:name=`${Your action name}` /> </intent-filter> </receiver> ... </application>
android:name
is fixed.
You can set value of action android:name
. But the value should be the same as the parameter of intent.setAction
.
5. Test & Release
Refer to Huawei integration procedure for testing and releasing.