How to enable the move to sd card feature in a cordova app for Android

How to enable the move to sd card feature in a cordova app for Android

Most Android applications should enable the ability for users to choose the application install location. I mean, not anyone can buy a last generation device with 64GB of internal storage. Some of us need to buy instead an sd card an expand the size that our device supports.

However, for some applications, this feature doesn’t make as much sense. For example, if you have an app widget, live wallpaper, or other sort of service or utility application (such as a file manager), your application won’t be of any use if the SD card is removed. Anyway, in this case you're looking how to enable this feature, that means you know what you're trying to do isn't?.

Requirements

We need to modify the AndroidManifest.xml file,  however we will not do it manually. This is not a good practice as everytime you build your project, your changes will be lost.

Instead we are going to use the cordova-custom-config plugin, which allow you to modify the AndroidManifest.xml file from your config.xml file without problems. To install the plugin execute the following command in your command line :

cordova plugin add cordova-custom-config
# Or phonegap
phonegap plugin add cordova-custom-config

Read more about the plugin here in the official repository here.

Implementation

Normally, for every default cordova application this feature is disabled :

Cordova move to sd disabled

Fortunately, to enable this feature you need add only a property to the AndroidManifest.xml file. Using cordova-custom-config plugin, which allow you to use custom config blocks in your config.xml file of your cordova app, you need to add the following line in the config.xml file inside the <platform ="android"> tag :

<!-- Note that needs to be inside the android config -->
<platform name="android">
   <!-- Modify the android-manifest block and add the new property android:installLocation with value auto !-->
   <preference name="android-manifest/@android:installLocation" value="auto" />
</platform>

The value is set to "auto" so that the user can decide where to install the application. If this value is not present at all, the default behavior is to only allow installation to internal storage and not to give the user a choice. The other possible values to use are "internalOnly" which is the same as the behavior without this setting, and "preferExternal", which will install the application on the SD card first, if it’s available and there is available storage.

Finally try to build your application using cordova run android. The feature should be now enabled and your user we'll be able to move your app to the sd card.

Move to sd enabled cordova

Information

Note that with the introduction of Android 6.0 Marshmallow, these feature is not available anymore, so don't worry if you are testing in a Galaxy device and it doesn't works as there's nothing to in this case.

The build target, however, needs to be updated to at least API Level 8 (Android 2.2), otherwise you’ll get an error like error: No resource identifier found for attribute 'installLocation' in package 'android’.

In some Android 6.0 Marshmallow devices (Nexus), the users are able to move it easily. Have fun

Become a more social person