API 30以下适配Google照片选择器遇ModuleDependencies类缺失问题求助
我希望在API 30以下的Android版本中使用Google照片选择器,因此在AndroidManifest.xml中添加了触发Google Play服务安装回退照片选择器模块的代码,但出现清单中引用的com.google.android.gms.metadata.ModuleDependencies类未找到的错误。
AndroidManifest.xml代码:
<!-- suppress AndroidDomInspection --> <!-- Trigger Google Play services to install the backported photo picker module. --> <service android:name="com.google.android.gms.metadata.ModuleDependencies" android:enabled="false" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.metadata.MODULE_DEPENDENCIES" /> </intent-filter> <meta-data android:name="photopicker_activity:0:required" android:value="" /> </service>
build.gradle.kts依赖配置:
dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) implementation(libs.material) implementation(libs.androidx.constraintlayout) implementation(libs.androidx.navigation.fragment.ktx) implementation(libs.androidx.navigation.ui.ktx) implementation(libs.play.services.base) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) implementation(libs.androidx.activity.ktx) }
已确认模拟器中Google Play服务已激活,且API 30以上版本可正常使用Google照片选择器,需解决类未找到的错误。
补充Google Play服务元数据依赖:
com.google.android.gms.metadata.ModuleDependencies类属于play-services-metadata模块,当前仅引入play-services-base不足以包含该类,需在build.gradle.kts的dependencies块中添加:implementation(libs.play.services.metadata)若项目未在libs.toml中定义该依赖,可直接指定版本引入:
implementation("com.google.android.gms:play-services-metadata:17.0.0")版本号可根据Google Play服务的最新稳定版调整。
同步Gradle项目:添加依赖后,执行Android Studio的「Sync Project with Gradle Files」操作,确保依赖被正确下载并集成到项目中。
保留Manifest配置:原AndroidManifest.xml中的
<service>配置无需修改,它是触发Google Play服务安装回退照片选择器模块的必要声明,类找不到的问题本质是缺少对应依赖库,补充依赖后即可解决。
内容的提问来源于stack exchange,提问作者seunghee

