科尔多瓦Android插件:config.xml中覆盖由科尔多瓦prepare? [英] Cordova Android Plugin: config.xml overwritten by cordova prepare?

查看:154
本文介绍了科尔多瓦Android插件:config.xml中覆盖由科尔多瓦prepare?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个科尔多瓦插件为Android平台。

I'm developing a Cordova plugin for the Android platform.

如前所述<一个href="http://cordova.apache.org/docs/en/3.1.0/guide_platforms_android_plugin.md.html#Android%20Plugins_plugin_class_mapping">here,我编辑了平台/安卓/ RES / XML / config.xml中文件包含插件的类映射。

As described here, I edited the platforms/android/res/xml/config.xml file to include the plugin's class mapping.

这一切工作完全正常,只是我每次运行时间科尔多瓦prepare机器人这个文件中获取的覆盖说法:

It all works perfectly fine except that every time I run cordova prepare android this file get's overwritten saying:

Generating config.xml from defaults for platform "android"

所以,我有充分的,这是非常恼人的时间来撤消更改。有没有办法告诉科尔多瓦不这样做,或者将类映射别的地方?

So, I have to undo the change every time which is very annoying. Is there a way to tell cordova not to do that or insert the class mapping somewhere else?

推荐答案

您将要创建一个插件包并安装它来解决这个问题。

You will want to create a plugin package and install it to fix this issue.

一个插件包具有存储在此结构中的plugin.xml文件,JS文件和你的本地code:

A plugin package has a plugin.xml file, a JS file and your native code stored in this structure:

PLUGIN_NAME\
 src\
  PLATFORM_NAME\
   PLAFORM_SPECIFIC_FILES
 www\
  PLUGIN_JS_FILE
 plugin.xml

例如:

BackgroundAPI\
 plugin.xml
 src\
  android\
   BackgroundAPI.java
  ios\
   BackgroundAPI.h
   BackgroundAPI.m
 www\
  BackgroundAPI.js

在plugin.xml中定义安装插件时将被放置到config.xml中的项目。确保你的code不会被删除每次运行构建或prepare命令。

In your plugin.xml you define the items that will be placed into config.xml when the plugin is installed. This make sure your code is not removed each time run the build or prepare command.

例如:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
    id="com.dawsonloudon.backgroundapi"
    version="1.0.0">
    <name>backgroundapi</name>
    <description>run external api calls on a background thread</description>
    <license>MIT</license>

    <js-module src="www/BackgroundAPI.js" name="BackgroundAPI">
        <clobbers target="BackgroundAPI" />
    </js-module>

    <platform name="ios">
        <config-file target="config.xml" parent="/*">
            <feature name="BackgroundAPI">
                <param name="ios-package" value="BackgroundAPI" />
            </feature>
        </config-file>

        <header-file src="src/ios/BackgroundAPI.h" />
        <source-file src="src/ios/BackgroundAPI.m" />
    </platform>

    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="BackgroundAPI" >
                <param name="android-package" value="com.dawsonloudon.backgroundapi.BackgroundAPI"/>
            </feature>
        </config-file>

        <config-file target="AndroidManifest.xml" parent="/*">
            <uses-permission android:name="android.permission.INTERNET" />
        </config-file>
        <source-file src="src/android/BackgroundAPI.java" target-dir="src/com/dawsonloudon/backgroundapi" />
    </platform>

</plugin>

一旦这一切都建立在从项目中一个单独的目录,进入到项目目录,然后运行:

Once you have this all built out in a separate directory from your project, go to your project directory and run:

cordova plugin add /PATH/TO/YOUR/PLUGIN

现在已经安装了插件,你将有一个plugins文件夹在您的项目,所有的config.xml文件的编辑将永远是present。

Now that the plugin is installed, you will have a plugins folder in your project and all of your config.xml edits will always be present.

要编辑插件,您更改的JS文件/插件/ PLUGIN_NAME / WWW

To edit your plugin, you make changes to the JS file in /plugins/PLUGIN_NAME/www

要编辑原生code,浏览到/平台/平台/找到你的本地code。

To edit the native code, browse to /platforms/PLATFORM/ and find your native code.

每当你建立或prepare,你的JS文件将被改写到平台的插件目录的具体路径,但本机code只写入到安装该插件后,当/平台/路径

Everytime you build or prepare, your JS file will be rewritten to the platform specific paths from the plugins directory, but your native code is only written to the /platforms/ paths once when the plugin is installed.

我建议,一旦你的插件是完整的,你应该收集所有的你的插件的特定文件和复制他们回你的插件包目录,因此您可以在将来的项目中再次使用(或张贴他们的开源供其他人使用)。

I advise that once your plugin is complete, you should collect all of your plugin specific files and copy them back to your plugin package directory so you can use them again in future projects (or post them for open sourcing for others to use).

这篇关于科尔多瓦Android插件:config.xml中覆盖由科尔多瓦prepare?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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