背景
项目中使用的flutter_webview不支持图片上传功能,于是找到一个替换插件flutter_webview_plugin,引入项目后运行报错,flutter_webview_plugin和install_plugin插件冲突了
Attribute provider#androidx.core.content.FileProvider@authorities value=(com.ybm100.app.heye.store.test.fileProvider.install) from [:install_plugin] AndroidManifest.xml:12:13-72
is also present at [:flutter_webview_plugin] AndroidManifest.xml:11:13-64 value=(com.ybm100.app.heye.store.test.fileprovider).
Suggestion: add 'tools:replace="android:authorities"' to <provider> element at AndroidManifest.xml:10:9-18:20 to override.
Attribute meta-data#android.support.FILE_PROVIDER_PATHS@resource value=(@xml/provider_install_paths) from [:flutter_bugly] AndroidManifest.xml:27:17-55
is also present at [:flutter_webview_plugin] AndroidManifest.xml:17:17-50 value=(@xml/filepaths).
Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml to override.
查看install_plugin源码相关配置
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider.install"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_install_paths"/>
</provider>
</application>
查看flutter_webview_plugin源码相关配置
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
原因:
由于provider的name相同,authorities和resource都不一致,所以导致冲突。
如果直接指定某一个provide使用tools:replace="android:authorities,android:resource"会导致两个插件某一个不可用。
解决方案:
1.自定义provider
public class MyFileProvider extends androidx.core.content.FileProvider {
}
这里刚开始我使用的kotlin代码,运行代码直接导致程序崩溃,报错not found MyFileProvider,查看apk的class.dex文件也没有MyFileProvider class,很奇怪,换成java的代码就没问题了
class MyFileProvider : FileProvider()
2.manifest设置
<!--重写install_plugin provider -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider.install"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
tools:replace="android:resource"
android:resource="@xml/provider_install_paths"/>
</provider>
<!--重新flutter_webview_plugin provider -->
<provider
android:name=".MyFileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
@xml/filepaths和@xml/provider_install_paths是从两个插件项目中复制出来的
运行一下,ok,两个功能都兼容了。