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. Your app will allow you to:

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, with buttons to create and share a link of App Linking.

<?xml version="1.0" encoding="utf-8"?> <?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" /> <Button android:id="@+id/create" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Create App Linking" android:textAllCaps="false" android:textSize="20sp" 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.2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Long App Linking:" android:textSize="18sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.3" /> <TextView android:id="@+id/longLinkText" android:layout_width="match_parent" android:layout_height="80dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.4" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Short App Linking:" android:textSize="18sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" /> <TextView android:id="@+id/shortLinkText" android:layout_width="match_parent" android:layout_height="60dp" 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.6" /> <Button android:id="@+id/shareShort" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Share short App Linking" android:textAllCaps="false" android:textSize="20sp" 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.7" /> <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.75" /> <TextView android:id="@+id/result_text" android:layout_width="match_parent" android:layout_height="40dp" android:textSize="15dp" android:hint="Result" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.85" /> </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.

When creating links of App Linking in your app, you need to specify the URL prefix and deep link. In this codelab, the URL prefix and deep link have been configured.
Open the MainActivity file under app/src/main/java/PACKAGE_NAME of your Android Studio project, and configure the following parameters in Class:

private static final String DOMAIN_URI_PREFIX = "https://codelabhard.drcn.agconnect.link"; private static final String DEEP_LINK = "https://developer.huawei.com/consumer/cn"; private static final String ANDROID_DEEP_LINK = "codelabhard://applinking.codelab/testid=123"; private static String App_LinkingUrl;
  1. Open the MainActivity file under app/src/main/java/PACKAGE_NAME of your Android Studio project, and import the following classes:
    import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.huawei.agconnect.applinking.AGConnectAppLinking; import com.huawei.agconnect.applinking.AppLinking;
  2. Define UI elements in the onCreate method, which includes two buttons for creating and sharing links of App Linking.
    // create Button findViewById(R.id.create).setOnClickListener(view -> { createAppLinking(); }); // share Button findViewById(R.id.shareShort).setOnClickListener(view -> { shareLink(App_LinkingUrl); });
  3. Call the API for creating a link of App Linking.
    private void createAppLinking() { AppLinking.Builder builder = new AppLinking.Builder() .setUriPrefix(DOMAIN_URI_PREFIX) .setDeepLink(Uri.parse(DEEP_LINK)) .setAndroidLinkInfo(new AppLinking.AndroidLinkInfo.Builder() .setAndroidDeepLink(ANDROID_DEEP_LINK) .build()); TextView longTextView = findViewById(R.id.longLinkText); longTextView.setText(builder.buildAppLinking().getUri().toString()); builder.buildShortAppLinking().addOnSuccessListener(shortAppLinking -> { Log.i("AppLinkingCodeLab", "buildShortAppLinking success"); TextView shortTextView = findViewById(R.id.shortLinkText); App_LinkingUrl = shortAppLinking.getShortUrl().toString(); shortTextView.setText(App_LinkingUrl); }).addOnFailureListener(e -> { Log.i("AppLinkingCodeLab", "buildShortAppLinking failed: " + e.toString()); }); }
  4. Share the created link of App Linking.
    private void shareLink(String LinkingUrl) { if (LinkingUrl != null) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, LinkingUrl); startActivity(intent); } }
  1. Open the AndroidManifest file under app/src/main of your Android Studio project, and add an intent filter in to receive the link of App Linking.
    <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- Add the custom scheme of deep link. --> <data android:host="applinking.codelab" android:scheme="codelabhard" /> </intent-filter> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- Add the URL prefix of App Linking. --> <data android:host="photoplaza.drcn.agconnect.link" android:scheme="http"/> <data android:host="photoplaza.drcn.agconnect.link" android:scheme="https"/> </intent-filter>
  2. Configure the signing certificate in Android Studio.
  3. Open the app-level build.gradle file, and configure the signing information for your project.
    signingConfigs { release { storeFile file('HmsDemo.jks') keyAlias 'hmsdemo' keyPassword '123456' storePassword '123456' v1SigningEnabled true v2SigningEnabled true } } buildTypes { release { signingConfig signingConfigs.release minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } debug { signingConfig signingConfigs.release debuggable true } }
  4. Run the keytool -list -v -keystore <keystore-file> command, perform operations as prompted, and obtain the SHA-256 fingerprint. indicates the absolute path to the app's signing certificate.
  5. In AppGallery Connect, go to Project settings > General information. In the App information area, click Add next to SHA-256 certificate fingerprint, and enter the obtained SHA-256 certificate fingerprint. Click Save.
  6. Open the MainActivity file under app/src/main/java/PACKAGE_NAME of your Android Studio project, and add the following method for receiving links of App Linking in onCreate.
    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. Tap the Create App Linking button to generate a long link and a short link.

  3. Tap Share short App Linking button and copy the generated short link to Notepad.
  4. Tap the short link in the notepad and check whether your app is launched and whether the deep link is displayed in your app.

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

Code copied