Dynamically replace applicationId in file_paths.xml

I installed 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 that I have many products that change applicationId . Is there a way to replace this value with the appropriate application without creating a file path for each product? How to replace a tag like this Android/data/{appId}/files/suggestions ? Or even using the relative path ... (I tried everything, but only this full path worked).

+8
android android-studio gradle
source share
2 answers

I managed to solve this problem 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 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> 
+8
source share

You do not need to use applicationId in your file_paths.xml at file_paths.xml . Just replace the external_path clause with:

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

This will require 24.0.0 or higher support libraries.

Solution copied from https://stackoverflow.com> .

+4
source share

All Articles