动态模块功能和 ML Kit 在应用程序打开时崩溃

toa*_*sty 4 android firebase-mlkit dynamic-feature-module google-mlkit

应用程序具有 ML Kit 功能(翻译)。我试图通过引入动态模块功能、按需加载来减小应用程序的大小。

遵循本指南 将 'com.google.mlkit:playstore-dynamic-feature-support:16.0.0-beta1' 添加到基础 apk 的 build.gradle

com.google.mlkit:translate:16.1.2 在功能模块 build.gradle 中,

一切都会编译并尝试在模拟器上运行,但不幸的是应用程序从日志启动时崩溃

java.lang.RuntimeException: Unable to get provider com.google.mlkit.common.internal.MlKitInitProvider: com.google.firebase.components.MissingDependencyException: Unsatisfied dependency for component Component<[class com.google.android.gms.internal.mlkit_translate.zzxa]>{0, type=0, deps=[Dependency{anInterface=class com.google.mlkit.common.sdkinternal.SharedPrefManager, type=required, injection=direct}, Dependency{anInterface=class com.google.android.gms.internal.mlkit_translate.zzwx, type=required, injection=direct}]}: class com.google.mlkit.common.sdkinternal.SharedPrefManager

 Caused by: com.google.firebase.components.MissingDependencyException: Unsatisfied dependency for component Component<[class com.google.android.gms.internal.mlkit_translate.zzxa]>{0, type=0, deps=[Dependency{anInterface=class com.google.mlkit.common.sdkinternal.SharedPrefManager, type=required, injection=direct}, Dependency{anInterface=class com.google.android.gms.internal.mlkit_translate.zzwx, type=required, injection=direct}]}: class com.google.mlkit.common.sdkinternal.SharedPrefManager
    
Run Code Online (Sandbox Code Playgroud)

哪一种没有意义。因为我添加了 playstore-dynamic-feature-support。

小智 5

弄清楚了,

步骤 1. 在应用程序模块中禁用 MlKitInitProvider(阻止应用程序崩溃)

<provider
android:name="com.google.mlkit.common.internal.MlKitInitProvider"
            android:authorities="${applicationId}.mlkitinitprovider"
            tools:node="remove"
            />
Run Code Online (Sandbox Code Playgroud)

步骤 2. 构建 apk 并打开应用程序清单,找到应用程序中使用的所有 MLKit Registrars。步骤3.将找到的所有添加到一个ComponentRegistrar数组ArrayList中;步骤4、在动态特征库中,调用MlKitContext.initialize(context, arr); (在 getProvider 中,服务提供者)在使用 mlkit 功能之前;

此外,仅在应用程序模块中使用这些 mlkit 依赖项(用于拆分安装) api 组:'com.google.mlkit',名称:'common',版本:'17.5.0' api 组:'com.google.mlkit',名称:'playstore-dynamic-feature-support',版本:'16.0.0-beta1'


san*_*oli 5

如果有人仍然无法弄清楚使用 @Shane Gallagher 的答案,我将详细说明步骤:首先如上所述,在应用程序模块中添加 Provider 以禁用 MlKit 初始化:

<provider
        android:name="com.google.mlkit.common.internal.MlKitInitProvider"
        android:authorities="${applicationId}.mlkitinitprovider"
        tools:node="remove"/>
Run Code Online (Sandbox Code Playgroud)

接下来构建 apk 并打开合并的清单。查找您的应用程序中使用的所有组件注册器。打开 AndroidManifest.xml 后,您可以通过单击 Android Studio 左下角的合并清单文本来打开合并清单

参考图片

接下来,在动态功能模块中,根据应用程序中使用的注册商添加以下代码

val registrars = listOf(CommonComponentRegistrar(), VisionCommonRegistrar(), BarcodeRegistrar())
MlKitContext.initialize(this, registrars)
Run Code Online (Sandbox Code Playgroud)