12

I would like to be able to control the "supports-screens" element within the AndroidManifest.xml file when doing a build from the Cordova CLI.

Specifically, I'd like to control the following element within AndroidManifest.xml:

    <supports-screens android:anyDensity="true" 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:resizeable="true" 
    android:smallScreens="true" 
    android:xlargeScreens="true" />

Ideally, I'm hoping that there is a setting available within the Cordova config.xml file that would let me directly control the supported screen sizes.

I've tried monkeying around with config.xml settings like the following to no avail:

<platform name="android">
    <supports-screen xlargeScreens="false"/>
</platform>

I recognize that I can store a customized AndroidManfiest.xml file in my source control and simply copy it around using a Cordova hook, but doing so feels a bit clunky, and I'm worried that future tweaks to the config.xml file might then not make it into the AndroidManifest.xml because we forgot that we're overwriting the generated file during an after_prepare hook.

Is what I'm asking possible using the Cordova CLI? If so, a sample of the config.xml to achieve this would be appreciated.

RMD
  • 2,907
  • 30
  • 47
  • 1
    `I recognize that I can store a customized AndroidManfiest.xml file in my source control and simply copy it around using a Cordova hook` As far as I know this is the way to do it. Use a hook to pull custom tags out of the config.xml. I give a kind of poor example here for changing android themes: http://stackoverflow.com/questions/29490925/change-android-theme-from-cordova-config-xml/29495578#29495578. Better yet, check out https://github.com/djett41/generator-ionic/blob/master/templates/hooks/after_prepare/update_platform_config.js it might already do what you are looking for. – laughingpine May 12 '15 at 22:41
  • Hmm - manipulating the generated AndroidManifest.xml vs. overwriting it with one I have in source is a very appealing option; at least that way I don't have to worry about clobbering new settings that may have been introduced by config.xml file changes. That ionic after_prepare hook looks awesome. You should make your comment into an answer - your solution works for me. – RMD May 12 '15 at 23:38

2 Answers2

12

Since this change in the latest cordova > 6.3 versions, we should be able to use the new edit-config tag to edit the Android Manifest.mf file like this:

<!-- For Cordova Android < 7, use file="AndroidManifest.xml" -->
<edit-config file="app/src/main/AndroidManifest.xml" target="/manifest/supports-screens" mode="merge">
   <supports-screens android:resizeable=["true"| "false"]
                     android:smallScreens=["true" | "false"]
                     android:normalScreens=["true" | "false"]
                     android:largeScreens=["true" | "false"]
                     android:xlargeScreens=["true" | "false"]
                     android:anyDensity=["true" | "false"]
                     android:requiresSmallestWidthDp="integer"
                     android:compatibleWidthLimitDp="integer"
                     android:largestWidthLimitDp="integer"/>
</edit-config>

Also you'll need to add xmlns:android="http://schemas.android.com/apk/res/android" to the widget element in config.xml.

More info there and there

user276648
  • 6,018
  • 6
  • 60
  • 86
Żabojad
  • 2,946
  • 2
  • 32
  • 39
  • This is just what I was looking for! – distante Nov 11 '16 at 21:22
  • 3
    `target` should be `/manifest/supports-screens`. Also I needed to add `xmlns:android="http://schemas.android.com/apk/res/android"` to the widget element in config.xml. After that it worked like a charm! – Hjalmar Apr 21 '17 at 15:05
  • 1
    Thank you @Hjalmar, I'll edit my answers with your input. – Żabojad Apr 23 '17 at 07:17
  • 2
    As of Cordova Android 7.0.0, major breaking change; file param needs to point to file="app/src/main/AndroidManifest.xml" and not file="AndroidManifest.xml" as per http://cordova.apache.org/announcements/2017/12/04/cordova-android-7.0.0.html – Palvinder Feb 14 '18 at 16:03
3

As far as I know, hooks are the way to do this. The yeoman iconic framework generator has a great example of this which can take many android specific tags and copy them over to the generated config.xml. See this file here from this slick ionic generator.

Config.xml example from code (https://github.com/diegonetto/generator-ionic/blob/master/templates/hooks/after_prepare/update_platform_config.js):

<config-file target="AndroidManifest.xml" parent="/*>
    <supports-screens
        android:xlargeScreens="false"
        android:largeScreens="false"
        android:smallScreens="false" />
    <uses-permission android:name="android.permission.READ_CONTACTS" android:maxSdkVersion="15" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
</config-file>

Hooks can be automatically run from the hooks folder, and this particular hook would reside in hooks/after_prepare, or in the config as <hook type="after_prepare" src="path/to/file/update_platform_config.js" />

More documentation on hooks can be found in the hooks readme: http://cordova.apache.org/docs/en/dev/guide/appdev/hooks/index.html#Hooks%20Guide

Edit: update git repositories for generator and cordova docs.

laughingpine
  • 1,039
  • 14
  • 20
  • The example code looks like a reasonable approach. But currently, it is not working. See [this question](http://stackoverflow.com/questions/30668889/setting-supports-screens-for-ionic-cordova-app-in-android). Could you suggest any other solutions? – mc9 Dec 15 '15 at 00:09
  • Didn't work for me. It added another < supports-screens > tag, not replacing the old one. – Juangui Jordán Mar 07 '16 at 10:35