筛选项

Android Instant Apps

概述

At the 2017 Google IO, Google launched the public version of Instant Apps for developers to adopt and build on. Instant Apps are a way to load a partial section (or split as Google refers to them), without the user having to visit the Play Store and install your app. In effect, it's instant access to your native app. If you have been using a website as a fallback when your full Android app is not installed, an Instant App is an alternative. You'll have to choose between mobile web and your Instant App on Android so make sure you plan out your integration!

Branch 使得在 Instant App 中开始使用基本的 Branch Link 变得异常容易,如下所示。此页面上的指南将引导您完成整个配置过程,以在新的 Instant App 中启用 Branch。

📘

将 Android 应用配置为 Instant App

This guide is not about how to configure your Android app to be an Instant App. Please see Google's documentation for detailed instructions on how to do this. Come back to this guide when you're ready to enable Branch.

为什么要在您的 Instant App 中使用 Branch

如果您遵循 Google 指南,他们会让您将自己的网络链接集成为 Android App Link,这将触发 Instant App。而这时 Branch 能为您做什么?首先,让我为您解释 Branch 如何与 Instant App 一起使用。除您自己的 web domain 之外,Branch 还将充当您要注册 Android App Link 的 web domain,或作为 Manifest(清单)中的唯一域。我们充当您的链接。

这里列出了 Branch Link 和 Instant App 的潜在用例:

  1. Branch will host personalized web links for you, since we provision a custom domain (yourapp.app.link or a domain of your choosing registered in the Branch Dashboard) for every app. If you don't have a website, this means that we can take care of all your Android App Links needs, automatically configuring and hosting the assetlinks.json file on your behalf.
  2. 如果您有一个受欢迎的网站并配置了 Android App Link,则可能不希望100%的 Web 链接触发您的 Instant App。您的 Instant App 将仅支持应用全功能的一小部分。您可以配置 Instant App,让其仅在Branch Link domain 和路径上触发,然后通过 Branch 的使用方法来控制用户何时链接到 Instant App。
  3. Branch can measure and attribute clicksinstalls and custom conversion events back to the Branch tracking link for all visits to your Instant App, which are visible on the dashboard.
  4. 您可以使用 Branch Link 将深度链接从 Android Instant App 推迟到完整的 Android 应用。您可能会构建一个功能,将用户从 Instant App 推送到完整的 Android 应用,Branch 可以在此过程中发挥其作用。
  5. Branch can measure and attribute clicksinstalls and custom conversion events inside your full native Android App for users who were referred from an Instant App, showing the conversion from click -> Instant App -> Full App on the dashboard.

设置

为您的 Instant App 集成 Branch

So you're convinced. Let's get started! If at any time, you need a real example, you can check out a full demo application on our Github. We've replicated our original Android demo application and modified it to support Android Instant Apps.

初始化 Branch SDK

If you plan on deep linking from your Android Instant App to your full Android app after its installed, you'll be utilizing the Play Install Referrer Library, which is bundled with the SDK. The referrer library returns deeplinking information asynchronously, which may impact your startup time. For that purpose, the SDK provides the method, setPlayStoreReferrerCheckTimeout, which defines how long the Branch initialization will wait for the Google Play Referrer, the default timeout is 1.5 seconds. If you want to set a different timeout, make sure you call this method before SDK initialization as shown below.

在同一部分,将代码片段替换为:

public class MyApplication extends Application {
  ...
  @Override
  public void onCreate() {
    super.onCreate();
    // This tells the Branch initialization how long to wait for the Google Play Referrer before proceeding. (Default: 1.5 second)
    Branch.setPlayStoreReferrerCheckTimeout(1000L);
  
    // Initialize the Branch SDK
    Branch.getAutoInstance(this);
  }
  ...
}

In Section https://help.branch.io/developers-hub/docs/android-instant-apps#add-your-branch-keys-and-register-for-install-referrer replace code snippet with:

<application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:supportsRtl="true"
        android:name=".MyApplication">

  <meta-data android:name="io.branch.sdk.TestMode" android:value="false" /> <!-- Set to true to use Branch_Test_Key -->
  <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_my_live_key" />
  <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="key_test_my_test_key" />

</application>
public void onCreate() {
  super.onCreate();
  // Branch's InstallListener is needed to deferred deep link from an Android Instant App to a full app
  // This tells the Branch initialization how long to wait for the Google Play Referrer before proceeding. (Default: 1 second)
  Branch.setPlayStoreReferrerCheckTimeout(1000L);

  // Initialize the Branch SDK
  Branch.getAutoInstance(this);
}

添加您的 Branch key 并注册 Install Referrer

Instant Apps can be rather confusing as there are many different manifests, but you want to find the Manifest that contains your application tags. Make sure your Application class name is defined here, and then specify the Branch keys inside the application element.

<application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:supportsRtl="true"
        android:name=".MyApplication">

  <meta-data android:name="io.branch.sdk.TestMode" android:value="false" /> <!-- Set to true to use Branch_Test_Key -->
  <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_my_live_key" />
  <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="key_test_my_test_key" />

  <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
    <intent-filter>
       <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
  </receiver>
</application>

将 Branch Link 配置为 Android App Link

本指南默认您已经为 Android App Link 配置了 Branch。

Now, you simply need to edit the above manifest and paste in the following snippet inside the desired activity. Then you'll need to replace the xxxx with your own custom subdomain which will be visible on the Branch link settings dashboard at the bottom of the page. If you're using a custom subdomain, you can find the advanced instructions in the above link regarding configuring Android App Links.

<application
  ......>

<activity...
  <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" />
      <data android:scheme="https" android:host="xxxx.app.link" />
      <data android:scheme="https" android:host="xxxx-alternate.app.link" />
  </intent-filter>
</activity>

</application>

Branch Link 上的自定义路径

Optionally, when you create shortened Branch links, you can specify a alias parameter (described here) in the creation flow to define a custom path (e.g. https://example.app.link/product/id12345 or any custom strings). This can be helpful if you are managing multiple Instant App paths.

获得 Branch 深度链接数据

Add Branch sessionBuilder()...init() in Activities which are configured to open from a link click in order to receive the deep link params.

protected void onStart() {
    super.onStart();
    Branch.sessionBuilder(this).withCallback(new Branch.BranchReferralInitListener() {
        @Override
        public void onInitFinished(JSONObject referringParams, BranchError error) {
            Log.d(""Branch"", ""onInitFinished() with deep link data: "" + referringParams);
        }
    }).init();
}

配置从 Instant App 到 Full App 的深度链接

Now, the user has arrived in your Instant App and you're ready to convert them to install your full native app. Don't worry, Branch as got your covered! We have overridden the default showInstallPrompt with a method that auto configures the Google Play prompt with all of the deep link data you need to carry context through install. Additionally, we can provide you the full set of attribution on how many users convert through this prompt.

Branch SDK 提供了方便的方法来检查应用类型和完整的应用转化。这消除了对 Google IA 支持 SDK('com.google.android.instantapp')的依赖。这里有一些使生活变得轻松的方法

  • Branch#isInstantApp()

此便捷方法会检查当前运行的应用版本是 Instant App 还是 Full Android App,以便为您提供方便

  • Branch#showInstallPrompt()

此方法显示 full Android app 的安装提示,使您可以轻松地通过安装过程将 Branch 引用深度数据传递给 full app。与 Branch 通常使用延迟深度链接类似,完整的应用将在句柄回调中接收深度链接参数。

下例显示了如何创建自定义 Branch Universal Object,并将其与安装提示相关联,该提示将在用户安装后传递到完整的本机 Android 应用。

if (Branch.isInstantApp(this)) {
  myFullAppInstallButton.setVisibility(View.VISIBLE);
  myFullAppInstallButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
       BranchUniversalObject branchUniversalObject = new BranchUniversalObject()
            .setCanonicalIdentifier("item/12345")
            .setTitle("My Content Title")
            .setContentDescription("My Content Description")
            .setContentImageUrl("https://example.com/mycontent-12345.png")
            .addContentMetadata("property1", "blue")
            .addContentMetadata("property2", "red");

      Branch.showInstallPrompt(myActivity, activity_ret_code, branchUniversalObject);
    }
  });
} else {
  myFullAppInstallButton.setVisibility(View.GONE);
}

问题排查

测试 Instant App

You can create Branch links in a variety of ways, but to quickly test your Branch integration, head to the quick links section on our dashboard and create a link. If you configured deep linking via a specific key in the deep link data, make sure that you add it to your quick link to properly simulate a real live Branch link.