首页 > 解决方案 > 重建应用程序时,Cordova Android 未将自定义 URL 方案插件中的所有 xml 添加到 AndroidManifest

问题描述

我将插件https://github.com/EddyVerbruggen/Custom-URL-scheme添加到我的 cordova 项目中。

该插件在适用于 Android 的 plugin.xml 文件中包含以下内容:

<config-file target="AndroidManifest.xml" parent="/manifest/application/activity">
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="$URL_SCHEME"/>
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="$ANDROID_SCHEME"
          android:host="$ANDROID_HOST"
          android:pathPrefix="$ANDROID_PATHPREFIX" />
  </intent-filter>
</config-file>

安装插件时,我提供 CUSTOM_URL 变量的值:

cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=appscheme

然后我使用cordova run android --emulator 在模拟器中运行该应用程序。Android Manifest 正确生成,包括两个意图过滤器块:

  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="appscheme"/>
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme=" " android:host=" " android:pathPrefix="/" />
  </intent-filter>

事情按预期工作。

但是,如果我随后对我的项目进行任何更改,并通过再次运行 cordova run android 来重建应用程序,那么 Android Manifest 文件中缺少一个意图过滤器块,并且插件功能不起作用。

  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme=" " android:host=" " android:pathPrefix="/" />
  </intent-filter>

我需要删除插件,然后将其重新添加,以便正确生成 AndroidManifest.xml。

知道为什么它会在重建时删除一个意图过滤器块吗?

谢谢你的帮助!

标签: androidcordovacordova-pluginsandroid-manifest

解决方案


在阅读了这篇文章中接受的解决方案的第一条评论后,我找到了一个解决方案: Cordova Change AndroidManifest using Config.xml file

我需要从我的 config.xml 文件中删除这个:

    <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application/activity[@android:name='MainActivity']">
        <activity android:theme="@style/Full" />
    </edit-config>

而是在项目中使用cordova-custom-config插件,并在config.xml中使用:

<custom-preference name="android-manifest/application/activity/@android:theme" value="@style/Full" />

现在一切都进入了 AndroidManifest.xml 文件。


推荐阅读