Select to view content in your preferred language

AppStudio and Android 10 Scoped Storage Changes

2117
0
07-10-2020 02:48 PM
ErwinSoekianto
Esri Alum
0 0 2,117

Working with the file system is an important part of developing any AppStudio app. For an Android app, up until targeting Android 10, when you gave an app storage permission, it could access any file on the device. When an app targets Android 10, Google enforces the concept of Scoped Storage, which enhances user control and privacy while cutting back the file clutter that removed apps leave behind. 

Google is changing the rules for submissions to Google Play. Starting on August 3, 2020, all new apps submitted to the Google Play store must be built for Android 10 by targeting SDK 29. And starting on November 2, 2020, all new apps and existing apps (app updates) must target Android 10 by targeting SDK 29. The upcoming AppStudio 4.3 and AppStudio 4.3 Beta will have default Android targetSdkVersion set to SDK 29.

This blog discusses exactly what this means and how it may impact users of your app.

How will users be affected? 

Users of your apps will be affected if both of the scenarios are applied,   

  • They have installed an older version of your app and have upgraded to a version of your app built with AppStudio 4.3 (Android targerSdkVersion set to SDK 29), AND,
  • They have unsaved work ( e.g. input, forms, data, surveys, images, databases, etc ) from the older version of your app.

You should advise your users to save their work before installing app updates targeting SDK 29. This may mean submitting their unsaved work online.

Currently, up until targeting SDK 28, when your user uninstalls your app, their work will persist on storage location like /sdcard. After targeting SDK 29, when the user uninstalls your app, data stored with the app will be deleted. 

How you should prepare your apps and users?

The worst-case scenario, after targeting SDK 29, is the app will not be able to access old data from before. 

The primary method of preparation is you should give users ample warning as soon as possible. Tell them that changes are coming and that they should prepare. Also, tell them to save their work online before upgrading their application. Such messaging can mitigate any loss of data better than any app code will do.

The secondary method of preparation is to offer your users in-app mechanisms for access or migration of their data. However, as indicated, Google is closing off access to the older files location and you have limited opportunities to maximize your user’s success with this option. Also, you don’t want to implement migration code that’s stuck in your codebase for, potentially, forever (as you don’t know when the user will conduct the migration).

AppStudio 4.2 and Android 10

AppStudio 4.2 default Android targetSdkVersion set to SDK 28. If you wish to opt-ni and want to target SDK 29, you can edit appinfo.json to change the "targetSdkVersion" property to 29

"deployment": {
 "android": {
 "targetSdkVersion": 29
 }
}‍‍‍‍‍

AppStudio 4.3 and Android Scoped Storage

The upcoming AppStudio 4.3 and AppStudio 4.3 Beta will have default Android targetSdkVersion set to SDK 29. So when your app is built using AppStudio 4.3, it will already target SDK 29 required by Google Play. 

If you wish to opt-out and do not want to target SDK 29, you can edit appinfo.json to change the "targetSdkVersion" property to 28. 

"deployment": {
  "android": {
    "targetSdkVersion": 28
  }
}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If you want to keep the targetSdkVersion at SDK 28 but still want to use the new Android Scoped Storage, you can set the "scopedStorage" property in appinfo.json. This will allow apps to get ready with the changes in AppFramework.userHomePath without incurring losing access to the old location.

"deployment": {
  "android": {
    "scopedStorage" : true
  }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The changes in AppFramework.userHomePath 

For apps targetting SDK 28 and apps setting "scopedStorage" property to false the AppFramework.userHomePath will default to “/sdcard”. For apps targetting SDK 29 or apps setting "scopedStorage" property to true the AppFramework.userHomePath will default to Scope Storage location.

"/storage/emulated/0/Android/data/com.myorg.myawesomeapp/files"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Apps should use AppFramework.userHomePath or ~ to save data using the following code snippet as a guide:

let fileInfo = AppFramework.fileInfo( "~/Data/file.dat" );
let fileFolder = fileInfo.folder();
let data = "sample data";
fileFolder.makeFolder();
fileFolder.writeTextFile( fileInfo.fileName, "sample data" );‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Apps should use AppFramework.userHomePath or ~ to read data using the following code snippet as a guide:

let fileInfo = AppFramework.fileInfo( "~/Data/file.dat" );
let fileFolder = fileInfo.folder();
let data = fileFolder.readTextFile( fileInfo.fileName );‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The above methods are supplied as a guide because:

  • The fileInfo object is a good way to expand userHomePath via the ~ shorthand
  • The fileInfo object contains an easy way to access a FileFolder object ( which is needed to ensure the folder actually is created before writing files within )

Apps effectively using AppFramework.userHomePath or ~ will automatically migrate to the newer location.

When apps target SDK 28 and below (i.e. AppStudio 4.2 and below), we are currently defaulting AppFramework.userHomePath to “/sdcard”.

When apps target SDK 29 and above (i.e. AppStudio 4.3 and above), we will be setting AppFramework.userHomePath to a new location defined by the Scoped Storage. And when uninstalling and re-install, data will be gone.

New AppStudioHomeLocation enumeration

In AppStudio 4.2, we have introduced a new StandardPaths.AppStudioHomeLocation enumeration. To use it,

let userHomePaths = AppFramework.standardPaths.standardLocations( StandardPaths.AppStudioHomeLocation );‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

On Android, this will return a string array containing two values, the first value will contain the new target SDK 29 userHomeLocation location and the second value will contain the target SDK 28 userHomePath.

[ "/storage/emulated/0/Android/data/com.myorg.myawesomeapp/files", "/sdcard" ]

This location could be to check the old SDK 28 location to see if there was old content that we could migrate to the user location.

This could be used to implement data migration, however, only a small group of users will be able to use it to migrate their data effectively. i.e. those users who haven’t upgraded their phone to Android Q, or those users who are running a target SDK 28 version of your app.

AppStudio Player as a distribution tool 

We have made the changes in AppStudio Player 4.3 to target Android 10, and introduced a new feature called Custom Profile so you could better use AppStudio Player as a distribution tool. Using AppStudio Player as a distribution tool for your AppStudio apps would make sure that your apps would continue to work since the changes are done by us in the AppStudio Player side.

If you have any questions regarding this or anything related to AppStudio, please leave your comment below or reach out to us at appstudiofeedback@esri.com. 

  • New to AppStudio? Check out this website to learn more about what AppStudio for ArcGIS can do to you and your organization. 
  • Become an AppStudio for ArcGIS developer! Watch this video on how to sign up for a free trial. 
  • Follow us on Twitter @ArcGISAppStudio  to keep up-to-date on the latest information and let us know about your creations built using AppStudio to be featured in the AppStudio Showcase.
  • The AppStudio team periodically hosts workshops and webinars; please sign up for the AppStudio newsletter if you are interested in information regarding AppStudio events.
About the Author
Product Evangelist for ArcGIS AppStudio