注意:
本文档中描述的修改插件的部分(步骤8~11)并非最佳方法,请直接使用以下git库安装!!!
cordova plugin add https://github.com/gisxiaowei/cordova-plugin-baidumaplocation --variable ANDROID_KEY="<API_KEY_ANDROID>" --variable IOS_KEY="<API_KEY_IOS>"
# 此处的API_KEY_XX来自于第一步,直接替换<API_KEY_XX>,也可以最后跟 --save 参数,将插件信息保存到config.xml中
# 如果只需要Android端或者IOS端,可以只填写一个相应的AK,但是都不填肯定不行
描述
Cordova目前版本已到8.0.0,但由于Cordova7之后,对目录结构做了调整,导致之前不少插件无法加载。出现的常见问题如下:
1、配置文件找不到
Parsing platforms\android\res\xml\config.xml failed(node:10988) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory
2、带有libs包的Java程序编译不通过
:app:compileDebugJavaWithJavacE:\mapp\qyjg\cordova\platforms\android\app\src\main\java\com\aruistar\cordova\baidumap\BaiduMapLocation.java:8:: com.baidu.location FAILED
import com.baidu.location.BDLocation;
^
本人通过不停的google、百度,经实践总结出如下方案。本文以百度地图定位Cordova插件v3.0.2为例,说明如何使其适用于Cordova8。
环境
node v8.11.1
npm 5.6.0
cordova 8.0.0
插件
百度地图定位Cordova插件cordova-plugin-baidumaplocation
步骤
(1)安装cordova
安装最新版本(这里有个坑,不要用cnpm安装,因为创建项目时会报缺少package.json的错误)
npm install -g cordova
安装某一版本
npm install -g cordova@8.0
卸载
npm uninstall -g cordova
(2)创建项目
cd /d E:\mapp\demo
cordova create cordova com.my.demo 示例
解释一下,第1行为进入某个目录下,第2行为在该目录下创建cordova
文件夹,id为com.my.demo
,名称为示例
。
(3)添加Android平台
安装cordova-android@7.0.0
cd cordova
cordova platform add android
(4)apk签名
① 将C:\Program Files\Java\jdk1.8.0_172\bin
将入环境变量的Path
中;
② 生成秘钥release-key.keystore
keytool -genkey -v -keystore C:\release-key.keystore -alias demo -keyalg RSA -keysize 2048 -validity 20000
秘钥库密码和秘钥密码都可以设为demo..
,其他信息可选。
③ 在cordova
文件夹根目录放置release-key.keystore
和build.json
build.json
文件内容:
{
"android": {
"debug": {
"keystore": "release-key.keystore",
"storePassword": "demo..",
"alias": "demo",
"password" : "demo..",
"keystoreType": ""
},
"release": {
"keystore": "release-key.keystore",
"storePassword": "demo..",
"alias": "demo",
"password" : "demo..",
"keystoreType": ""
}
}
}
④ 获取SHA1
通过如下命令得到SHA1
keytool -v -list -keystore c:/release-key.keystore
(5)申请百度地图Android Key
访问http://lbsyun.baidu.com/apiconsole/key,创建应用,输入如下信息。
应用名称:测试
;
应用类型:Android SDK
;
发布版SHA1:apk签名中得到的SHA1;
包名:com.my.demo
。
最终得到访问应用的AK。
(6)安装百度地图定位Cordova插件
将申请到的AK填入<API_KEY_ANDROID>
位置后执行
cordova plugin add cordova-plugin-baidumaplocation --variable ANDROID_KEY="<API_KEY_ANDROID>" --variable IOS_KEY="<API_KEY_IOS>"
(7)定位代码
// 进行定位
baidumap_location.getCurrentPosition(function (result) {
alert(JSON.stringify(result, null, 4));
}, function (error) {
});
(8)运行cordova,报config.xml文件找不到
运行:
cordova run android
报config.xml文件找不到:
Parsing platforms\android\res\xml\config.xml failed(node:10988) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory
原因分析:
cordova\platforms\android
下多了libs
文件夹,导致判断为Eclipse
还是AndroidStudio
出错。具体代码位于cordova\platforms\android\cordova\lib\AndroidStudio.js
中var eclipseFiles = ['AndroidManifest.xml', 'libs', 'res'];
解决方法:
在根目录下的config.xml文件中<platform name="android">标签后添加hook
<platform name="android">
<hook src="scripts/patch-android-studio-check.js" type="before_plugin_add" />
<hook src="scripts/patch-android-studio-check.js" type="before_plugin_install" />
<hook src="scripts/patch-android-studio-check.js" type="before_plugin_rm" />
<hook src="scripts/patch-android-studio-check.js" type="before_plugin_uninstall" />
<hook src="scripts/patch-android-studio-check.js" type="before_prepare" />
</platform>
scripts/patch-android-studio-check.js内容如下:
/**
* This hook overrides a function check at runtime. Currently, cordova-android 7+ incorrectly detects that we are using
* an eclipse style project. This causes a lot of plugins to fail at install time due to paths actually being setup
* for an Android Studio project. Some plugins choose to install things into 'platforms/android/libs' which makes
* this original function assume it is an ecplise project.
*/
module.exports = function(context) {
if (context.opts.cordova.platforms.indexOf('android') < 0) {
return;
}
const path = context.requireCordovaModule('path');
const androidStudioPath = path.join(context.opts.projectRoot, 'platforms/android/cordova/lib/AndroidStudio');
const androidStudio = context.requireCordovaModule(androidStudioPath);
androidStudio.isAndroidStudioProject = function() { return true; };
};
(9)再次运行cordova,Java代码编译出错
出错信息:
:app:compileDebugJavaWithJavacE:\mapp\qyjg\cordova\platforms\android\app\src\main\java\com\aruistar\cordova\baidumap\BaiduMapLocation.java:8:: com.baidu.location FAILED
import com.baidu.location.BDLocation;
^
E:\mapp\qyjg\cordova\platforms\android\app\src\main\java\com\aruistar\cordova\baidumap\BaiduMapLocation.java:9:: com.baidu.location
import com.baidu.location.BDLocationListener;
原因分析:
libs路径不正确
解决方法:
修改plugins\cordova-plugin-baidumaplocation\plugin.xml
修改前
<platform name="android">
<source-file src="src/android/BaiduMapLocation.java" target-dir="src/com/aruistar/cordova/baidumap"/>
<source-file src="libs/android/armeabi/liblocSDK7a.so" target-dir="libs/armeabi"/>
<source-file src="libs/android/armeabi-v7a/liblocSDK7a.so" target-dir="libs/armeabi-v7a"/>
<source-file src="libs/android/arm64-v8a/liblocSDK7a.so" target-dir="libs/arm64-v8a"/>
<source-file src="libs/android/x86/liblocSDK7a.so" target-dir="libs/x86"/>
<source-file src="libs/android/x86_64/liblocSDK7a.so" target-dir="libs/x86_64"/>
<source-file src="libs/android/BaiduLBS_Android.jar" target-dir="libs"/>
</platform>
修改后
<platform name="android">
<source-file src="src/android/BaiduMapLocation.java" target-dir="src/com/aruistar/cordova/baidumap"/>
<lib-file src="libs/android/armeabi" arch="device"/>
<lib-file src="libs/android/armeabi-v7a" arch="device"/>
<lib-file src="libs/android/arm64-v8a" arch="device"/>
<lib-file src="libs/android/x86" arch="device"/>
<lib-file src="libs/android/x86_64" arch="device"/>
<lib-file src="libs/android/BaiduLBS_Android.jar" arch="device"/>
</platform>
(10)再次运行cordova,Java代码编译仍然出错
原因分析:
添加插件时,已生成相关代码,见platforms\android\app\src\main\java\com\aruistar\cordova\baidumap\BaiduMapLocation.java
路径
解决方法:
移除Android,再添加
cordova platform remove android
cordova platform add android
再次运行
再次运行,成功执行。
(11)执行定位代码时,定位出错
输出IocType
为162
,IocTypeDescription
为NetWork location failed because baidu location service can not decrypt the request query, please check the so file!
原因分析:
查找百度地图相关开发文档,发现platforms\android\app\build.gradle
文件中缺少sourceSets
的配置,位于dependencies
之上
sourceSets{
main {
jniLibs.srcDir 'libs'
jni.srcDirs = [] //disable automatic ndk-build
}
}
}
/*
* WARNING: Cordova Lib and platform scripts do management inside of this code here,
* if you are adding the dependencies manually, do so outside the comments, otherwise
* the Cordova tools will overwrite them
*/
dependencies {
结语
再次运行,成功输出当前地理位置信息,至此,终于完成Cordova8兼容旧版百度地图定位Cordova插件的功能。
参考资料
cordova
1.http://cordova.apache.org/docs/en/latest/guide/cli/index.html
百度地图定位
2.https://github.com/aruis/cordova-plugin-baidumaplocation
3.http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/create-project/android-studio
配置文件找不到等
4.https://gist.github.com/joeljeske/68121fa6d643e0937f50458d0172e16e
5.https://github.com/litehelpers/Cordova-sqlite-storage/issues/729
6.https://github.com/ionic-team/ionic/issues/13702
7.http://cordova.apache.org/announcements/2017/12/04/cordova-android-7.0.0.html
apk签名
8.https://www.jianshu.com/p/e9a685c88258
9.https://jingyan.baidu.com/article/59703552e877f98fc00740f0.html
转载请注明:作者gisxiaowei,首发简书 jianshu.com
网友评论