美文网首页
Cordova插件开发

Cordova插件开发

作者: Frank_Kivi | 来源:发表于2018-12-03 20:40 被阅读20次
  1. 安装plugman.
    plugman是用来创建插件的工具。命令:
npm install -g plugman
  1. 创建插件项目
plugman create --name xxx --plugin_id xxx --plugin_version xxx

3.给插件添加Android平台
进入插件的项目下;

plugman platform add --platform_name android

4.开发工作
完成3后,已经创建了对应的目录。我们直接在对应的目录下开发就行了。需要注意的plugin.xml,这个配置文件非常重要。里边包含了对应的目录映射。src->android->code目录下是安卓代码,src->android->libs目录下是安卓用到的jar包,www 目录下的是js接口文件。

5.创建package.json
使用命令:

npm init

方便后边发布。

  1. 编辑plugin.xml文件
    这是最重要的配置文件。
<?xml version='1.0' encoding='utf-8'?>
<plugin xmlns:android="http://schemas.android.com/apk/res/android" id="fxp-plugin-fileUtil"
    version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0">
    <name>fileUtil</name>
    <!-- 一个plugin可以有多个module,类似于Android Studio中project和module的概念-->
    <!-- js-module name可随意命名,src值为此module对应js文件的相对路径 -->
    <!-- 【插件id.js-module的name值】对应安装后zipUtil.js中cordova.define方法第一个参数,以及其在cordova_plugin.js中的id值 -->
    <js-module name="zipUtil" src="www/fileUtil.js">
        <!-- target值即为js调用插件方法时所用的对象名,可随意命名;对应安装后cordova_plugin.js中clobbers值 -->
        这个就是在js中调用时的类名。
        <clobbers target="fxp.plugins.zipUtil" />
    </js-module>
    <!-- 可添加多个平台,name值为平台名-->
    <platform name="android">
        <config-file parent="/*" target="res/xml/config.xml">
            <!-- 注册插件module,feature的name值为需要注册的js-module的name值 -->
            <feature name="zipUtil">
                <!-- name值随意,value值为【将插件id中"-"替换为"."后的字符串】.【feature的name值】-->
                <param name="android-package" value="fxp.plugin.fileUtil.zipUtil" />
            </feature>
        </config-file>
        <config-file parent="/*" target="AndroidManifest.xml">
            <!-- 此处添加所需权限 -->
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        </config-file>
        <!-- 将插件文件放到指定目录:src为在插件安装包中的相对路径,target-dir为在插件安装后的相对路径 -->
        <!-- 将java类文件放到【src/包名】目录,注意:此处所写包名须与java类中包名一致 -->
        <source-file src="src/android/code/zipUtil.java" target-dir="src/fxp.plugin.fileUtil" />
        <source-file src="src/android/code/CompressUtil.java" target-dir="src/fxp.plugin.fileUtil" />
        <source-file src="src/android/code/DateUtils.java" target-dir="src/fxp.plugin.fileUtil" />
        <source-file src="src/android/code/FileService.java" target-dir="src/fxp.plugin.fileUtil" />
        <source-file src="src/android/code/FileUtils.java" target-dir="src/fxp.plugin.fileUtil" />
        <source-file src="src/android/code/StringUtil.java" target-dir="src/fxp.plugin.fileUtil" />
 
        <!-- Cordova官方提供了以下两种库依赖方式 -->
        <!-- 1,首选framework标签。此方法引入的库可被多个插件使用而不产生冲突;但此方法引入非官方jar包会失败,故此demo不用此方式 -->
        <!--<framework src="com.android.support:support-v4:24.1.1+" />-->
 
        <!-- 2,其次lib-file标签。但如果有其他插件也添加了此依赖,则可能产生冲突;此demo依赖的是非官方库,只有用此标签了 -->
        <lib-file src="src/android/libs/zip4j_1.3.1.jar" />
        <lib-file src="src/android/libs/commons-lang3-3.1.jar" />
        <lib-file src="src/android/libs/commons-codec-1.7.jar" />
 
        <!-- 经验证,使用source-file标签将依赖包放到libs目录,然后添加依赖关系也是可行的。如下: -->
        <!-- 将依赖包文件放到【libs】目录 -->
        <!-- 插件安装后执行cordova run 命令或在Android Studio中Sync操作即可自动添加依赖关系;如果没有自动添加,可以手动添加 -->
        <!--<source-file src="src/android/libs/commons-codec-1.7.jar" target-dir="libs" />-->
        <!--<source-file src="src/android/libs/commons-lang3-3.1.jar" target-dir="libs" />-->
        <!--<source-file src="src/android/libs/zip4j_1.3.1.jar" target-dir="libs" />-->
 
    </platform>
</plugin>

7.安装自己的插件
在cordova工程目录下使用命令:

cordova plugin add 插件安装包相对路径

8.卸载插件,在cordova工程目录下执行以下命令:

cordova plugin remove 插件id

相关文章

网友评论

      本文标题:Cordova插件开发

      本文链接:https://www.haomeiwen.com/subject/crsucqtx.html