js模块封装示例_奖金-动态模块可能非常适合的示例

js模块封装示例

Have you been lately searching for tricks to reduce your app size? I am pretty sure you must have come around the term “Dynamic feature modules”. Let’s see how can one benefit from them. Or can one?

您最近是否在寻找减少应用程序大小的技巧? 我很确定您一定使用术语“ 动态功能模块 ”。 让我们看看如何从中受益。 还是可以?

应用大小如何重要? (How does the app size matters?)

App size is one of the vital metrics that an android developer strives to keep low, especially after Nov 2017 when Play Store decided to show app size when showcasing the apps on its platform. Since then Google has also been providing innovative ways to help developers. In 2018, they launched app bundle and dynamic modules. App bundles are no doubt a life saviour when you are out of ideas to squeeze out some extra size. We at Bobble.ai reduced our app size by 19% just by switching over to app bundles. But, what about dynamic delivery?

应用程式大小是Android开发人员努力保持低位的重要指标之一,尤其是在2017年11月之后,Play商店决定在其平台上展示应用程式时显示应用程式大小。 从那时起,Google一直在提供创新的方式来帮助开发人员。 在2018年,他们推出了App Bundle和动态模块。 当您没有主意来挤出一些额外的尺寸时,应用程序包无疑是一个救生员。 仅通过切换到应用程序捆绑包, Bobble.ai的应用程序大小减少了19%。 但是,动态交付又如何呢?

什么是动态功能模块? (What are Dynamic feature modules?)

The idea of dynamic feature modules is similar to à la carte where the consumer (user in our case) is served only those services which he/she demands. The main motive behind dynamic feature modules was to stop bloating user’s device from giving them features that they don’t even require.

动态功能模块的概念类似于点菜 ,即仅向消费者(在我们的情况下为用户)提供他/她需要的那些服务。 动态功能模块背后的主要动机是阻止膨胀给用户的设备提供他们甚至不需要的功能。

So, after reducing 19% of our app size, we decided to dive into dynamic modules and see if we can further improve our app size. I won’t be discussing on how to start with dynamic modules, rather how it works internally, its limitations, and how to decide if you need dynamic modules.

因此,在缩小应用程序大小的19%之后,我们决定深入研究动态模块,看看是否可以进一步提高应用程序大小。 我不会讨论如何从动态模块开始,而是它在内部如何工作,其局限性以及如何确定是否需要动态模块的问题。

动态应用程序模块如何工作? (How does dynamic app modules work?)

Let us dive deep to see how dynamic app modules work. By default, when a app is installed, all its code(dex files and native libs) and resources(drawable, strings, values, etc) are placed at /data/app/<package-name>/ . Code and resources here are directly accessible to the system. But, when a dynamic module is downloaded, it’s code and resources are placed at your apps internal storage — /data/data/<package-name>/files/splitcompat/<version-code>/. Code and resources placed here are NOT ACCESSIBLE to the system!

让我们深入了解动态应用程序模块如何工作。 默认情况下,安装应用程序后,其所有代码(dex文件和本机libs)和资源(可绘制,字符串,值等)都位于/ data / app / <package-name> /中。 此处的代码和资源可直接由系统访问。 但是,下载动态模块时,其代码和资源位于应用程序的内部存储中- / data / data / <程序包名称> / files / splitcompat / <版本代码> /。 此处放置的代码和资源对系统而言是不可访问的!

Image for post
LEFT-data/data/com.touchtalent.bobbleapp -data / data / com.touchtalent.bobbleapp RIGHT-data/app/com.touchtalent.bobbleapp/files/splitcompat -data / app / com.touchtalent.bobbleapp / files / splitcompat

This is why, we need to call SplitCompat.install() in Application class and SplitCompat.installActivity() in module activities. These function calls are responsible for emulating the dynamic modules as if they were a part of the app. Without them, the system won’t be able to recognise the dynamic modules. Since we have seen how it works, let’s discuss its limitations.

这就是为什么我们需要在Application类中调用SplitCompat.install()并在模块活动中调用SplitCompat.installActivity()的 原因 。 这些函数调用负责模拟动态模块,就像它们是应用程序的一部分一样。 没有它们,系统将无法识别动态模块。 既然我们已经了解了它的工作原理,那么让我们讨论它的局限性。

局限性(解决方法很少) (Limitations (with few workarounds))

  1. The dynamic module feature only works with app bundles and are not included if you build an apk. Yet, if you want to distribute your app via apk (direct apk from a website or other app stores) including the dynamic module, you need to build your project via command line —

    动态模块功能仅适用于应用程序捆绑包如果您构建apk则不包含 。 但是,如果您想通过apk(来自网站或其他应用商店的直接apk)分发应用程序(包括动态模块),则需要通过命令行构建项目-

    The dynamic module feature only works with app bundles and are not included if you build an apk. Yet, if you want to distribute your app via apk (direct apk from a website or other app stores) including the dynamic module, you need to build your project via command line — ./gradlew :app:packageDebugUniversalApk

    动态模块功能仅适用于应用程序捆绑包如果您构建apk则不包含 。 但是,如果您想通过apk(来自网站或其他应用商店的直接apk)分发应用程序(包括动态模块),则需要通过命令行来构建您的项目./gradlew :app:packageDebugUniversalApk

    But this will disable your apk splits(older alternative to app bundles) and you may see a sudden a spike in app size.

    但这会禁用您的apk拆分(较旧的应用程序捆绑包),并且您可能会看到应用程序大小突然增加。

  2. If your dynamic feature modules include activities or services which interact with system components, such as launcher shortcut icons, dynamic shortcuts, or notifications, you may have a hard time customising your app to support these. This is because these system components are unable to access resources like icons from dynamic modules. They need to be explicitly moved to the base module instead of dynamic module, which in turn defeats the purpose of modularising.

    如果动态功能模块包含与系统组件交互的活动或服务,例如启动器快捷方式图标,动态快捷方式或通知,则可能很难定制应用程序以支持这些功能。 这是因为这些系统组件无法访问动态模块中的资源,例如图标 。 需要将它们显式移动到基本模块而不是动态模块,这反过来又不利于模块化的目的。

  3. It is not easy to implement a direct entry point to the dynamic module like a launcher icon. This is because the manifest of the base module and dynamic feature module is merged at build time. Suppose you include an activity X in your dynamic feature module marked with launcher category. This information is delivered with base apk and not dynamic module apk. Therefore the launcher will know that there exists an activity X (because the manifest was merged) and will showcase it. But, when the user clicks on it, a crash will happen if the module isn’t yet downloaded.

    像启动器图标一样,实现动态模块的直接入口点并不容易。 这是因为基本模块和动态功能模块清单在构建时已合并 。 假设您在动态功能模块中添加了标有启动器类别的活动X。 此信息与基本apk一起提供,而不与动态模块apk一起提供。 因此,启动器将知道存在活动X(因为清单已合并)并将展示它。 但是,当用户单击它时,如果尚未下载该模块,则会发生崩溃。

    This can be prevented by enabling/disabling the component from the base module. But this can be hard to implement if you want to use

    这可以通过从基本模块启用/禁用组件来防止。 但这可能很难实现

    deferredInstall(), as it becomes hard to track whether the module has been installed.

    deferredInstall(),因为很难跟踪模块是否已安装。

    P.S: Google is trying to make this easier with

    PS:Google尝试通过以下方法简化此操作

    Navigation framework.

    导航框架

  4. Code from dynamic modules is not directly accessible to the base module. This brings in the need of Reflection class, which is still a nightmare for many developers. Special care has to be taken with code obfuscation. Code refactoring and maintainability also becomes hard.

    基本模块不能直接访问动态模块中的代码。 这带来了Reflection类的需求,对于许多开发人员而言,这仍然是一场噩梦。 代码混淆必须格外小心。 代码重构和可维护性也变得很困难。

  5. Testing dynamic modules are very difficult since it requires to be deployed on the Play Store. Yes, bundletool does have a local-testing option but it’s not enough for your app to be ready for rollout. Testing via Google Play Internal app sharing is not as efficient as deploying it directly from Android Studio/command line. It is also possible that you may not have set up your app on Play console yet or probably you don’t have access to the internal app sharing(if you are not admin of the play console account).

    测试动态模块非常困难,因为它需要部署在Play商店中。 是的,bundletool确实具有本地测试选项,但这还不足以使您的应用程序准备就绪。 通过Google Play内部应用共享进行测试的效率不如直接从Android Studio /命令行进行部署有效。 也有可能您尚未在Play控制台上设置您的应用,或者您可能无权访问内部应用共享(如果您不是Play控制台帐户的管理员)。

  6. Resources from Dynamic modules can be accessed only via Application level context because that is where the emulation took place. Using other context won’t throw any exception but will give you unwanted and unexpected behaviour. So, be careful with them.

    只能通过应用程序级别上下文访问动态模块中的资源,因为这是进行仿真的地方。 使用其他上下文不会引发任何异常,但是会给您带来不必要和意外的行为。 因此,请小心他们。

  7. Android Studio’s resource shrinker, (shrinkResources=true) is NOT compatible with dynamic modules. You may have to manage those changes yourself, if you have been benefiting from them till now. However, this may not be the case when Android studio 4.2 rolls out.

    Android Studio的资源缩减器( rinkestResources = true)与动态模块 兼容 。 如果您一直从中受益,那么您可能必须自己管理这些更改。 但是,Android Studio 4.2推出时可能并非如此

  8. Last but not the least, if your dynamic feature module size exceeds 10MB, Play Store pops up for a confirmation from the user when the download is initiated, which definitely adds to friction in your elegantly designed UI and UX.

    最后但并非最不重要的一点是,如果动态功能模块的大小超过10MB,则会在开始下载时弹出Play商店以供用户确认,这无疑会增加设计精美的UI和UX的难度。

我应该如何决定? (How should I decide?)

It is undoubtedly, a very hard decision whether or not to use dynamic feature modules. You are now aware of the hardships related to dynamic feature modules and how they can affect you. Analysing the flowchart below may help you to decide if you need dynamic feature modules in your app.

无疑,是否使用动态功能模块是一个非常艰难的决定。 您现在已经知道与动态功能模块相关的困难以及它们如何影响您。 分析下面的流程图可以帮助您确定是否需要在应用程序中使用动态功能模块。

Image for post
Flowchart to decide whether or not to use dynamic modules.
决定是否使用动态模块的流程图。

奖金-动态模块可能非常适合的示例 (Bonus - Examples where dynamic modules may be well suited)

  1. If you want to run machine learning models locally on the device, you may need to add machine learning library like TensorFlow Lite. Libraries like this along with the ML models can increase initial download size by a great extent.

    如果要在设备上本地运行机器学习模型,则可能需要添加机器学习库,例如TensorFlow Lite。 像这样的库以及ML模型可以在很大程度上增加初始下载大小。
  2. Imagine an app for managing some accounts. You may optionally provide your users with the option to export their data as an excel file. For that, you may want to use a library like Apache POI. Dynamic modules can be a good option in such scenarios.

    想象一下一个用于管理某些帐户的应用程序。 您可以选择为用户提供将其数据导出为ex​​cel文件的选项。 为此,您可能要使用Apache POI之类的库。 在这种情况下,动态模块可能是一个不错的选择。

结论 (Conclusion)

Dynamic modules at first may seem to be an amazing concept, but it is very crucial to carefully analyse several aspects of an app before using them. Even after 2 years of launch, it still needs numerous changes for the developers to benefit from them and pass the same to users.

刚开始,动态模块似乎是一个了不起的概念,但是使用它之前仔细分析应用程序的多个方面非常关键。 即使在发布了2年之后,开发人员仍然需要进行大量更改才能从中受益,并将其传递给用户。

翻译自: https://medium.com/bobble-engineering/dynamic-feature-modules-is-it-worth-the-effort-b9fe9af177fc

js模块封装示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值