The App Linking service allows links that work across platforms even on devices where your app is not installed. You can use these links to direct users to promotional information or native app content that they can share with others. You can create links of App Linking and send them to users, 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 Preparations for Integration in AppGallery Connect.

Enabling App Linking

  1. Sign in to AppGallery Connect, click My projects, and click your app for which you need to enable App Linking. On the page that is displayed, go to Develop > Growing > App Linking. If you use App Linking for the first time, click Enable now.
  2. If the data storage location is not set, you need to set it for your app. For details, please refer to Setting a Data Storage Location.

Integrating the App Linking SDK

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

  1. Sign in to AppGallery Connect, click My projects, and click your app for which you need to enable App Linking.
  2. Go to Project Setting > Generalinformation, and click agconnect-services.json to download the configuration file.
  3. Copy the agconnect-services.json file to the app's root directory.
  4. Open the build.gradle file in the app directory and configure the App Linking SDK. To collect statistics on App Linking events, you need to use HUAWEI Analytics Kit, configure the Huawei Analytics SDK synchronously.
    // Configure the following address: apply plugin: 'com.huawei.agconnect' dependencies { // Configure the following address: implementation ' com.huawei.agconnect:agconnect-applinking:' implementation ' com.huawei.hms:hianalytics:' }
  5. In the app-level build.gradle file, set the Java compatibility mode to JDK 1.8.
    compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 }
  6. Click Sync Now to synchronize the configuration.

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 links of App Linking.

  1. Sign in to AppGallery Connect, click My projects, and click your app for which you need to enable App Linking. On the page that is displayed, go to Growing > App Linking.
  2. Click the URL prefix tab and click Add URL prefix. In the Set domain name area, enter a URL prefix used in this codelab.
  3. Click Next. The system automatically checks whether the URL prefix is available.
  4. After the verification is successful, click Back to return to the URL prefix management page.

When creating links of App Linking on the app client, you need to specify the URL prefix and deep link. In this codelab, the URL prefix and deep link have been configured.

private TextView shortTextView; private TextView longTextView; private static final String DOMAIN_URI_PREFIX = "https://applinkingtest.drcn.agconnect.link"; private static final String DEEP_LINK = "https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides";
  1. Import related classes to MainActivity.
    import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.huawei.agconnect.applinking.AppLinking; import com.huawei.agconnect.applinking.AGConnectAppLinking;
  2. Define UI elements in the onCreate method.
    TextView deeplinktext = findViewById(R.id.deepLink); deeplinktext.setText(DEEP_LINK); shortTextView = findViewById(R.id.shortLinkText); longTextView = findViewById(R.id.longLinkText); //creatButton findViewById(R.id.create).setOnClickListener(view -> { createAppLinking(); }); //shareButton findViewById(R.id.shareShort).setOnClickListener(view -> { shareLink((String) shortTextView.getText()); }); findViewById(R.id.shareLong).setOnClickListener(view -> { shareLink((String) longTextView.getText()); });
  3. Initialize an App Linking object instance.
  4. Create links of App Linking.
    private void createAppLinking() { AppLinking.Builder builder = new AppLinking.Builder().setUriPrefix(DOMAIN_URI_PREFIX) .setDeepLink(Uri.parse(DEEP_LINK)); longTextView.setText(builder.buildAppLinking().getUri().toString()); builder.buildShortAppLinking(ShortAppLinking.LENGTH.SHORT).addOnSuccessListener(shortAppLinking -> { shortTextView.setText(shortAppLinking.getShortUrl().toString()); }).addOnFailureListener(e -> { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); }); }
  5. Share the created links of App Linking.
    private void shareLink(String agcLink) { if (agcLink != null) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, agcLink); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }
  1. Create an AdsActivity to display pages opened after links of App Linking are received. In the activity_ads.xml file, add the TestView layout for displaying passed deep links.
    <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" 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.4" />
  2. Open the AndroidManifest file and add the Intent filter that receives the deep link domain name to AdsActivity.
    <activity android:name=".AdsActivity" android:label="Ads_Page">> <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="developer.huawei.com" android:scheme="https" /> </intent-filter> </activity>
  3. In AdsActivity, receive links of App Linking and display deep links of App Linking.
    AGConnectAppLinking.getInstance().getAppLinking(this,getIntent()).addOnSuccessListener(resolvedLinkData -> { Uri deepLink = null; if (resolvedLinkData != null) { deepLink = resolvedLinkData.getDeepLink(); TextView textView = findViewById(R.id.textView); textView.setText(deepLink.toString()); } }).addOnFailureListener(e -> { Log.i("AppLinking", "getAppLinking:onFailure", e); });
  1. Run the Android Studio project to generate an APK and install the APK on the test mobile phone.
  2. Check whether the deep link on the initialization page is the same as the configured address.
  3. Click Create App Linking. The created long link and short link are displayed below.
  4. Click Share long App Linking to share the created long link to ShareApp that is installed in advance. Check whether Ads_Page can be opened using a long link in ShareApp.
  5. Click Share short App Linking to share the created short link to ShareApp that is installed in advance. Check whether Ads_Page can be opened using a short link in ShareApp.

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

For details about the development guide of App Linking, please refer to App Linking Service Introduction.
For details about APIs related to App Linking of AppGallery Connect, please refer to API Reference.
Download the demo source code used in this codelab from the following address:

Download source code

Code copied