Overview

The App Linking service allows you to create links that work across multiple platforms including Android, iOS, and web. You can send promotion information to users with links of App Linking, or users can share links of App Linking dynamically generated in your app. Anyone who receives a link can tap it to access the linked content.
When a user taps a link of App Linking:

What You Will Create

In this codelab, you will build an Android app that integrates App Linking, and create a link of App Linking in AppGallery Connect, through which your app will be launched.

What You Will Learn

Development Environment and Skill Requirements

Device Requirements

To integrate App Linking, you must complete the following preparations:

For details, please refer to Integration Preparations.

Enabling App Linking

  1. Sign in to AppGallery Connect, click My projects, click your project card, and select your app from the drop-down list on the top. On the page that is displayed, go to Grow > App Linking. If it is the first time that you use the service, click Use now.
  2. If the data processing location is not set, set it for your app.

Integrating SDKs

If you are using Android Studio, you can integrate SDKs by using the Maven repository into your Android Studio project before development.

  1. Click My projects in AppGallery Connect, click your project card, and select your app for which you want to enable App Linking from the drop-down list on the top.
  2. Go to Project settings > General information, and click agconnect-services.json to download it.
  3. Copy the agconnect-services.json file to your app's module directory.
  4. Open the app-level build.gradle file in Android Studio and configure the App Linking SDK. To collect statistics on App Linking events, you need to enable HUAWEI Analytics and integrate the Analytics SDK.
    // Configure the following address: apply plugin: 'com.huawei.agconnect' dependencies { // Configure the following addresses: implementation 'com.huawei.agconnect:agconnect-applinking:1.6.2.300' implementation 'com.huawei.hms:hianalytics:6.3.2.300' }
  5. Click Sync Now to synchronize the configurations.

In this codelab, you can create a page in your Android Studio project and design the UI according to the following figure. On the page, a link of App Linking can be received and displayed.

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Codelab" android:textSize="24dp" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="getLinkingResult:" android:textSize="20sp" android:textAllCaps="false" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.3" /> <TextView android:id="@+id/result_text" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="15dp" android:hint="Result" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.4" /> </androidx.constraintlayout.widget.ConstraintLayout>
  1. In AppGallery Connect, click My projects.
  2. Click your project card and select an app from the app drop-down list on the top.
  3. Go to Grow > App Linking. Click the URL prefixes tab and click New URL prefix. In the Set domain name area, enter the URL prefix used in this codelab.
  4. Click Next. The system automatically checks whether the URL prefix is available.
  1. On the App Linking page, click the App Linking tab, and click New link.
  2. In the Set short link area, use the default short link and click Next.
  3. In the Set deep link area, configure the following parameters:
    1. Link name: deep link name. In this demo, enter AppLinking-easy.
    2. Default deep link: HTML5 address, which can be accessed on a PC. Set it to https://developer.huawei.com/consumer/cn here.
    3. Android deep link: redirection link that can be received on an Android device. Set it to codelabeasy://applinking.codelab/testid=123 here. Ignore the optional parameters and click Next.
  4. In the Set link behavior area, set Link behavior for Android to Open in app, select your app, and set Redirect a user not installing the app to to App details page on HUAWEI AppGallery.
  5. Ignore the optional parameters and click Release in the upper right corner.
  1. Open the AndroidManifest file under app/src/main, and add an intent filter in <activity> to receive deep link domain name.
    <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="applinking.codelab" android:scheme="codelabeasy" /> </intent-filter>
  2. Open the MainActivity file under app/src/main/java/PACKAGE_NAME, and add the following code snippet to receive and display the deep link.
    AGConnectAppLinking.getInstance().getAppLinking(this).addOnSuccessListener(resolvedLinkData -> { Uri deepLink = null; if (resolvedLinkData!= null) { deepLink = resolvedLinkData.getDeepLink(); Log.i("AppLinkingCodeLab", "Open From App Linking: " + deepLink.toString()); TextView resultText = findViewById(R.id.result_text); resultText.setText(deepLink.toString()); // Perform the following processing code for the deep link. } });
  1. Run your Android Studio project and generate the APK. Then, install the APK on your mobile phone for testing.
  2. In AppGallery Connect, go to the App Linking page of your project, find the created link, and click the QR code icon next to it.
  3. Scan the QR code using the browser of your device and do as follows to launch your app.

Well done. You have successfully built an app that integrates App Linking of AppGallery Connect and learned how to:

Code copied