Android 13 is the latest major Android version
released this year and it comes with some behavioral changes that will force us to make
some adaptations to our app to ensure it works properly. We already have at our disposal an informative
document about these changes with links of interest to understand in more detail what
they entail. However, in this video I want to take a more
pragmatic approach through a simple example. What I am going to present to you is an application
running on Android 12. We are going to see what it does and what
happens when we run it on Android 13. Once we have seen what happens, we will make
the appropriate adjustments so that it works correctly on the latest version of Android. But first, a brief note: In this video I am
going to focus basically on the changes related to reading files on disk and the new notifications
permission, since they are functionalities that the vast majority of applications have. You can see the rest of the changes in the
official documentation, you can find the link in the video description. Our app will consist of the following: A floating button that when pressed will ask
us for permission to read files. Once the permission is granted, we can choose
a photo and it will be painted on the screen. Additionally, a notification will be displayed
indicating that the photo has been uploaded. correctly We will use file_picker to be able to select
files and permission_handler to request the system permissions. To show notifications we will use flutter_local_notifications. Let's quickly see the code that compose this
application: We first initialize the notifications using
the flutter_local_notifications plugin. We then launch our app encapsulated in the
App widget. In our main screen we have a state variable
called _filepath, which will store the path to the chosen file. When the user clicks the button, the _pickFile()
method will be called, which will take care of requesting permission to read files and
will allow us to choose a file of type image. Once selected, the status is updated and the
picture is displayed on the screen, and the _postNotification() method is called to publish
the informative notification. This code snippet works correctly in versions
before Android 13. If we run this app on a device with Android
13 we will see that it works more or less the same as before, except that now, once
the image is selected, it asks us for permission to show notifications. Although the app continues to work, we have
the problem that it is the system that is choosing the moment to ask for permission
to show notifications, when ideally we should be the ones who decide when it is requested
and if any explanatory text must be shown before. The problem is aggravated if we target Android
13. Now when pressing the floating button we are
not even asked for permission to read files. Why is this happening? Android 13 incorporates a granular permission
system when it comes to reading files. While before it was enough to have the READ_EXTERNAL_STORAGE
permission granted to read any file from external storage, now we will have to specifically
define what type of files we intend to read. In this app we want to read images, so we
will ask for the READ_MEDIA_IMAGES permission. If we wanted to read videos we would ask for
READ_MEDIA_VIDEO and if we wanted audio files we would have READ_MEDIA_AUDIO. Let's make the following adjustments to the
AndroidManifest file to resolve this issue: Through this change we are indicating to the
system that for Android 12 or lower we continue to depend on READ_EXTERNAL_STORAGE, while
from Android 13 we will use the new granular permissions system. Other change that Android 13 brings is the
need to have explicit permission from the user to display notifications, as has been
the case in iOS for a long time. Let's declare that permission in AndroidManifest. Install device_info_plus plugin. This plugin will help us to know what version
of Android is running, and consequently, we will know what set of permissions to request. Now, modify pickFile() as follows. If the version is Android 12 or lower, we
ask for the storage permission, otherwise; we ask for the new image and notification
permission. And with this last change we can now choose
an image, it is displayed on the screen and the informative notification is also shown. In this video I have made a simple review
of two of the behavioral changes that Android 13 incorporates: the new granular permission
system for reading files and the new permission to be able to show notifications. In addition to these two, I recommend that
you read the official documentation to see these changes in more detail as well as to
see the rest of the changes that this version of Android incorporates since they may affect
the operation of your application. Please like the video if you found it usefuly
and consider subscribing for more content related to Flutter and app development. Thank you for watching, and have a nice rest
of your day.