[Huawei Developers] Mayism:
How Can I Quickly Integrate App Linking of AppGallery Connect into an Unity-based App?

Recently, my app needs to use cross-platform sharing links. App Linking of AppGallery Connect just meets my requirements.

Perform the following steps for service integration:

Step 1: Create an app, enable App Linking, and create a URL prefix.

Step 2: Integrate the App Linking SDK into a Unity project.

Step 3: Import the HuaweiService package to Unity Editor.

Step 4: Create a link of App Linking.

Step 5: Receive and test the link.

1. Enable App Linking and create a URL prefix.

1. Create an app or use an existing app in AppGallery Connect. Click My projects and go to Grow > App Linking, and click Enable now in the displayed page.

2. On the displayed App Linking service page, click the URL prefix tab and then click Add URL prefix to create a unique URL prefix.

The system will automatically check your domain name to ensure its uniqueness.

2. Integrate the App Linking SDK into a Unity project.

Prepare the Android development environment in Unity Editor.

1. Choose Edit > Project Settings > Player > Publish Settings. In the Build area, select items for Android as required.

Note: You need to add build dependencies for Android in Unity Hub, as shown in the following figure.

2. In AppGallery Connect, click My projects, and find your project from the list. Go to Project settings > General information, download the agconnect-services.json file, and save the file to the Assets\Plugins\Android directory of your Unity project.

3. In Unity Editor, choose Edit > Project Settings > Player > Other Settings, and set the package name to the one you set in AppGallery Connect.

4. Add the following code to the project-level baseProjectTmeplate.gradle file in the Assets\Plugins\Android directory:

allprojects {
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}

5. Add the following code to the app-level LauncherTmeplate.gradle file in the Assets\Plugins\Android directory:

dependencies {
implementation 'com.huawei.agconnect:agconnect-applinking:1.4.1.300'
}

3. Import the HuaweiService package to Unity Editor.

1. Import the source package to your project. You can either download the package from the Unity Asset Store, or download it from the following website:

https://share.unity.com/receive/?th...e=CHA6BzFizOIgjWvhSXYRaJiNzOcwfItJRY7dZSNEb4A

2. After the download is complete, choose Assets > Import package, select the required package, and click Import.

4. Create a link of App Linking.

You can create a link of App Linking in AppGallery Connect or by calling an API in your Unity project. Let’s see how to create a link in your Unity project.

1. Create buttons.

In Unity Editor, choose GameObject > UI > Button, and create two buttons called create and share. Click Add Component on the right to create a script file.

2. Add a method for creating a link.

Add the following method to the script file of the test app:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HuaweiService.AppLinking;
using HuaweiService;

public class testApp : MonoBehaviour
{
private const string URI_PREFIX = "https://testcocos1203.drcn.agconnect.link";
private const string DEEP_LINK = "https://developer.huawei.com/consumer/cn/service/josp/agc/index.html";

void Start()
{
}

void Update()
{

}

public void createLongLink() {
AGConnectAppLinking.getInstance();
AppLinking.Builder builder = createBuilder();
Uri applinkingUri = builder.buildAppLinking().getUri();
var link = applinkingUri.toString();
Debug.Log ("AppLinking create" + link);
GUIUtility.systemCopyBuffer = link;
}

public AppLinking.Builder createBuilder(){
return AppLinking.newBuilder()
.setUriPrefix(URI_PREFIX)
.setDeepLink(Uri.parse(DEEP_LINK))
.setAndroidLinkInfo(
AppLinking.AndroidLinkInfo.newBuilder()
.build());
}

public void OpenLink()
{
var activity = new Activity();
var intent = new Intent();
intent.setData(Uri.parse(GUIUtility.systemCopyBuffer));
activity.startActivity(intent);
}
}

3. Bind a method to a button.

In Unity Editor, click the create button. In the On Click() area on the right, add a tap event, as shown in the following figures.

Bind a method to the share button as well.

Note: You can drag the .cs file created by clicking create to the Add Component area on the right to add a tap event.

5. Receive a link.

To receive a link, you need to configure the manifest file and set the getAppLinking method.

1、 Configure the manifest file. Here, the scheme of the deep link domain name is set in the file.

Example: DEEP_LINK = ‘https://developer.huawei.com/cn/';

Sample code:

6. Package your app and test your link.

1. After your app runs properly, tap create to create a link. Then tap share to open the link in a browser.

2. If the app can be launched, the link is normal.

7. Summary

The integration is simple. The SDK is small, and links can be shared across Android and iOS. No adaptation is required on different platforms, reducing the workload.

Operations personnel can create links in AppGallery Connect for app promotion, and you can write code in apps to create links, which is very convenient.

Reference:

HUAWEI AppGallery Connect App Linking documentation: https://developer.huawei.com/consum...ry-connect-Guides/agc-applinking-introduction

Unity documentation about App Linking: https://docs.unity.cn/cn/Packages-c...linking.html#integrating-huawei-analytics-kit

Unity Editor Installation and APK Packaging: https://forums.developer.huawei.com/forumPortal/en/topic/0204435788744370088?fid=0101187876626530001

if you have any questions，please refer to stackoverflow .