instant apps_Android Instant Apps 101:它们是什么以及它们如何工作

instant apps

by Tomislav Smrečki

通过TomislavSmrečki

Android Instant Apps are a cool new way to consume native apps without prior installation. Only parts of the app are downloaded and launched, giving the users a native look and feel in a couple of seconds.

Android Instant Apps是无需预先安装即可使用本机应用程序的一种很酷的新方法。 仅下载并启动了该应用程序的一部分,从而在几秒钟内为用户提供了本机外观。

它们如何工作? (How do they work?)

First of all, don’t confuse them with Progressive Web Apps where a launcher icon opens a web app via the Chrome browser. An Instant app will actually be installed on your phone, but without the need to search for it on the Play Store.

首先,不要将它们与渐进式Web应用程序混淆,在该应用程序中,启动器图标会通过Chrome浏览器打开网络应用程序。 Instant应用程序实际上将安装在您的手机上,但无需在Play商店中进行搜索。

Web URLs will trigger the Google Play Store on your phone and fetch only the part of the app that is associated with the requested URL. The rest of the app is not downloaded. This way users can quickly enjoy the native experience of your Android application.

网络URL将触发手机上的Google Play商店,并且仅提取与请求的URL关联的应用程序部分。 该应用程序的其余部分未下载。 这样,用户可以快速享受Android应用程序的本机体验。

背景是什么? (What’s the background?)

Well, you need to divide your Android project into a couple of modules. One of them is a base module with the essential code which is used in all other modules (API connection, database, shared preferences etc.). The other, feature modules, contain specific functionalities and activities which can be accessed via associated URLs.

好吧,您需要将Android项目划分为几个模块。 其中之一是具有必要代码的基本模块,该基本代码可在所有其他模块(API连接,数据库,共享首选项等)中使用。 其他功能模块包含可以通过关联的URL访问的特定功能和活动。

Let’s say you have a web app with a list of products and a single page of the product. For example, you can link https://example.domain/products to launch the ProductsListActivity and https://example.domain/products/12 to launch the ProductActivity.

假设您有一个Web应用程序,其中包含产品列表和产品的单个页面。 例如,您可以链接https://example.domain/products以启动ProductsListActivity和https://example.domain/products/12以启动ProductActivity。

To make them accessible as instant app activities, they need to be packed into individual feature modules and they need to have associated App Links defined in their module manifests. We will call them Product and Product list modules.

为了使它们可以作为即时应用程序活动进行访问,需要将它们打包到各个功能模块中,并且需要在其模块清单中定义关联的应用程序链接。 我们将它们称为“产品”和“产品列表”模块。

Now, when a user tries to open https://example.domain/products/12, both Product and Base modules will start to download and the ProductActivity will be launched.

现在,当用户尝试打开https://example.domain/products/12时 Product和Base模块都将开始下载,并且ProductActivity将启动。

You’ve probably heard of deep links. They are defined in the app manifest, and they will be registered to the OS. When a user tries to open such a link, the OS will ask the user to choose between opening the link in a web browser or in your app. However, this is not enough for Instant apps, you need to go one step further — App Links. You need to include the autoVerify=”true” property.

您可能听说过深层链接。 它们在应用清单中定义,并且将被注册到操作系统。 当用户尝试打开此类链接时,操作系统将要求用户在打开Web浏览器或应用程序中的链接之间进行选择。 但是,这对于Instant应用程序来说还不够,您需要再走一步-App Links 。 您需要包括autoVerify =“ true”属性。

<activity android:name=".ProductActivity"> <intent-filter android:autoVerify="true" android:order="100">
<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"      android:host="example.domain"       android:pathPrefix="/products" /> <data android:scheme="https"/>
</intent-filter> </activity>

Your app will verify if the links you specified are really associated with your domain. For this, you need to include the assetlinks.json file into the following folder of your domain root:

您的应用将验证您指定的链接是否确实与您的域相关联。 为此,您需要将assetlinks.json文件包含到域根目录的以下文件夹中:

https://example.domain/.well-known/assetlinks.json.

https://example.domain/.well-known/assetlinks.json。

Also, notice the android:order=”100″ property. This is actually a priority in this case. If you have a product list and a product single that correspond to the same path (/products and /products/10), the product single activity will be launched if there’s an id after the /products path. If not, then the product list activity is launched.

另外,请注意android:order =“ 100”属性。 在这种情况下,这实际上是优先事项。 如果您有一个与相同路径(/ products和/ products / 10)相对应的产品列表和一个产品单,则在/ products路径后面有一个id时,将启动产品单活动。 如果不是,则启动产品列表活动。

It is very important to define this. If there are two activities that correspond to the same path, the Play Store won’t know which part of the app should be fetched.

定义这一点非常重要。 如果有两个活动对应同一路径,则Play商店将不知道应提取应用的哪一部分。

将您的应用与您的域相关联 (Associate your app with your domain)

The assetlinks.json will need to contain your SHA256 keystore hashes. The relation field is set to the default value below, and the target object needs to be filled with app specific data and your SHA256 hash of the keystore.

assetlinks.json将需要包含SHA256密钥库哈希 。 关系字段设置为下面的默认值,并且目标对象需要使用应用程序特定的数据和密钥库的SHA256哈希填充。

[{   "relation": ["delegate_permission/common.handle_all_urls"],  "target": {   "namespace": "android_app",   "package_name": "com.example.app",   "sha256_cert_fingerprints":["00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"]   } }]

When autoVerify=true does its magic, all associated App Links will directly launch your app. If you don’t have the app installed, the instant app will be downloaded instead.

autoVerify = true发挥作用时,所有关联的应用程序链接将直接启动您的应用程序。 如果您尚未安装该应用,则将立即下载该即时应用。

Here’s an example of a demo app we did recently. When clicked on the associated link, a screen like this opens and offers to use the instant app instead. Note how quickly the app is opened, and on Oreo it’s even faster.

这是我们最近进行的演示应用程序的示例。 当单击关联的链接时,将打开一个类似这样的屏幕,并提示您使用即时应用程序。 请注意,该应用的打开速度有多快,在Oreo上则更快。

如何定义Android Instant模块? (How to Define Android Instant Modules?)

For an instant app, your project will consist of at least three different modules. You need to use Android Studio 3.0 for this. If you’re creating your app from scratch, there’s an option to enable the Instant app support for your project.

对于即时应用程序,您的项目将至少包含三个不同的模块。 您需要为此使用Android Studio 3.0。 如果您是从头开始创建应用程序,则可以选择为项目启用即时应用程序支持。

All of the following modules will be initialised automatically. If you’re modifying an older app, you’ll need to break the old app module into a single base module and a couple of feature modules. Also, you’ll need to create an App and an Instant app module, which you will use to build both regular and instant app APKs.

以下所有模块将自动初始化。 如果要修改较旧的应用程序,则需要将旧的应用程序模块拆分为一个基本模块和几个功能模块。 另外,您将需要创建一个应用程序和一个即时应用程序模块,用于构建常规和即时应用程序APK。

App Module

应用模块

First, you have to create an app module which defines the dependencies for all other modules (base + feature modules). In the build.gradle file of this module, you will need to define the following:

首先,您必须创建一个应用程序模块,该模块定义所有其他模块(基本+功能模块)的依赖关系。 在此模块的build.gradle文件中,您将需要定义以下内容:

apply plugin: 'com.android.application' ...
dependencies {   implementation project(':product')   implementation project(':productlist')   implementation project(':base') }

Base Module

基本模块

In this module, you will define the following dependency statements. Also, make sure that the ‘com.android.feature’ plugin is applied here.

在本模块中,您将定义以下依赖项语句。 另外,请确保在此处应用了“ com.android.feature”插件。

apply plugin: 'com.android.feature' android {  baseFeature true   ... }
dependencies {   api 'com.android.support:appcompat-v7:26.0.1'   api 'com.android.support.constraint:constraint-layout:1.0.2'  implementation 'com.google.firebase:firebase-appindexing:11.0.4'  application project(':app')   feature project(':product')   feature project(':productlist') }

Note that here, the compile statements become API statements for the regular dependencies we used before. The application project and feature projects are defined separately.

请注意,这里的compile语句成为我们之前使用的常规依赖项的API语句。 应用程序项目和功能项目是分别定义的。

Feature Module

功能模块

This module will have the following setting, also with the com.android.feature plugin applied.

该模块将具有以下设置,并且已应用com.android.feature插件。

apply plugin: 'com.android.feature' ... dependencies {   implementation project(':base')   ... }

You need to state which module is your base module and include it with the implementation project statement. Next, you can include the dependencies which are required only for this specific module. For example, if you’re using an animation library which is not used in any of the other modules.

您需要声明哪个模块是您的基本模块,并将其包含在实施项目语句中。 接下来,您可以包括仅此特定模块所需的依赖项。 例如,如果您使用的动画库在其他任何模块中均未使用。

Instant App Module

即时应用模块

Finally, now there’s a com.android.instantapp plugin to be included in the build.gradle file for the instantapp module.

最后,现在有一个com.android.instantapp插件包含在Instantapp模块的build.gradle文件中。

apply plugin: 'com.android.instantapp' dependencies {   implementation project(':product')   implementation project(':productlist')   implementation project(':base') }

In this module, we will define which modules will be built as instant apps. The result of the instantapp module build is a zip file with the instant app APKs which you can upload separately to Google Play Store in the Android Instant Apps release manager. These APKs are handled similarly as the regular ones, they have their own rollout history and versioning.

在此模块中,我们将定义将哪些模块构建为即时应用程序。 Instantapp模块构建的结果是一个包含即时应用APK的zip文件,您可以在Android Instant Apps版本管理器中分别将其上传到Google Play商店。 这些APK的处理方式与常规APK类似,它们具有自己的推出历史记录和版本控制。

That’s it! It’s fairly simple to start developing Android Instant Apps. But, there’s always a but!

而已! 开始开发Android Instant Apps非常简单。 但是,总会有一个!

Android Instant Apps面临哪些挑战? (What were the Android Instant Apps’ challenges?)

First of all, the Instant Apps are not enabled by default for now. If you want to try it, you need to check your phone settings under Google account and enable the Instant Apps setting.

首先,默认情况下,暂时不启用Instant Apps。 如果要尝试,则需要检查Google帐户下的手机设置并启用“即时应用”设置。

Next, we found that it’s extremely important to specify app links data in the following format:

接下来,我们发现以以下格式指定应用链接数据非常重要:

<intent-filter android:autoVerify="true"> ... <data android:scheme="http"   android:host="example.domain"   android:pathPrefix="/products" /> <data android:scheme="https"/> </intent-filter>

Both http and https schemes need to be defined as shown in this code snippet. Any other way would cause a link verification failure and the app wouldn’t be linked properly.

如下面的代码片段所示,必须定义http和https方案。 任何其他方式都会导致链接验证失败,并且应用程序无法正确链接。

Also, there is a recommendation to include the following code snippet into one of the activities in your app manifest. This annotates which activity should be launched in case the Instant app is launched from the Settings or a system launcher.

另外,建议您在应用清单中的活动之一中包含以下代码段。 这可以注释在从“设置”或系统启动器启动Instant应用程序的情况下应启动的活动。

<meta-data  android:name="default-url" android:value="https://example.domain" />

The official documentation states that the Google Search would offer Instant app annotation by default (small thunder icon), but we had problems with it. For our demo app, this was not the case. Google Search results didn’t annotate our demo links as Instant apps and the links led to the web page. Only if we tried to open the associated link from another app, like Gmail, the whole instant app process was triggered and the instant app was launched. Have you encountered any similar problems?

官方文档指出,默认情况下,Google搜索会提供即时应用注释(小小的雷声图标),但是我们遇到了问题。 对于我们的演示应用程序,情况并非如此。 Google搜索结果没有将我们的演示链接注释为Instant apps,并且这些链接指向了网页。 仅当我们尝试打开其他应用程序(例如Gmail)的关联链接时,整个即时应用程序流程才会被触发并启动即时应用程序。 您是否遇到过类似的问题?

结论 (Conclusion)

When first announced two years ago, I was very enthusiastic about Android Instant Apps. They respond to the problem of users having to search for the apps on the Store and wait till they’re downloaded to start using them. Web apps are much more accessible in that regard and the ease of discovery is much better.

两年前首次发布时,我对Android Instant Apps非常热衷。 他们回应了用户不得不在商店中搜索应用程序并等到下载后才能开始使用它们的问题。 在这方面,Web应用程序更易于访问,发现的便利性更好。

Instant apps come really close to filling this gap between web and native mobile apps. They already act very well and I think that they will become more popular with time. The main problems we encountered was a rather small community and the lack of proper documentation, but the situation on that matter is also getting better.

即时应用程序确实接近填补了网络和本机移动应用程序之间的空白。 他们已经表现得很好,我认为他们会随着时间的推移变得越来越受欢迎。 我们遇到的主要问题是一个相对较小的社区和缺乏适当的文档,但是在此问题上的情况也越来越好。

We would love to hear from you if you’ve tried using them or had any challenges implementing them!

如果您尝试使用它们或在实现它们时遇到任何挑战,我们很乐意听取您的意见!

Originally published at www.bornfight.com.

最初在www.bornfight.com上发布。

翻译自: https://www.freecodecamp.org/news/android-instant-apps-101-what-they-are-and-how-they-work-8b039165ed24/

instant apps

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值