如何在即时应用中包含即时动态特性模块?

4
我有一个涉及以下模块的项目:
  • app
  • bookingfeature (开启即时功能)
  • map (未开启即时功能)
app 包含一些常用代码和资源,以及一个起始活动,其逻辑可根据是否作为即时应用运行将应用程序路由到正确的目标。 bookingfeature 包含一个活动和一些片段,我想部署到即时应用程序中。 map 包含其余的应用程序(正在进行工作,以将其拆分成更多模块)
使用 Android Studio 进行此配置一切正常。

enter image description here

如果我取消勾选 bookingfeature 的框,显然它将无法工作,因为该功能不存在。
当我创建一个应用程序包并将其上传到Play商店,并在Play商店中单击“立即尝试”,它的行为就像未选择 bookingfeature 一样。
我是否可以使其表现得像已选择 bookingfeature,并将其包含在 app 模块中?还是我必须将所有代码从 bookingfeature 移动到 app 中?
“立即尝试”按钮是否只运行 app 模块,没有改变它的方法吗?

应用程序清单:

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

<applicationandroid: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并安装功能模块。但这不是我想要的。我希望在下载即时应用程序时包含该模块。

1个回答

1
我遇到了完全相同的问题,而这个选项“作为即时应用部署”让我浪费了几个小时。无论如何,在即时模块(在你的情况下是bookingfeature)的清单中添加CATEGORY_LAUNCHER后,我解决了这个问题。
你需要从基础模块清单中删除CATEGORY_LAUNCHER,并只将其添加到即时模块清单中,否则在使用Playstore上的“安装”按钮安装应用程序时,它将显示两个应用图标。
现在,当你使用“立即试用”时,它会打开你的即时模块活动,该活动已设置为启动器,并且你可以从此活动检查它是否正在作为即时应用运行并启动基础模块活动。
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,并在此活动中检查它是否正在作为即时应用运行 -
val instantApp = InstantApps.getPackageManagerCompat(this).isInstantApp()
if(!instantApp) {
 // start base module activity
// finish() this activity
}

希望这有所帮助,如果您有任何建议或其他修复方法,请在评论区留言。

感谢您提供详细答案。看起来我们只能包含一个特性模块,那就是具有启动器活动的特性模块?我一段时间前放弃了这个项目,但我相信这将对未来有所帮助。 - Torkel Velure
我们可以拥有多个即时特性模块,但只能有一个即时特性模块具有启动器活动,其他模块可以定义URL链接。 - Jitendra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接