Home > Back-end >  Accessing assets of main application inside package
Accessing assets of main application inside package

Time:12-16

i'm currently trying to develop a package for a Flutter App, with Kotlin. My issue is that I need to provide the package with a config file, which should only be defined inside the main App. Since the config differs for the Dev and Prod environment, the app should pass through the path of the File via the Method Channel. The problem is that the package isn't able to access the assets folder of the calling application. Path: "assets/config.json" (the root being the main application)

Steps I already tried:

  1. Creating the file inside the res/raw & accessing the config file through a ressource id -> Kotlin gives me an "Unresolved reference" error, unless I create the file inside the packages res/raw

  2. Instead of passing through the path, I tried passing through the content of the config & writing it into an empty temporary file. The code in Kotlin like this:

        val config = File(applicationContext.filesDir,"config.json")
        config.writeText(configContent)
    

-> This works, but it seems like a weird solution to the problem.

please let me know if I need to provide further information & thank you in advance!

edit:

The Java Method that is called during initialisation:

public static void createMultipleAccountPublicClientApplication(@NonNull final Context context,
                                                                @NonNull final File configFile,
                                                                @NonNull final IMultipleAccountApplicationCreatedListener listener)

CodePudding user response:

Flutter assets aren't files - they are packaged up and only available through the rootBundle. So, if you want to make a file from a text asset, someone has to load the asset and write it to a file.

As your plugin user will be in charge of the asset, they will have to do the first part (and will end up with a String). The question arises of who should do the writing.

You could make the plugin user use path_provider to find the temporary directory and write it there and then pass you the file path. Eventually, down in the Java, you new File(theTempFilePath). Or they could pass the string to the Dart half of your plugin and you create the temp file in the same way.

It's probably more convenient if they pass your plugin the string, you pass that to the native side and have the native side create a temporary file and write the string there. (BTW, I assume we are talking about this config file: https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-configuration#how-to-use-a-configuration-file )

See this answer for creating temporary files: Creating temporary files in Android

Note that there's actually no reason that your plugin user then needs to use an asset. They could, instead, just hard code the string in their code if the configuration never really changes.

There's an argument that as this is a JSON configuration file, you may not want to bother your user with the details of this JSON configuration file. You may want to default it in your Dart code (why not hard code it as a string, as above, if it never really changes) and then provide some methods to override particular values like the client id and the redirect uri, which may be the only things that users ever change in practice. So rather than making them supply a complete JSON file, they just give you those two strings and you plonk them into your default JSON. Maybe a version 2 feature :)

  • Related