How Can I Quickly Integrate App Linking of AppGallery Connect into My Android App?

1. Creating an App and Enabling App Linking for the App

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. Creating a URL Prefix

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

3. Integrating the App Linking SDK into Your Android Project

To configure the SDK address, open your Android project, and configure the following content in red in the project-level build.gradle file.

buildscript {
repositories {
// ...
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
// ...
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
}
}

allprojects {
repositories {
// ...
maven {url 'https://developer.huawei.com/repo/'}
}
}
...
apply plugin: 'com.huawei.agconnect'
...
dependencies {
...
implementation "com.huawei.agconnect:agconnect-applinking:1.4
implementation 'com.huawei.hms:hianalytics:5.0.4.301'.1.300"
}

4. Creating a Link of App Linking

You can create a link of App Linking in AppGallery Connect or call an API in your Android project.

4.1 Creating a Link in AppGallery Connect

1. On the displayed App Linking service page, click the App Linking tab and create a link as prompted.

4.2 Creating a Link Through Coding

1. In the activity_main.xml file, configure two buttons, one for link creation and the other for sharing. Create a TextView to display the link.

<Button
android:id="@+id/create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create App Linking"
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" />

<TextView
android:id="@+id/showLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="your App Linking"
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.5" />

<Button
android:id="@+id/shareLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share App Linking"
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.8" />
private static final String DOMAIN_URI_PREFIX = "https://testapplinking1016.drcn.agconnect.link";
private static final String DEEP_LINK = " https://consumer.huawei.com/cn/";
private static final String Android_DEEP_LINK = "myapp://testapplinking/?data=1016";
private String shortLink;
findViewById(R.id.create).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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());.build());

builder.buildShortAppLinking().addOnSuccessListener(shortAppLinking -> {
shortLink = shortAppLinking.getShortUrl().toString();
TextView showAppLinking = findViewById(R.id.showLink);
showAppLinking.setText(shortLink);
}).addOnFailureListener(e -> {
Log.e("AppLinking", "Failure + "+ e.getMessage());
});
}
});
findViewById(R.id.shareLink).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, shortLink);
startActivity(intent);
}
});

5. Receiving a Link

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

private static final String DEEP_LINK = "myapp://testapplinking/?data=1016";
<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="testapplinking" android:scheme="myapp" />
</intent-filter>
AGConnectAppLinking.getInstance().getAppLinking(this).addOnSuccessListener(resolvedLinkData -> {
if (resolvedLinkData != null) {
String Result = resolvedLinkData.getDeepLink().toString();
TextView showAppLinking = findViewById(R.id.showLink);
showAppLinking.setText(Result);
}
});

6. Packaging Your App and Testing App Linking

1. After your app runs properly, tap CREATE APP LINKING to create a link. The link is displayed on the app screen.

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store