应该在 config.xml 文件中声明 phonegap 插件吗? [英] Should a phonegap plugin be declared in the config.xml file?

查看:28
本文介绍了应该在 config.xml 文件中声明 phonegap 插件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 phonegap 开发的新手,所以我有几个问题:

1) 我正在使用加速度计插件.我在手册中读到我需要在 config.xml 文件中声明插件才能使用它.但是我注意到即使我从 config.xml 中删除声明

<功能名称="加速度计"><param name="android-package" value="org.apache.cordova.AccelListener"/></功能>

加速度计仍然有效.

所以我想问一下,在phonegap 3.0.0版本中,config.xml的使用是否已经过时了.如果是这样,那么绑定发生在哪里?

2) 我使用 android 平台来构建应用程序.项目结构中有3个内容不同的config.xml文件:

  • a) 在 assets/www/phonegap-app-hello-world-3.0.0/www/config.xml
  • b) 在 assets/www/phonegap-app-hello-world-3.0.0/config.xml 中
  • c) 在 res/xml/config.xml 中

每一个有什么用?我想在哪里声明我的插件?我是在 res/xml/config/xml 文件中做的

谢谢

解决方案

我很确定您仍然可以使用该插件的原因是因为您编辑了错误的 config.xml 或不运行cordova 命令行工具将更改传播到应用实际使用的正确config.xml 文件.

在 Cordova 3.x 项目的不同位置有多个 config.xml 文件.我将尝试向您概述文件的不同位置以及您应该如何与它们交互.请记住,这是使用 CLI 时发生的情况(命令行界面) - 我通过键入以下内容生成此目录结构:

cordova 创建 {MyApp}cordova 平台添加 android ioscordova 插件添加 org.apache.cordova.network-information

或者在 Cordova 3.1 之前,将最后一行替换为:

cordova 插件添加 https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git

如果您仅使用平台级 shell 脚本来构建应用程序(我们在 Cordova 2.X 中采用的旧"方式),您通常可以使用相同的工作流程,但需要 使用 Plugman 管理插件.(我们正在记录这两种不同的工作流程".)

首先,当你使用 cordova create MyApp 创建一个应用程序时,它会创建一个像这样的空项目结构:

/myApp//www/# 这是您的跨平台"文件所在的位置.# 构建工具将这些文件复制到正确的# 每个平台的资产文件夹,如/assets/www/# android 或者只是/www/用于 iO.这是你应该去的地方# 做你大部分/所有的工作,这是应该做的# 可能是版本控制的./平台//android/# 这些只会出现在 `cordova platform add` 之后/ios/# 您通常不应按原样触摸这些文件# 经常重新创建,但更改将持续存在./插件//android/# 这些只会出现在 `cordova plugin add` 之后.他们# 几乎只包含本机和 Web 插件代码# 适用于插件支持的所有平台./ios//merges/# 这是您可以放置​​特定于平台的代码的地方# 你写的将与你的十字架合并#平台源码,见自定义各个平台"# 部分:http://cordova.apache.org/docs/en/3.0.0/guide_cli_index.md.html

您应该对 /www/ 中的文件进行所有更改,这是跨平台"源代码.当您使用命令行工具(无论是 Android 的 /assets/www 还是 www代码>/www/(适用于 iOS).这样,您的应用程序只需要一个源文件夹 - 这是您应该在版本控制下拥有的文件夹.您想要的任何应用程序范围的配置更改都应该对放置在此位置的 config.xml 文件进行;稍后当您使用这些工具时,此 config.xml 文件将被复制(有时会使用特定于平台的配置信息进行修改)到每个应用程序的适当位置,例如 /platforms/android/res/xml/config.xml(安卓)或 /platforms/ios/AppName/config.xml(iOS).

说你想通过输入cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git来添加加速插件.如果您在此命令后运行差异,您将看到以下文件已更改或添加:

plugins/org.apache.cordova.network-information/- 这个新文件夹包含每个支持平台的所有插件元信息和代码,包括 Web 和本机代码

plugins/android.json 和 plugins/ios.json- 现在这两个文件都经过编辑,包含对网络信息插件的引用.您将在此处看到 JSON 的 config-munge 位.随着您添加更多插件,此文件将不断增长以引用所有插件.该文件告诉命令行工具它需要替换哪些代码以及在哪些文件中.比如添加了cordova-plugin-network-information插件后,你会在/plugins/android.json中看到:

<代码>{准备队列":{已安装":[],已卸载":[]},config_munge":{res/xml/config.xml":{/*":{"<功能名称="NetworkStatus"><param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager"/></feature>":1}},AndroidManifest.xml":{/*":{"<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>": 1}}},已安装的插件":{org.apache.cordova.network-信息":{"PACKAGE_NAME": "io.cordova.hellocordova"}},dependent_plugins":{}}

这告诉脚本将功能名称写入 res/xml/config.xml(同样,对于 iOS,这将有所不同,因为应用程序级别的配置文件位于iOS!),并告诉它在 AndroidManifest.xml 中写入 android.permission.ACCESS_NETWORK_STATE 权限(你不会在 iOS 上找到这样的东西,因为不需要权限.)(顺便说一句,写入这些 json 文件的内容在插件的 plugin.xml 文件中定义.)

platforms/android/AndroidManifest.xml- CLI 工具负责将插件的 xml 文件中定义的任何权限添加到 AndoridManifest.是的,当您执行 cordova plugin add 时会发生这种情况.这些权限直接从 plugins/android.json 文件中的任何内容复制.当您 'rm' 插件时,这些权限也会被删除.但是,编辑这些文件是智能的,因为您可以将自定义内容添加到 AndroidManifest.xml 中,它们将保持不变.

platforms/android/assets/www/cordova_plugins.js- 该文件隐藏在构成最终应用程序的 html 资源中 - 这些资源(几乎在/platforms/中的任何内容)不应由您编辑,因为它们将被 CLI 工具频繁替换.Cordova 在运行时使用此文件来加载您添加的插件代码;它还负责将 JavaScript 命名空间映射到实际文件(这是clobbers"声明.)例如,我看到:

<代码>{"file": "plugins/org.apache.cordova.network-information/www/network.js","id": "org.apache.cordova.network-information.network",破坏者":["navigator.connection",导航器.网络.连接"]}

这意味着在您的应用程序代码中,navigator.connectionnavigator.network.connection 都将映射到 plugins/org.apache 中包含的代码.cordova.network-information/www/network.js.

platforms/android/res/xml/config.xml- 这是适用于 Android 的平台级 config.xml 文件.该文件由 CLI 工具创建.您在顶级 config.xml (/MyApp/www/config.xml) 中编写的许多信息将被复制到此处,但不是全部(还有一些其他内容,我不确定在哪里额外的东西来自.)这是Android在运行您的应用程序时读取的文件,它需要检查您的配置数据.例如,Cordova Android 代码将使用它来查看安装了哪些插件以及哪些本机类映射到哪些命名空间.我认为编辑它的唯一方法是使用我上面提到的/merges/文件夹.

platforms/ios/{AppName}.xcodeprojcj/project.pbxproj- AndroidManifest.xml 的 iOS 等价物

platforms/ios/{AppName}/config.xml- 这是适用于 iOS 的平台级 config.xml 文件.看看它在与 Android 上的不同位置如何?(例如,不在/res/xml/config.xml 中?)该文件由 CLI 自动更新,您不应触摸它.

platforms/ios/www/cordova_plugins.js- 相同的文件存在于 Android 上(但位于不同的位置)并且具有相同的目的:帮助 Cordova 在有人使用应用程序时在运行时加载您的插件

我认为这几乎描述了cordova项目中使用的所有文件和文件夹.

希望现在您可以看到您实际上应该只编辑 /www/config.xml 文件.该文件将用于构建 /platforms/android/res/xml/config.xml/platforms/ios/{AppName}/config.xml,它们是Cordova 在打包的应用程序运行时使用.此文件的一部分将用于编辑 AndroidManifest.xml 和 project.pbxprojc 文件(分别适用于 Android 和 iOS.)

这解释了为什么即使删除了 <feature name="Accelerometer"> 行,您仍然可以在应用程序中使用加速度计 - 它们只是被重新复制到平台级配置中.xml 来自主应用程序范围的 config.xml

我想唯一需要弄清楚的是如何编辑特定于平台的配置文件;例如,如何编辑 AndroidManifest.xml 文件?好吧,事实证明您可以直接编辑 /platforms/android/AndroidManifest.xml 文件 - CLI 足够智能,不会在自动添加或删除插件权限时删除您的自定义.因此,如果出于某种原因您需要支持比 Cordova 支持的 Android 版本更低的 Android,您只需更改对象,它就会在您的 cordova 插件 add|rm {id} 调用时持续存在.>

我希望澄清事情,请随时提出更多问题!

I am a newbie in phonegap development so I have a couple of questions:

1) I am using the accelerometer plug-in. I read in the manual that i need to declare the plugin in the config.xml file in order to use it. However I noticed that even if i remove the declaration from the config.xml

<feature name="Accelerometer">
    <param name="android-package" value="org.apache.cordova.AccelListener" />
</feature>

the accelerometer still works.

So I would like to ask you if in the phonegap 3.0.0 version , the use of the config.xml is obsolete. If that s the case then where is the binding takes place?

2) I use the android platform to build the app. In the project structure there are three config.xml files with different content:

  • a) In the assets/www/phonegap-app-hello-world-3.0.0/www/config.xml
  • b) In the assets/www/phonegap-app-hello-world-3.0.0/config.xml
  • c) In the res/xml/config.xml

What s the use of each one of them ? Where I suppose to declare my plug in? I did it in the res/xml/config/xml file

Thank you

解决方案

I'm pretty sure the reason that you can still use the plugin is because you either edited the wrong config.xml or did not run the cordova command line tools to propagate your changes to the correct config.xml file that is actually used by the app.

There are multiple config.xml files in different spots in a Cordova 3.x project. I'll try to give you an overview of the different locations of the files and how you should interact with them. Keep in mind that this is what happens when you use the CLI (Command Line-interface) - I generated this directory structure by typing:

cordova create {MyApp}
cordova platform add android ios
cordova plugin add org.apache.cordova.network-information

Or prior to Cordova 3.1, replace the last line with:

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git

If you use just the platform level shell scripts to build the app (the "old" way that we did in Cordova 2.X), you can generally use the same workflow, but will need to Use Plugman to Manage Plugins. (We are in the process of documenting these two different "workflows".)

First, when you create an app with cordova create MyApp, it will create an empty project structure like this:

/myApp/
        /www/           # This is where your "cross-platform' files go.
                        # The build tools copy these files over to the correct
                        # asset folder for each platform, like /assets/www/ for
                        # android or just /www/ for iOs. This is where you should
                        # be doing most/all of your work and is what should
                        # probably be version controlled.
        /platforms/
            /android/   # These will only appear after `cordova platform add`
            /ios/       # You should generally not touch these file as they are
                        # recreated quite often, although changes will persist.
        /plugins/
            /android/   # These will only appear after `cordova plugin add`. They
                        # pretty much just contain the native and web plugin code
                        # for all platforms that a plugin supports.
            /ios/
        /merges/        # This is where you can place platform-specific code that
                        # you write that will get merged in with your cross
                        # platform source, see the "customize each platform"
                        # section of: http://cordova.apache.org/docs/en/3.0.0/guide_cli_index.md.html

You should make all of your changes to files in /www/, which is the "cross platform" source code. Whatever is in this folder will generally get copied and propagated to the platform level www folder when you use the command line tools (whether its /assets/www for Android or just /www/ for iOS). This way, you only need a single source folder for your app - this is the folder that you should have under version control. Any app-wide configuration changes that you want should be made to the config.xml file placed at this location; later when you use the tools, this config.xml file will be copied (and sometimes modified with platform-specific configuration information) to the appropriate place for each app, like /platforms/android/res/xml/config.xml (for android) or /platforms/ios/AppName/config.xml (for iOS).

Say you want to add the acceleration plugin by typing cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git. If you were to run a diff after this command, you will see the following files have been changed or added:

plugins/org.apache.cordova.network-information/ - This new folder contains all of the plugin meta information and code, including web and native code, for each supported platform

plugins/android.json and plugins/ios.json - Both of these files have been edited now to contain a reference to the network-information plugin. This is where you will see the config-munge bit of JSON. As you add more plugins, this file will continuously grow to reference all of them. This file tells the command line tools what code it needs to replace and in which files. For example, after adding the cordova-plugin-network-information plugin, you will see this in /plugins/android.json:

{
        "prepare_queue": {
            "installed": [],
            "uninstalled": []
        },
        "config_munge": {
            "res/xml/config.xml": {
                "/*": {
                    "<feature name="NetworkStatus"><param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" /></feature>": 1
                }
            },
            "AndroidManifest.xml": {
                "/*": {
                    "<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />": 1
                }
            }
        },
        "installed_plugins": {
            "org.apache.cordova.network-information": {
                "PACKAGE_NAME": "io.cordova.hellocordova"
            }
        },
        "dependent_plugins": {}
    }

This tells the scripts to write the feature name into res/xml/config.xml (again, for iOS, this will be different, since the app level config file is in a different location on iOS!), and also tells it to write android.permission.ACCESS_NETWORK_STATE permission into AndroidManifest.xml (you won't find anything like this on iOS since no permissions are necessary.) (By the way, what gets written to each of these json files is defined in the plugin's plugin.xml file.)

platforms/android/AndroidManifest.xml - The CLI tools took care of adding whichever permission were defined in the plugin's xml file to AndoridManifest. Yes, this happens when you do cordova plugin add. These permissions are directly copied from whatever is in the plugins/android.json file. These permissions are also deleted when you 'rm' a plugin. However, editing these files is done intelligenty, in that you can add custom things to AndroidManifest.xml and they will persist.

platforms/android/assets/www/cordova_plugins.js - This file is buried inside the html resources that will make up your final app - these resources (pretty much anything in /platforms/) should not be edited by you because they will be replaced by the CLI tools quite frequently. This file is used by Cordova at runtime to load the plugin code that you added; it also takes care of mapping the JavaScript namespaces to the actual files (this is the "clobbers" declaration.) For example, I see:

{
    "file": "plugins/org.apache.cordova.network-information/www/network.js",
    "id": "org.apache.cordova.network-information.network",
    "clobbers": [
        "navigator.connection",
        "navigator.network.connection"
    ]
}

this means that in your app code, navigator.connection and navigator.network.connection will both map to the code contained in plugins/org.apache.cordova.network-information/www/network.js.

platforms/android/res/xml/config.xml - This is the platform-level config.xml file for Android. This file gets created by the CLI tools. A lot of the information that you write in your top level config.xml (/MyApp/www/config.xml) will get copied here, but not all of it (and there is some additional stuff, I'm not exactly sure where the additional stuff comes from.) This is the file that gets read by Android when it is running your app and it needs to check your config data. For example, Cordova Android code will use this to see which plugins are installed and which native classes map to which namespaces. I think the only way you could edit this is by using the /merges/ folder that I mentioned above.

platforms/ios/{AppName}.xcodeprojcj/project.pbxproj - The iOS equivalent of AndroidManifest.xml

platforms/ios/{AppName}/config.xml - This is the platform-level config.xml file for iOS. See how it is in a different spot than on Android? (eg, not in /res/xml/config.xml?) This file automatically gets updated by the CLI and you should not touch it.

platforms/ios/www/cordova_plugins.js - The same file exists on Android (but in a different location) and has the same purpose: to help Cordova load your plugins at run time when somebody is using the app

I think that pretty much describes all of the files and folders that are used in a cordova project.

Hopefully now you can see that you should in fact only be editing the /www/config.xml file. This file will be used to construct the /platforms/android/res/xml/config.xml and /platforms/ios/{AppName}/config.xml, which are used by Cordova when the packaged app is running. Parts of this file will be used to edit the AndroidManifest.xml and project.pbxprojc files (for Android and iOS, respectively.)

This explains why you were still able to use the accelerometer in your app even after deleting the <feature name="Accelerometer"> lines - they were just being recopied into the platform level config.xml from the main app wide config.xml

I guess the only thing left to figure out is how you can edit platform specific configuration files; for example, how can you edit the AndroidManifest.xml file? Well, it turns out that you can just edit the /platforms/android/AndroidManifest.xml file directly - the CLI is smart enough to not erase your customizations when it automatically adds or removes plugin permissions. So say for some reason you needed to support a lower version of Android than what Cordova supports, you can just change the object and it will persist though your cordova plugin add|rm {id} calls.

I hope that clarifies things, feel free to ask any more questions!

这篇关于应该在 config.xml 文件中声明 phonegap 插件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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