之前写过两篇cordova自定义插件的文章,都是没使用到第三方库,也没有提供用户自定义配置变量,这篇文章主要就来处理这些问题。
例子是用的cordova-plugin-wechat这个插件
在正式开始之前先补充个知识点:
1,在安卓gradle里面有很多个控制变量,比如cdvBuildMultipleApks之类的,怎么来的呢
首先创建一个.build文件比如:config.build文件,
然后在里面定义变量如:
ext {
minSdkVersion = 14
targetSdkVersion = 23
compileSdkVersion = 23
}
也可以直接写为ext.cdvBuildMultipleApks,ext.cdvVersionCodeForceAbiDigit,ext.cdvMinSdkVersion
ext的意思是对外提供变量
我们在build.gradle(app)在中使用的cdvBuildMultipleApks可能来自于build.gradle(project)中定义,也可能来自于其他的.gradle文件,在build.gradle(app)中使用时需要引入(build.gradle(project)的除外)如:apply from "../config.gradle"
2,安卓中config.xml文件中插件的配置举例(cordova-plugin-crosswalk-webview插件):
variable中name对应于插件里面的plugin.xml中$XWALK_VERSION",$XWALK_LITEVERSION,$XWALK_COMMANDLINE,$XWALK_MODE,$XWALK_MULTIPLEAPK:
在插件的plugin.xml中通过上面的方法可以获取到config.xml中插件的配置value
3,安卓中config.xml里面可以写preference开头的标签文件,如:
然后在android中的.java文件和ios中的.m文件里面可以获取到并且配置。
现在我们正式开始,以cordova-plugin-wechat这个插件举例:
plugin.xml配置文件如下:
Wechat
A cordova plugin, a JS version of Wechat SDK
MIT
cordova,wechat,weixin,share
https://github.com/xu-li/cordova-plugin-wechat.git
https://github.com/xu-li/cordova-plugin-wechat/issues
weixin
wechat
NSAllowsArbitraryLoads
CFBundleURLName
weixin
CFBundleURLSchemes
$WECHATAPPID
注:发现通过$WECHATAPPID去取到config.xml中插件配置的value的,同时发现通过在plugin.xml里面就可以把需要的信息写入到了ios中的info文件和安卓中的mainfest.xml文件中去了
安装cordova-plugin-wechat这个插件需要用户指定变量的值,需要在插件plugin.xml中添加
1、name:变量名称,如果需要引用name对应的value时使用$name,如
2、default:默认值,如果用户没有指定值的时候。如果default参数未指定,则name属性用户必须填写,这也是cordova的一个策略。
使用方式:cordova plugin cordova_plugin_xxx --variable key=123456789
安卓刚刚实现了在插件里面的plugin.xml中可以去配置mainfest.xml,发现还有一个需求:就是怎么通过插件去控制gradle中的配置呢?
思路就是:在插件的src/android中创建一个.gradle文件,然后在里面写配置,同时可以根据插件里面的plugin.xml中的变量做不同的处理,还以开头讲的第二个知识点为例,
插件中的plugin.xml中其中有一个配置文件如下:
在xwalk.gradle文件中 ext.xwalkMode = getConfigPreference("xwalkMode");,
这里getConfigPreference("xwalkMode");里面的“xwalkMode”红字部分是对应的。
1, 安卓中自定义插件需要引入jar包,这个问题搞了一天才搞好,被网上的贴子和老的demo给坑了,有个demo是拿jpush举例子的
如下:
实践证明上面是错误的,看cordova官方有两种做法:
The preferred way is to use the
tag (see the Plugin Specification for more details). Specifying libraries in this manner allows them to be resolved via Gradle's Dependency Management logic. This allows commonly used libraries such as gson, android-support-v4, and google-play-services to be used by multiple plugins without conflict.
The second option is to use the
tag to specify the location of a jar file (see the Plugin Specification for more details). This approach should only be used if you are sure that no other plugin will be depending on the library you are referencing (e.g. if the library is specific to your plugin). Otherwise, you risk causing build errors for users of your plugin if another plugin adds the same library. It is worth noting that Cordova app developers are not necessarily native developers, so native platform build errors can be especially frustrating.
意思是说一种通过
过程如下:src/android/libs中放入需要引用的jar包
如上我是自定义友盟统计的插件,然后在plugin.xml中写入:
2,ios中添加framework(依然拿友盟framework举例)
注意的地方是framework有可能会依赖系统的库,比如友盟这里就需要添加: