将 android:name= "something" 添加到 AndroidManifest.xml "application" 来自 Cordova 插件的标签.xml



我决定提出新问题,因为已经发布的问题都没有好的答案。

我需要更新 AndroidManifest.xml">from plugin.xml">,以便 <application> 标签具有以下属性,以及它已经具有的属性:

android:name="mypackage"

怎么能做到呢?

谢谢

我遇到了同样的问题,我使用 Cordova 钩子来完成这项工作。

首先,编辑config.xml文件以添加挂钩:

<platform name="android">
    <hook type="after_prepare" src="scripts/android_app_name.js" />
</platform>

创建一个名为scripts/android_app_name.js的文件(将其设置为可执行文件(,然后在其中,只需使用搜索/替换功能即可。它应如下所示:

#!/usr/bin/env node
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 appClass = 'YOU_APP_CLASS';
      if (data.indexOf(appClass) == -1) {
        var result = data.replace(/<application/g, '<application android:name="' + appClass + '"');
        fs.writeFile(manifestFile, result, 'utf8', function (err) {
          if (err) throw new Error('Unable to write into AndroidManifest.xml: ' + err);
        })
      }
    });
  }

};

最简单和最新的(cordova 版本 8.1.2(方法是使用如下所示edit-config标签:

    <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
        <application android:name="mypackage" />
    </edit-config>

以类似的方式,您也可以编辑其他配置。

希望对您有所帮助!

事实上,正如jlreymendez所提到的,正确的方法是这样的:

    <edit-config file="AndroidManifest.xml" target="/manifest/application" mode="merge">
      <application android:name="com.mypackage.MyApplication"/>
    </edit-config>

另请注意,如果您删除插件,修改将恢复,钩子技巧不会发生什么。

我想我和你有同样的问题。我在科尔多瓦文档中找到了这个。

https://cordova.apache.org/docs/en/4.0.0/plugin_ref_spec.md.html

如果你搜索标题"配置文件元素",你会发现一个例子:

<config-file target="AndroidManifest.xml" parent="/manifest/application">
    <activity android:name="com.foo.Foo" android:label="@string/app_name">
        <intent-filter>
        </intent-filter>
    </activity>
</config-file>