添加 android:name="something";到 AndroidManifest.xml “应用程序"Cordova plugin.xml 中的标记 [英] Add android:name="something" to AndroidManifest.xml "application" tag from Cordova plugin.xml

查看:23
本文介绍了添加 android:name="something";到 AndroidManifest.xml “应用程序"Cordova plugin.xml 中的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I decided to open new question because none of those that are already posted, has a good answer.

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

I need to update AndroidManifest.xml "from plugin.xml", so that the <application> tag has the following property, alongside those it already has:

android:name="mypackage"

怎么做?

谢谢

推荐答案

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

I had the same issue, and I used a Cordova hook to do the work.

首先,编辑您的 config.xml 文件以添加钩子:

First, edit your config.xml file to add the hook:

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

创建一个名为 scripts/android_app_name.js 的文件(将其设置为可执行文件),然后在其中使用搜索/替换功能.它应该看起来像:

Create a file called scripts/android_app_name.js (set it executable), and inside, just use a search/replace function. It should look like:

#!/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);
        })
      }
    });
  }


};

这篇关于添加 android:name="something";到 AndroidManifest.xml “应用程序"Cordova plugin.xml 中的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆