Android Instant Apps教程

本文详细介绍了Android Instant Apps的原理和实现方法,包括如何模块化项目、创建活动以及设置URL映射,以提供无需下载即可体验的应用体验。通过实例展示了如何在Android Studio中创建即时应用程序,并解释了即时应用与传统应用的不同之处。
摘要由CSDN通过智能技术生成

In this tutorial, we’ll be discussing what are Instant Apps in Android and how to implement them in our android application.

在本教程中,我们将讨论什么是Android中的即时应用程序以及如何在我们的android应用程序中实现它们。

Android Instant Apps (Android Instant Apps)

It often happens on the web that when we open an URL link, it asks us to download the application. This can hamper the User Experience.

在网络上经常发生的情况是,当我们打开URL链接时,它要求我们下载该应用程序。 这可能会妨碍用户体验。

Hence google has come up with Instant Apps which are a mini version of the full application and can be launched from url without downloading the application entirely.

因此,谷歌提出了即时应用程序,这是完整应用程序的迷你版本,可以从url启动而无需完全下载该应用程序。

In Google Playstore, applications with instant apps feature would have a TRY NOW button along with the install button.

在Google Playstore中,具有即时应用程序功能的应用程序将具有“立即尝试”按钮和安装按钮。

Instant Apps are currently supported for devices running Android 5.0 and above.

目前,运行Android 5.0及更高版本的设备支持Instant Apps。

Instant Apps require the developers to modularise their project such that it would contain different screens/resources in different modules.

Instant Apps要求开发人员对他们的项目进行模块化,以便在不同的模块中包含不同的屏幕/资源。

An instant app project largely consists of the following modules:

即时应用项目主要由以下模块组成:

  • app module

    应用模块
  • instantapp module

    Instantapp模块
  • base feature module

    基本功能模块

应用模块 (app module)

app module contains everything that must not be there in the instant app. Having said that, the applicationId would be defined in the base feature module only.

应用程序模块包含即时应用程序中不能存在的所有内容。 话虽这么说, applicationId只能在基本功能模块中定义。

基本功能模块 (base feature module)

This module consists of the shared activities that would be used in the instant app and app module.

此模块包含将在即时应用程序和应用程序模块中使用的共享活动。

The applicationId of the application resides here. The main AndroidManifest.xml file is defined here too with all the activities, permissions etc.

applicationId的applicationId驻留在此处。 这里也定义了AndroidManifest.xml主文件,其中包含所有活动,权限等。

This module would hold all the resources for the application – drawable, mipmap icons etc.

该模块将保存应用程序的所有资源-可绘制,mipmap图标等。

In the build.gradle file of this module, we set the plugin to com.android.feature as well as set baseFeature true inside the android block.

在此模块的build.gradle文件中,我们将插件设置为com.android.feature ,并将android块内的baseFeature true设置baseFeature true

功能模块 (feature module)

This contains activities and classes for a specific feature of the instant app. By default, an instant app when loaded would download only one feature and one base feature module along with the instant app.

其中包含即时应用程序特定功能的活动和类。 默认情况下,即时应用程序在加载时将仅下载一个功能和一个基本功能模块以及即时应用程序。

You can create multiple features as per your requirements

您可以根据需要创建多个功能

Instantapp模块 (instantapp module)

This cannot contain any code. In the build.gradle file of this module, we add the dependencies for the base and feature modules.

它不能包含任何代码。 在此模块的build.gradle文件中,我们添加了基本模块和功能模块的依赖项。

Following is a diagram depicting how each of these modules is related.

下图描述了每个模块之间的关系。

The app module takes all the feature modules in aar format when the APK is built.
构建APK后,app模块将采用aar格式获取所有功能模块。

Where do the URL link to launch certain activities reside?

启动某些活动的URL链接位于何处?

Thanks to App Links Assistant, we can create url links mapped to every activity. Once it is created, an intent filter with the url data is set inside the AndroidManifest.xml file.

多亏了App Links Assistant,我们可以创建映射到每个活动的URL链接。 创建之后,将在AndroidManifest.xml文件中设置带有url数据的意图过滤器。

Once an application is uploaded to the playstore, which url is launched first?

将应用程序上传到Playstore后,哪个网址会首先启动?

We need to set a default-url in our manifest. On whichever activity it is set, that gets launched first.

我们需要在清单中设置一个default-url 。 无论设置了哪种活动,都会首先启动。

Once an application on playstore is instant app compatible, we can launch it using the link:
https://play.google.com/store/apps/details?id={package_name}&launch=true.

Playstore上的应用程序与即时应用程序兼容后,我们可以使用链接启动它:
https://play.google.com/store/apps/details?id={package_name}&launch=true

  • Android SDK Build-Tools 26.x or higher

    Android SDK Build-Tools 26.x或更高版本
  • Android SDK Platform Tools 25.x or higher

    Android SDK平台工具25.x或更高版本
  • Android SDK Tools (latest)

    Android SDK工具(最新)
  • Instant Apps Development SDK (latest)

    即时应用开发SDK(最新)
  • Support Repository (latest)

    支持资料库(最新)
  • For emulators, Google Play Image must be installed

    对于模拟器,必须安装Google Play图片
  • You need to implement Smart Lock for instant apps that require user login.

    您需要为需要用户登录的即时应用程序实施Smart Lock。

Enough talk. Let’s code!

聊够了。 让我们编码吧!

Let’s create our first AndroidInstantApps application in Android Studio.

让我们在Android Studio中创建第一个AndroidInstantApps应用程序。

Android Instant Apps入门 (Getting Started with Android Instant Apps)

On the next screen set the name of a feature module:

在下一个屏幕上,设置功能模块的名称:

In the following screen, select the activity template.

在以下屏幕中,选择活动模板。

We’ve used the Login Activity template.

我们使用了“登录活动”模板。

On the next screen, it’ll ask for the app link for the first activity.

在下一个屏幕上,它将询问第一个活动的应用程序链接。

Following is the random url path that we’ve set:

以下是我们设置的随机网址路径:

Once this is done, we have our project structure ready as shown below:

完成此操作后,我们将准备好我们的项目结构,如下所示:

Once you navigate through the folders, you’ll see that the activity gets created in our feature module. The manifest file resides in the same place:

浏览文件夹后,您会看到该活动已在我们的功能模块中创建。 清单文件位于同一位置:

Now to run the application we need to set a default-url in the manifest.

现在要运行该应用程序,我们需要在清单中设置一个默认URL。

We have two ways to do so:

我们有两种方法可以做到这一点:

Set the meta-data tag with the default url in the manifest itself.

在清单本身中使用默认网址设置元数据标签。

<meta-data android:name="default-url"
        android:value="https://www.journaldev.com/preview" />

Other way, we can set the url of the activity to be launched from the Edit Configurations.

另外,我们可以设置要从“编辑配置”启动的活动的网址。

The output of the above application is:

上面的应用程序的输出是:

As you can see from the recent tabs screenshot, the app icon have the instant app badge on it.

从最近的标签屏幕截图中可以看到,该应用程序图标上带有即时应用程序徽章。

Clicking the info button would show the following screen:

单击信息按钮将显示以下屏幕:

Clicking install would install the full application.

单击安装将安装完整的应用程序。

Up until now, we haven’t added anything into the app module.

到目前为止,我们还没有在app模块中添加任何内容。

Let’s create an activity in the app module and try running the instant app and the app modules separately.

让我们在app模块中创建一个活动,然后尝试分别运行instant appapp模块。

在App模块中创建活动 (Creating Activities in the App Module)

Right click on main in the app module and create a new directory named java.

在应用程序模块中右键单击main,然后创建一个名为java的新目录。

Create a new package with the name: com.journaldev.androidinstantapps.app inside the java folder.

在java文件夹中创建一个名称为com.journaldev.androidinstantapps.app的新软件包。

Inside the package create an activity by right-clicking and then New | Activity | Settings Activity.

在包内,通过右键单击然后单击新建|创建活动 活动| 设置活动

Following is how the project structure looks:

以下是项目结构的外观:

Since this is an Instant App project we need to add an URL for activities in the app module too.

由于这是一个即时应用程序项目,我们也需要在应用程序模块中添加活动的URL。

This is because Instant Apps navigate between activities within the same and different modules through urls.

这是因为Instant Apps通过URL在相同模块和不同模块内的活动之间导航。

So goto Tools | App Links Assistant | Open URL Mapping Editor. Over there add a new url mapper for the SettingsActivity.

因此,转到工具| 应用链接助手| 打开URL映射编辑器。 在那儿为SettingsActivity添加一个新的URL映射器。

app/main/AndroidManifest.xml

app / main / AndroidManifest.xml

Now goto The LoginActivity.java class in the feature module and replace the following code:

现在转到功能模块中的LoginActivity.java类,并替换以下代码:

mEmailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://journaldev.com/settings"));
                intent.setPackage(getPackageName());
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                startActivity(intent);

                //remove the below line and add the above ones.
                //attemptLogin(); 
            }
        });

So in the Intent, we pass in the link for the SettingsActivity that was created in the app module.

因此,在Intent中,我们传入了在应用模块中创建的SettingsActivity的链接。

This way you can switch between activities in different modules.

这样,您可以在不同模块中的活动之间进行切换。

Now run the application in the app module configuration.

现在,在应用程序模块配置中运行该应用程序。

Now the output of the application in action is given below:

现在,下面给出了实际应用程序的输出:

If you run the above code, but as an instant app, it will crash.

如果您运行上述代码,但是作为即时应用程序,它将崩溃。

WHY? 为什么?

As told at the beginning of this tutorial, an instant app cannot access the app module.

如本教程开始时所述,即时应用程序无法访问应用程序模块。

This brings an end to this tutorial. We’ll see how to refactor an existing project to use instant app feature in a later tutorial. You can download the project from the link below:

本教程到此结束。 在后面的教程中,我们将介绍如何重构现有项目以使用即时应用程序功能。 您可以从下面的链接下载项目:

翻译自: https://www.journaldev.com/21764/android-instant-apps-tutorial

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值