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

Mayism
5 min readDec 28, 2020

--

Recently, my app needs to use cross-platform sharing links. App Linking of AppGallery Connect just meets my requirements.If you want to quickly experience this service, see the demo on GitHub.

Perform the following steps for service integration:

i. Step 1: Create an app and enable App Linking for the app.

ii. Step 2: Create a URL prefix.

iii. Step 3: Integrate the App Linking SDK into the Android project.

iv. Step 4: Create a link of App Linking.

v. Step 5: Receive and test the link.

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.

Go to My projects > Project settings > General information, download the agconnect-services.json file, and save the file to the app directory of your Android project.

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.

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

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/'}
}
}

Open the app-level build.gradle file and configure the App Linking SDK and Analytics SDK by adding the following information in red:

...
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.

2. Configure the deep link of Android.

3. Set Link behavior for Android to Open in app.

After the link is successfully created, copy and use it.

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" />

Refer to the figure as follows.

2. Copy and add the URL prefix, and set the deep link to be opened.

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;

3. In onCreate of MainActivity, you can add a CREATE APP LINKING button to create a link.

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());
});
}
});

4. Then, you can tap SHARE APP LINKING in your app to share the created link.

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.

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

Example: DEEP_LINK = “myapp://testapplinking/?data=1016”;

private static final String DEEP_LINK = "myapp://testapplinking/?data=1016";

Sample code:

<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>

2. In the main entry of onCreate, set getAppLinking to obtain and display the link.

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.

2. Tap SHARE APP LINKING to share the link to the note. Then tap the link in the note and open the link in a browser. If the browser can directly open your app, the test is successful.

(The test process is the same for the link created in AppGallery Connect.)

For more details, please check:

https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-applinking-introduction

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 developers can write code in apps to create links, which is very convenient.

Reference:

HUAWEI AppGallery Connect App Linking documentation: https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-applinking-introduction

--

--

No responses yet