How to create APK Expansion Files for assets within flutter - flutter

I have created a flutter app with video assets which are about 450mb in size. I have published the app successfully in the Apple appstore. But Google Play does not accept my APK as it is over the limit of 200mb. I tried to go with the approach of creating APK expansion files as the recommended workflow. I have read all available Android documention about expansion files but I still can not figure out how to implement them with flutter.
How do you implement and access assets within APK expansion files in a flutter app?

You're going to have a little bit of fun with this. Basically, until someone implements a plugin to access APK expansion files, you're going to have to write the java code to connect up to Flutter.
It's not prohibitively difficult, it just means that you're going to have to learn about Platform Channels and write a bit of native android code. The documentation probably does a better job of explaining platform channels than I do, but basically the easiest way is to use a MethodChannel to pass data from dart to native and vice-versa.
What you'd do to start is set up a method channel to initiate this process and call it with something like getObbFolder.
On the android side the first thing you need to do is make sure your app actually has the files downloaded. According to the android documentation, you can't guarantee that they will have been so you need to write the logic to download them. I'd recommend using the Download Library they provide as there's all sorts of things to worry about like the device running out of storage, network connectivity, showing progress, etc. I think the documentation for that is moderately straight forward (and if you have issues I'd recommend asking a new question specific to it.
Once you've done that, you need to get the path to the file, and request permission to read it if needed. Some android versions and some devices in other versions (it sounds like it's a bit of a crapshoot to be honest), you need permissions to read the file, while in others you don't. So it's best to just try and ask for permission if trying fails.
To get the directory it's saved in use context.getObbDir()
Then this one way to do it from the android docs:
File obb = new File(obb_filename);
boolean open_failed = false;
try {
BufferedReader br = new BufferedReader(new FileReader(obb));
open_failed = false;
ReadObbFile(br);
} catch (IOException e) {
open_failed = true;
}
if (open_failed) {
// request READ_EXTERNAL_STORAGE permission before reading OBB file
ReadObbFileWithPermission();
}
And for the versions that don't do runtime permission checking add this to your application manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Android doesn't recommend always asking for the permission since sometimes the Obb folder is exempted from needing the READ_EXTERNAL_STORAGE permission.
Now you have two options. One is that you could pass that path back to flutter, and then use flutter's file reading to do something with the data, if the file is something you can directly read. If it's not, you could unpack it either using flutter or using java/kotlin, and then pass back the path to the unpacked files.
If you do choose to unpack the file from android you should do it to one of the directories flutter knows about (with the path_provider plugin for example), or write to wherever you want (and have permission to) and then simply pass the path back to flutter.
Hope that helps!

Related

how to bundle flutter desktop assets into a single file for production build

after building the flutter project for desktop, flutter copies all asset files inside the assets directory into the build/flutter_assets/assets which are easily accessible and modifiable (which is not secure at all!), user can change the whole images easily and do some kind of modding the app with no experience required! or even in the worst situation with modifying the AssetManifest he could change the runtime dynamic libs with the malicious libs and get a hook from the app.
most modern frameworks/languages have the option to embed the resources inside a single file (that makes it harder to modify the files / repack the resource file) for example C#(WPF/Win32) embeds the resources in the resource Resource section or even Electron (Javascript) embed the whole resources into a file (it's easy but better than nothing)
I didn't find any similar behavior in Flutter Desktop (Windows/Mac/Linux) is there any alternative for flutter?
For now [12/12/22] it seems flutter desktop won't use resources to bundle the resources into a single file.
So I've created two separate libraries to achieve this
https://github.com/kooroshh/bundler that compresses the whole given assets into a single file and encrypts it with AES-128
https://github.com/kooroshh/flutter_bundler that reads the bundle file from and decrypts it into the memory that can be used with Image.memory or Direct file access using Virtual Files.
I hope an official solution gets implemented in the flutter framework.

Native file path

I have started a development of a music player. I made some additional kotlin logic to retrieve all audio files meta data on device from the Android as a list of meta data. I'm trying to understand how a native path should look like for just_audio to understand. My current implementation uses a deprecated key - MediaStore.Audio.Media.DATA for contentResolver which returns a path like this /storage/emulated/0/Download/scifri20181123-episode.mp3. Can this path be used by the plugin or I need to find another way to retrieve the path in different format?
The same question is valid for the album art.
Yes, if it's a valid file path, then just_audio will be able to open it. You could pass this into setFilePath:
var filePath = '/storage/emulated/....../foo.mp3';
await player.setFilePath(filePath);
This is a convenience method for:
setAudioSource(AudioSource.uri(Uri.file(filePath)));
where Uri.file(filePath) will create a Uri instance containing something like file:///storage/emulated/..... under the hood.
Note that if your app tries to access any file outside of its own app data/cache directories, it also requires permission to do so, and there are other Flutter packages available to help you with this.
Note also that there is also an existing flutter package using the content resolver API called flutter_audio_query, so if it contains what you need, you might not need to write your own Kotlin code.

How to open/find the file created by path_provider?

I'm a beginner at flutter and dart and had some issues with file handling using flutter.
I'm trying to store data locally in text files for my flutter application, and came across the standard tutorial on flutter's website which used path_provider and dart:io. It had instructions on getting the path and reading and writing to a file, however this didn't seem to work for files I added to the project. The only way to store to files was to first write using the inbuilt functions, then reading this newly created file. I can't add my own file and read it using flutter.
I also can't open the file created by flutter as I don't know where its stored. The path given by getApplicationDocumentsDirectory() returns the path /data/user/0/com.example.daily_challenges/app_flutter, and I don't know where to find this in my project.
In summary: How do I read a pre-existing file and how do I view the file created automatically by flutter.
Thanks :)
You can access the files from a physical device or simulator through Device File Explorer in Android Studio. Follow this guide
You won't be able to access getApplicationDocumentsDirectory().
If you are using android device, you can try to store it in getExternalStorageDirectory(). There's no equivalent in IOS though.
If you are running in a physical device. Open Device File Explorer in Android Studio and you can find the file under
data/data/your app package/app_flutter/fileName.txt
For example,
data/data/com.example.file_example/app_flutter/example.txt
And if you want to read the pre-existing file, you not need to anything specific, if you give the same file name, if not exist, it will create one, otherwise it will return the old one.
final File file = File(filePath);
file.writeAsStringSync('${text}', mode: FileMode.append);
For write, you consider using FileMode if you want to append text to the existing file. Or else by default overwrite will happen.
For read, you can consider this
final File file = File(filePath);
String text = await file.readAsString();
Just use Device File Explorer from Android Studio.
But the weird thing, path_provider gives you path like /data/user/0/your_app_id/..., but in fact all files are located in /data/data/your_app_id/..., as mentioned in previous answer.

Is it possible to download and execute an uncompiled dart file (Flutter)

I am trying to make an extension based app, where you can download extensions to the app to add features/widgets. Could I somehow run an uncompiled dart file downloaded from a server that stores the .dart files?
download(file.dart)
compile(file.dart)
storeInPersistentDirectory()
if (dartFile.exists) {
ClassFromDownloadedFile.sayHello()
}
The aim is to decrease the app size by storing all of the data related to an extension in the dart file, (classes, json and images in the form of a string...). Users might want to download features, while some don’t need them, in which case they shouldn’t need to download a large app with many features that they will never use, so the initial app size remains small.
Thank you for anyone who knows anything relating to this in advance!
No, it is not possible. You can download a 'resource' such as image/video/mp3 etc. You can even download the dart file but can't compile and execute.
there is a concept in dart and other modern languages called reflection(mirroring).
Reflection allows us to examine and modify the structure and behavior
of a program at runtime.
search about it and check the below links for more information.
dart document
Understanding Reflection and Annotations in Dart
I recommend searching for RMI too.

Flutter: Reading and Writing User Visible Files

I am looking for something along the lines of getExternalStorageDirectory that is available in both iOS and android. The purpose of this directory is to save and retrieve .json configuration files that would be later read back by the application. However the normal getApplicationDocumentsDirectory isn't really what I need. The user is likely to want direct access to the saved files to send them to friends, forums, etc.
Workflows might include:
User A emails a config to User B
User uploads a configuration to a forum
User downloads a configuration from a forum
User creates a configuration offline using a json editor
It seems like I must be missing something obvious here as this seems like a common requirement but I have not found a suitable way to do this.
Thanks
Assuming we're speaking of the path_provider plugin, it actually contains a function called getExternalStorageDirectory, though, as the documentation states, iOS prohibits the user from arbitrarily accessing any directory on the device. I would suggest checking for the platform. If on Android, use getExternalStorageDirectory for Android, and if on iOS, use getApplicationDocumentsDirectory and make it visible to the user. More on that here: How to save a text file in external storage in ios using flutter?
Can't think of any other way of achieving this, since iOS is very strict when it comes to file management (or anything else, really).