Trying to update AndroidManifest.xml to stop GPS being required

I have now resolved this a different way using a hook script as follows, I am sure there is probably a nicer way of doing this if anyone has any suggestions or improvements to my script?

In my config.xml I added the following within the “Android” platform:

<platform name="android">
   .....
   <hook src="scripts/set-gps-not-required.js" type="after_prepare" />
   .....
</platform>

I then created a folder in the root of my project called “scripts” and added the following file named “set-gps-not-required.js”:

#!/usr/bin/env node

module.exports = function(context) {

  var fs = require('fs'),
      path = require('path');

  var platformRoot = path.join(context.opts.projectRoot, 'platforms/android');
  var manifestFile = path.join(platformRoot, 'app/src/main/AndroidManifest.xml');

  console.log("Platform ROOT: " + platformRoot);
  console.log("Manifest file: " + manifestFile);

  // If manifest file exists
  if (fs.existsSync(manifestFile)) {
    console.log("Manifest file exists");

    // Read manifest file
    fs.readFile(manifestFile, 'utf8', function (err, data) {
      if (err) {
        throw new Error('Unable to find AndroidManifest.xml: ' + err);
      }

      // Remove any current GPS settings
      data = data.replace(/<uses-feature android:name="android.hardware.location.gps" \/>/g, '');
      data = data.replace(/<uses-feature android:name="android.hardware.location.gps" android:required="false" \/>/g, '');

      // Add GPS uses-feature (setting to required false)
      data = data.replace("</manifest>", 
        '\t<uses-feature android:name="android.hardware.location.gps" android:required="false" \/>\n</manifest>');

      // Replace manifest file with updated version
      if(data){
        fs.writeFile(manifestFile, data, 'utf8', function (err) {
          if (err) throw new Error('Unable to write AndroidManifest.xml: ' + err);
        })
      }
    });
  } else {
    console.log("Manifest file DOES NOT exist");
  }
};
3 Likes