如何在即时应用中包含即时动态功能模块? [英] How to include instant dynamic feature module in instant app?

查看:60
本文介绍了如何在即时应用中包含即时动态功能模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下模块的项目:

I have a project with these modules:

  • 应用
  • bookingfeature (即时启用)
  • 地图(不是即时)
  • app
  • bookingfeature (instant enabled)
  • map (not instant)

app 包含一些常见的代码和资源,以及一些启动逻辑,这些逻辑根据应用是否立即运行将其路由到正确的目的地.

app contains some common code and resources and a starting activity with some logic to route the app to the correct destination, based on whether it's running as instant or not.

bookingfeature 包含一个活动和一些我想通过即时应用程序部署的片段.

bookingfeature contains an activity and some fragments that I want to deploy with the instant app.

map 包含应用程序的其余部分(正在将其拆分为更多模块的工作正在进行中)

map contains the rest of the app (work in progress to split this into more modules)

如果我在android studio中这样部署它,一切都会很好:

Everything works fine if I deploy it like this in android studio:

如果我取消选中 bookingfeature 的框,则显然无法使用,因为该功能不存在.

If I untick the box for bookingfeature obviously it won't work, because the feature is not present.

当我创建一个应用程序捆绑包并将其上传到Play商店时,点击立即尝试"在Play商店中,它的行为就像没有勾选 bookingfeature 一样.

When I create an app bundle and upload it to play store, and click on "try now" in play store, it behaves like bookingfeature is not ticked.

我可以使其表现得像 bookingfeature 一样被打勾,以某种方式将其包含在 app 模块中吗?还是我必须将所有代码从 bookingfeature 移到 app ?

Can I make it behave like bookingfeature is ticked, include it in the app module somehow? Or do I have to move all the code from bookingfeature into app?

执行立即尝试"吗?按钮只能运行 app 模块,有没有办法更改它?

Does the "try now" button only run the app module, is there no way to change it?

app 清单:

<manifest …>
<dist:module dist:instant="true" />

<application
   …
   android:name=".App">

    <activity
        android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


bookingfeature 清单:

<manifest ...>

    <dist:module
        dist:instant="true"
        dist:title="@string/title_bookingfeature">
        <dist:delivery>
            <dist:install-time />
        </dist:delivery>
        <dist:fusing dist:include="false" />
    </dist:module>
    <application>
        <activity
            android:name=".booking.view.BookingActivity"/>
    </application>
</manifest>


MainActivity :

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil
            .setContentView(this, R.layout.activity_main)

        if (isInstantApp(this)) {
            findNavController(R.id.main_nav_host).navigate(R.id.booking_activity)
        } else {
            findNavController(R.id.main_nav_host).navigate(R.id.splash_activity)
        }
        finish()
    }
}

导航:

...
<activity
   android:id="@+id/booking_activity"
   android:name="x.x.x.booking.view.BookingActivity"
   app:moduleName="bookingfeature" />

<activity
    android:id="@+id/splash_activity"
    android:name="x.x.map.splash.SplashActivity"
    app:moduleName="map" />

当我从活动中删除 finish()时,它将实际上启动BookingActivity并安装功能模块.但这不是我想要的.我希望该模块在下载即时应用程序时包含在内.

When I remove finish() from the activity, it will actually launch the BookingActivity and install the feature module. But it's not exactly what I want. I would like the module to be included when it downloads the instant app.

推荐答案

我遇到了完全相同的问题,并且此选项作为即时应用程序部署"毁了我几个小时.无论如何,我在即时模块的清单中添加了CATEGORY_LAUNCHER(在您的情况下为 bookingfeature ).

I had exact same problem and this option "Deploy as Instant app" destroyed my several hours. anyway i got rid of it by adding CATEGORY_LAUNCHER in manifest of instant module(bookingfeature in your case).

您必须从基本模块清单中删除CATEGORY_LAUNCHER,仅将其添加到即时模块清单中,否则在使用"Install"安装应用时会显示两个应用图标.游戏商店上的按钮.

you have to removed CATEGORY_LAUNCHER from base module manifest and only add it in instant module manifest otherwise it will show two app icons when app is installed using "Install" button on playstore.

现在,当您使用立即尝试"时,它将打开您设置为Launcher的即时模块活动,您可以从该活动中检查它是否作为即时应用程序运行,并启动基本模块活动.

Now when you use "Try Now" it will open your instant module activity which is set as Launcher and from this activity you can check whether it is running as an instant app or not and launch the base module activity.

Instant Module Activity will look like this -
    <activity
                android:name=".booking.view.BookingActivity">
    <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>

当您使用安装"安装应用时,在Playstore上点击按钮,它还会下载即时模块和基本模块.因此,现在它将启动即时模块的BookingActivity,并且在此活动中,您将检查它是否作为即时应用程序运行-

When you install the app using "Install" button on playstore it also downloads the instant module along with base module. So now it will launch this BookingActivity of instant module and inside this activity you will check if it is running as an instant app or not -

val instantApp = InstantApps.getPackageManagerCompat(this).isInstantApp()
if(!instantApp) {
 // start base module activity
// finish() this activity
}

希望这会有所帮助,如果您有任何建议/其他解决方法,请发表评论.

Hope this helps, do comment if you have any suggestion/any other way to fix it.

这篇关于如何在即时应用中包含即时动态功能模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆