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

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

问题描述

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

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

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

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>

加速度计仍然有效。

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

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)我使用android平台来构建应用程序。在项目结构中有三个具有不同内容的config.xml文件:

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


  • 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文件中进行了修改

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

谢谢

推荐答案

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

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.

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

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

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

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

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

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".)

首先,当您使用 cordova创建应用程序时创建MyApp ,它将创建一个空的项目结构,如下所示:

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

您应该对 / www / 中的文件进行所有更改,这是跨平台源代码。当您使用命令行工具(无论其 / assets / www)时,此文件夹中的任何内容通常都会被复制并传播到平台级别 www 适用于Android,或仅适用于 / www / 适用于iOS)。这样,您的应用程序只需要一个源文件夹 - 这是您应该在版本控制下的文件夹。对于位于此位置的 config.xml 文件,您需要进行任何应用程序范围的配置更改;稍后当您使用工具时,这个 config.xml 文件将被复制(并且有时修改平台特定的配置信息)到每个应用程序的适当位置,如 /platforms/android/res/xml/config.xml (适用于Android)或 /platforms/ios/AppName/config.xml (对于iOS)。

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).

假设您要通过键入 cordova插件添加https:// git-wip-us来添加加速插件。 apache.org/repos/asf/cordova-plugin-network-information.git
如果您在此命令后运行diff,您将看到以下文件已更改或添加:

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 /
- 此新文件夹包含每个受支持平台的所有插件元信息和代码(包括网络和本机代码)

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和plugins / ios.json
- 这两个文件现在都已编辑,以包含对网络信息插件的引用。这是您将看到JSON的config-munge位的位置。当您添加更多插件时,此文件将不断增长以引用所有插件。这个文件告诉命令行工具它需要替换什么代码和在哪些文件。例如,在添加cordova-plugin-network-information插件之后,您将在 /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": {}
    }

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

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
- CLI工具负责将插件的xml文件中定义的权限添加到AndoridManifest。是的,当您执行 cordova plugin add 时,会发生这种情况。这些权限直接从plugins / android.json文件中的任何内容复制。当你'rm'插件时,这些权限也会被删除。但是,编辑这些文件是非常聪明的,因为您可以向AndroidManifest.xml中添加自定义内容,并且它们会持久保存。

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
- 这个文件被隐藏在html资源内,这将构成你的最终应用程序 - 这些资源(几乎任何/平台/),不应该由你编辑,因为他们将被CLI工具相当频繁地替换。 Cordova在运行时使用此文件加载您添加的插件代码;它也负责将JavaScript名称空间映射到实际文件(这是clobbers声明。)例如,我看到:

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"
    ]
}

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

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
- 这是Android的平台级config.xml文件。此文件由CLI工具创建。你在你的顶层config.xml(/MyApp/www/config.xml)中写的很多信息将被复制到这里,但不是所有的信息(还有一些额外的东西,我不知道在哪里额外的东西来自。)这是当Android运行您的应用程序时,它需要检查您的配置数据获取读取的文件。例如,Cordova Android代码将使用此代码来查看安装了哪些插件以及哪些本机类映射到哪些命名空间。 您可以编辑的唯一方法是使用上述/ merges /资料夹。

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
- Android等效于AndroidManifest.xml

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

platforms / ios / {AppName } /config.xml
- 这是iOS的平台级config.xml文件。看看它是如何在不同的地方比Android? (例如,不在/res/xml/config.xml?)此文件由CLI自动更新,您不应该触摸它。

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
- 同一个文件存在于Android上(但在不同的位置),目的相同:帮助Cordova在运行时加载您的插件,当有人正在使用应用程序

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

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

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

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

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.)

这解释了为什么您仍然可以即使在删除< feature name =Accelerometer> 行之后,也可以在应用程序中使用加速度计 - 它们只是被重新复制到平台级config.xml中app wide config.xml

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

我想剩下的唯一的事情是如何编辑平台特定的配置文件;例如,如何编辑AndroidManifest.xml文件?好吧,事实证明,你可以直接编辑 /platforms/android/AndroidManifest.xml 文件 - CLI是足够聪明的,以便在自动添加时不会删除您的自定义,删除插件权限。所以说,由于某种原因,你需要支持一个较低版本的Android比Cordova支持,你可以更改对象,它会坚持,虽然你的 cordova插件add | rm {id} 呼叫。

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天全站免登陆