问题
I want set couple of settings not to be required for my android app so Google' play store will recognize it also useful for tablets. I need add these two lines to my AndroidManifest.xml:
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />
Since I use PhoneGap build to compile my app, I need somehow add them to the config.xml file, so PhoneGap will add them to the right place during build process (I guess) ... But no success! Is there any specific syntax I need to use to add uses-feature through config.xml file?
Note that I did almost same thing for setting minSdkVersion by adding line below to my config.xml file, and after build I have it in my manifest file:
<preference name="android-minSdkVersion" value="11" />
回答1:
Found the solution! Based on PhoneGap Documentation - Config File Elements, just need to add something like this to my 'config.xml' file:
<gap:config-file platform="android" parent="/manifest" mode="add">
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />
</gap:config-file>
Adding gap namespace to widget node, is also needed:
xmlns:gap="http://phonegap.com/ns/1.0"
回答2:
<config-file>
looks super useful for overriding.... alas it seems that this is a PhoneGap only feature. For Cordova you will probably need to use hooks.
A good example can be found HERE
Ive modified the script to add the features I needed (I need GPS and Camera to be optional features).
module.exports = function (context) {
var fs = context.requireCordovaModule('fs'),
path = context.requireCordovaModule('path');
var platformRoot = path.join(context.opts.projectRoot, 'platforms/android');
var manifestFile = path.join(platformRoot, 'AndroidManifest.xml');
if (fs.existsSync(manifestFile)) {
fs.readFile(manifestFile, 'utf8', function (err, data) {
if (err) {
throw new Error('Unable to find AndroidManifest.xml: ' + err);
}
var insertAt = data.indexOf("</manifest>");
var optionalFeatures = "<uses-feature android:name=\"android.hardware.LOCATION\" android:required=\"false\"/><uses-feature android:name=\"android.hardware.location.GPS\" android:required=\"false\"/><uses-feature android:name=\"android.hardware.camera\" android:required=\"false\"/><uses-feature android:name=\"android.hardware.camera.autofocus\" android:required=\"false\"/>";
var result = data.slice(0, insertAt) + optionalFeatures + data.slice(insertAt);
fs.writeFile(manifestFile, result, 'utf8', function (err) {
if (err) throw new Error('Unable to write into AndroidManifest.xml: ' + err);
})
});
}
};
Then set the hook in config.xml
<hook type="after_build" src="scripts/afterBuild.js" />
Hope it helps
来源:https://stackoverflow.com/questions/29434321/phonegap-make-android-feature-optional