谷歌最近更新了有关即时应用的文档:准备你的应用程序
其中大部分内容都很清晰,除了第三点:如有必要,请重构您的应用。
他们建议为浏览、搜索、物品详情和结账等零售模块进行操作。
问题:我如何将应用程序拆分为完全功能和URL可寻址的模块?
这里存在几个问题:
- 如果我们使用诸如Dagger、ButterKnife等库,则所有模块都会依赖于其他模块
- 如果我们的模块包含所需的(视图),则应如何实现从另一个模块到另一个视图的过渡而不导入此模块?
是否有人能为此提供帮助?谢谢!
谷歌最近更新了有关即时应用的文档:准备你的应用程序
其中大部分内容都很清晰,除了第三点:如有必要,请重构您的应用。
他们建议为浏览、搜索、物品详情和结账等零售模块进行操作。
问题:我如何将应用程序拆分为完全功能和URL可寻址的模块?
这里存在几个问题:
是否有人能为此提供帮助?谢谢!
使用以下步骤可以轻松完成Instant Apps所需的模块分离:
com.android.feature
插件,其使用方式类似于传统的com.android.library
插件,在Android项目中可以像使用库一样使用它,因此可以将库文档作为参考。在输出方面,与可安装的应用程序模块一起使用时,它将输出常规的AAR文件;与新的Instant App模块一起使用时,它将输出特性APK。Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://yourdomain.com/itemdetail"));
intent.setPackage(getPackageName());
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(intent);
如其他人所述,任何将被所有功能使用的库都将放入您的基础功能中。如果我们使用像dagger、butterknife等库,所有模块都将依赖于其他模块
这个答案涵盖了概述-但这部分似乎是你问题的根源,所以我会尝试深入挖掘一下。如果我们的模块包含所需的(视图),则应该如何实现从另一个模块到另一个视图的转换而不导入此模块?
BrowseActivity
)想要打开功能2(ItemDetailActivity
)。 与其直接调用 startActivity(ItemDetailActivity.class)
,功能1将不得不使用下面的方法调用(因为它们彼此之间没有依赖关系,所以功能1无法访问功能2的ItemDetailActivity.class
)。
这是Google提供的代码示例
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com/itemdetail"));
intent.setPackage(getPackageName());
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(intent);
AndroidManifest
中,您需要声明ItemDetailActivity
正在监听https://example.com/itemdetail
链接。这里是来自Google的相关代码示例。<activity android:name=".ItemDetailActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="http" />
<data android:scheme="https" />
<data android:host="example.com" />
<!-- IMPORTANT -->
<data android:pathPrefix="/itemdetail"/>
</intent-filter>
<meta-data
android:name="default-url"
android:value="https://www.example.com/itemdetail" />
</activity>