【问题标题】:Can I control the <supports-screen> setting of an AndroidManifest.xml file from my Cordova config.xml?我可以从我的 Cordova config.xml 控制 AndroidManifest.xml 文件的 <supports-screen> 设置吗?
【发布时间】:2015-05-12 22:30:49
【问题描述】:

从 Cordova CLI 进行构建时,我希望能够控制 AndroidManifest.xml 文件中的“supports-screens”元素。

具体来说,我想在 AndroidManifest.xml 中控制以下元素:

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

理想情况下,我希望 Cordova config.xml 文件中有一个可用的设置,可以让我直接控制支持的屏幕尺寸。

我尝试过使用 config.xml 设置,但无济于事:

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

我知道我可以在我的源代码管理中存储一个自定义的 AndroidManfiest.xml 文件,然后使用 Cordova 钩子简单地复制它,但这样做感觉有点笨拙,我担心未来对 config.xml 的调整然后文件可能不会进入 AndroidManifest.xml,因为我们忘记了在 after_prepare 挂钩期间我们正在覆盖生成的文件。

使用 Cordova CLI 可以实现我的要求吗?如果是这样,将不胜感激 config.xml 的示例来实现这一点。

【问题讨论】:

  • I recognize that I can store a customized AndroidManfiest.xml file in my source control and simply copy it around using a Cordova hook 据我所知,这是做到这一点的方法。使用挂钩将自定义标签从 config.xml 中拉出。我在这里给出了一个用于更改 android 主题的糟糕示例:stackoverflow.com/questions/29490925/…。更好的是,请查看 github.com/djett41/generator-ionic/blob/master/templates/hooks/…,它可能已经满足您的需求。
  • 嗯 - 操作生成的 AndroidManifest.xml 与用我在源代码中的一个覆盖它是一个非常吸引人的选择;至少这样我就不必担心破坏可能由 config.xml 文件更改引入的新设置。那个离子 after_prepare 钩子看起来很棒。你应该把你的评论变成一个答案——你的解决方案对我有用。

标签: android cordova build android-manifest


【解决方案1】:

由于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 中的小部件元素。

更多信息therethere

【讨论】:

  • 这正是我想要的!
  • target 应该是 /manifest/supports-screens。我还需要将xmlns:android="http://schemas.android.com/apk/res/android" 添加到config.xml 中的小部件元素。之后它就像一个魅力!
  • 谢谢@Hjalmar,我会根据您的意见编辑我的答案。
  • 从 Cordova Android 7.0.0 开始,重大的重大变化;根据cordova.apache.org/announcements/2017/12/04/…,文件参数需要指向 file="app/src/main/AndroidManifest.xml" 而不是 file="AndroidManifest.xml"
【解决方案2】:

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

来自代码 (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 文件夹中自动运行,这个特定的 hooks 将驻留在 hooks/after_prepare 中,或者在配置中作为 &lt;hook type="after_prepare" src="path/to/file/update_platform_config.js" /&gt;

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

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

【讨论】:

  • 示例代码看起来是一种合理的方法。但目前,它不起作用。见this question。您能否提出其他解决方案?
  • 对我不起作用。它添加了另一个 标签,而不是替换旧标签。
猜你喜欢
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 2012-06-10
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
相关资源
最近更新 更多