Android 动态替换文件_paths.xml中的applicationId

Android 动态替换文件_paths.xml中的applicationId,android,android-studio,gradle,Android,Android Studio,Gradle,我已使用以下res/xml/file\u path.xml设置了一个文件提供程序: <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="suggestions" path="Android/data/com.example.app.dev/files/suggestions" /> </paths> 问题是,我有许多产品风格

我已使用以下
res/xml/file\u path.xml
设置了一个文件提供程序:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="suggestions" path="Android/data/com.example.app.dev/files/suggestions" />
</paths>


问题是,我有许多产品风格可以改变
applicationId
。是否有任何方法可以在不为每种产品创建文件路径的情况下用适当的applicationId替换该值?像这样替换标签
Android/data/{appId}/files/suggestions
?或者甚至使用相对路径。。。(我尝试了所有方法,但只有完整路径有效)。

我通过使用Gradle在build.Gradle文件中动态创建字符串资源值来表示文件路径,从而解决了这个问题:

defaultConfig {
    applicationId "com.example.myapp"
    ...
    resValue 'string', 'images_file_path', "Android/data/" + applicationId + "/files/Pictures"
}
然后,您可以在
res/xml/file\u path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" android:path="@string/images_file_path" />
</paths>

您根本不需要在
文件\u path.xml
中使用applicationId。只需将
外部路径
子句替换为:

<external-files-path name="my_images" path="Pictures" />

这将需要24.0.0或更高版本的支持库


如果将
android:
namespace放入,则从中复制的解决方案。

FileProvider将不会读取
path
属性的值。以下是编写
path=“Android/data/com.example.myapp/files/Pictures”
时URI的外观:content://com.example.myapp.photos_provider/my_images/my_file.jpg. 这是正确的。但如果您编写
android:path=“android/data/com.example.myapp/files/Pictures”
(或使用类似于您答案中的参考资料中的字符串),您会得到:content://com.example.myapp.photos_provider/my_images/Android/data/com.example.myapp/files/Pictures/my_file.jpg. 所以使用FileProvider没有任何好处。android:path在这里不起作用。它需要包含路径属性。