Cordova8不兼容旧版插件解决方案
2018-11-21 本文已影响0人
白小纯Zzz
注意:
本文档中描述的修改插件的部分(步骤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;
3、运行cordova,报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; };
};
4、再次运行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>
5、再次运行cordova,Java代码编译仍然出错
原因分析:
添加插件时,已生成相关代码,见platforms\android\app\src\main\java\com\aruistar\cordova\baidumap\BaiduMapLocation.java路径
解决方法:
移除Android,再添加
cordova platform remove android
cordova platform add android
再次运行
再次运行,成功执行。
6、执行定位代码时,定位出错
输出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 {
环境
node v8.11.1
npm 5.6.0
cordova 8.0.0
插件
百度地图定位Cordova插件cordova-plugin-baidumaplocation