Add android:name=“something” to AndroidManifest.xm

2019-01-22 20:24发布

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

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"

How can do that?

Thank you

4条回答
SAY GOODBYE
2楼-- · 2019-01-22 20:36

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

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

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

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);
        })
      }
    });
  }


};
查看更多
Ridiculous、
3楼-- · 2019-01-22 20:37

The simplest and the latest(cordova version 8.1.2) way to do it to use edit-config tag like below.

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

In similar way you can edit other configurations as well.

Hope it will be helpful!

查看更多
Luminary・发光体
4楼-- · 2019-01-22 20:43

I think I had the same problem as you. I found this in the cordova documentation.

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

If you search the title "config-file Element" you will find an example:

<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>
查看更多
贼婆χ
5楼-- · 2019-01-22 21:00

Indeed, as jlreymendez mentioned, the right way is this:

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

Also note that modifications will revert if you remove the plugin, what will not happen with the hook trick.

查看更多
登录 后发表回答