flutter fileProvider 沖突

背景
項目中使用的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,兩個功能都兼容了。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容