Can I control the <supports-screen> setting of an AndroidManifest.xml file from my Cordova config.xml?

时间:2015-05-12 22:30:50

标签: android cordova build android-manifest

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:

if(firstDropdownNames != null || secondDropdownNames != null){
  for(int i=0; i < Math.max(firstDropdownNames.length,secondDropdownNames.length); i++)
    {
        if(secondDropdownNames.length> firstDropdownNames.length){
           int j= secondDropdownNames.length - firstDropdownNames.length;
            if(i==j){
            String firstDropdownId = null;
            firstDropdownNames[i] = null;
            String secondDropdownId = service.getSecondId(secondDropdownNames[i]);
            Pair pair = new Pair(ProjectId, firstDropdownId, firstDropdownNames[i], secondDropdownId, secondDropdownNames[i]);
            service.save(pair);
           }
        }
       else if(firstDropdownNames.length> secondDropdownNames.length){
           int j= firstDropdownNames.length - secondDropdownNames.length;
            if(i==j){
            String secondDropdownId = null;
            secondDropdownNames[i] = null;
            String firstDropdownId = service.getFirstId(firstDropdownNames[i]);
            Pair pair = new Pair (ProjectId, firstDropdownId, firstDropdownNames[i], secondDropdownId, secondDropdownNames[i]);
            service.save(pair);
           }
        }
        else{
          String firstDropdownId = service.getFirstId(firstDropdownNames[i]);
           String secondDropdownId = service.getSecondId(secondDropdownNames[i]);
           Pair pair = new Pair (ProjectId, firstDropdownId, firstDropdownNames[i], secondDropdownId, secondDropdownNames[i]);
          service.save(pair);
    }
    response.setMessage("Saved successfully");
  }

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:

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

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.

2 个答案:

答案 0 :(得分:11)

this change in the latest cordova > 6.3 versions以来,我们应该能够使用新的edit-config标记来编辑Android Manifest.mf文件,如下所示:

<edit-config file="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>

此外,您还需要将xmlns:android="http://schemas.android.com/apk/res/android"添加到config.xml中的widget元素。

更多信息therethere

答案 1 :(得分:3)

据我所知,钩子是这样做的方法。 yeoman标志性框架生成器有一个很好的例子,它可以采用许多特定于android的标签并将它们复制到生成的config.xml。请参阅此this file here中的slick ionic generator

代码(https://github.com/diegonetto/generator-ionic/blob/master/templates/hooks/after_prepare/update_platform_config.js)中的Config.xml示例:

<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文件夹自动运行钩子,这个特殊的钩子将驻留在hooks / after_prepare中,或者在配置中作为<hook type="after_prepare" src="path/to/file/update_platform_config.js" />

有关钩子的更多文档可以在钩子自述文件中找到:http://cordova.apache.org/docs/en/dev/guide/appdev/hooks/index.html#Hooks%20Guide

编辑:更新生成器和cordova文档的git存储库。