Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically replace applicationId in the file_paths.xml

I have set a FileProvider with the following res/xml/file_paths.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>

The problem is, I have many product flavors that change the applicationId. Is there any way to replace that value with the proper applicationId without creating a filepath for each product flavor? Like replacing a tag like this Android/data/{appId}/files/suggestions? Or even using a relative path... (I've tried everything but only this full path worked).

like image 289
Fabricio Avatar asked Aug 30 '16 09:08

Fabricio


1 Answers

I managed to solve this issue by using Gradle to dynamically create a string resource value in the build.gradle file to represent the file path:

defaultConfig {
    applicationId "com.example.myapp"
    ...
    resValue 'string', 'images_file_path', "Android/data/" + applicationId + "/files/Pictures"
}

You can then reference this string resource value in the res/xml/file_paths.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>
like image 180
Gambit Avatar answered Oct 04 '22 09:10

Gambit