注意:
本文檔中描述的修改插件的部分(步驟8~11)并非最佳方法号胚,請直接使用以下git庫安裝S涫省>魅亍栗竖!
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 參數(shù)狐肢,將插件信息保存到config.xml中
# 如果只需要Android端或者IOS端,可以只填寫一個相應(yīng)的AK沥曹,但是都不填肯定不行
描述
Cordova目前版本已到8.0.0份名,但由于Cordova7之后,對目錄結(jié)構(gòu)做了調(diào)整妓美,導(dǎo)致之前不少插件無法加載僵腺。出現(xiàn)的常見問題如下:
1渺绒、配置文件找不到
Parsing platforms\android\res\xml\config.xml failed(node:10988) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory
2矾芙、帶有l(wèi)ibs包的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、百度责循,經(jīng)實踐總結(jié)出如下方案贵试。本文以百度地圖定位Cordova插件v3.0.2為例琉兜,說明如何使其適用于Cordova8。
環(huán)境
node v8.11.1
npm 5.6.0
cordova 8.0.0
插件
百度地圖定位Cordova插件cordova-plugin-baidumaplocation
步驟
(1)安裝cordova
安裝最新版本(這里有個坑毙玻,不要用cnpm安裝豌蟋,因為創(chuàng)建項目時會報缺少package.json的錯誤)
npm install -g cordova
安裝某一版本
npm install -g cordova@8.0
卸載
npm uninstall -g cordova
(2)創(chuàng)建項目
cd /d E:\mapp\demo
cordova create cordova com.my.demo 示例
解釋一下,第1行為進(jìn)入某個目錄下桑滩,第2行為在該目錄下創(chuàng)建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
將入環(huán)境變量的Path
中幌氮;
② 生成秘鑰release-key.keystore
keytool -genkey -v -keystore C:\release-key.keystore -alias demo -keyalg RSA -keysize 2048 -validity 20000
秘鑰庫密碼和秘鑰密碼都可以設(shè)為demo..
,其他信息可選戳吝。
③ 在cordova
文件夾根目錄放置release-key.keystore
和build.json
build.json
文件內(nèi)容:
{
"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浩销,創(chuàng)建應(yīng)用,輸入如下信息听哭。
應(yīng)用名稱:測試
慢洋;
應(yīng)用類型:Android SDK
塘雳;
發(fā)布版SHA1:apk簽名中得到的SHA1;
包名:com.my.demo
普筹。
最終得到訪問應(yīng)用的AK败明。
(6)安裝百度地圖定位Cordova插件
將申請到的AK填入<API_KEY_ANDROID>
位置后執(zhí)行
cordova plugin add cordova-plugin-baidumaplocation --variable ANDROID_KEY="<API_KEY_ANDROID>" --variable IOS_KEY="<API_KEY_IOS>"
(7)定位代碼
// 進(jìn)行定位
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
文件夾太防,導(dǎo)致判斷為Eclipse
還是AndroidStudio
出錯妻顶。具體代碼位于cordova\platforms\android\cordova\lib\AndroidStudio.js
中var eclipseFiles = ['AndroidManifest.xml', 'libs', 'res'];
解決方法:
在根目錄下的config.xml文件中<platform name="android">標(biāo)簽后添加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內(nèi)容如下:
/**
* 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代碼編譯仍然出錯
原因分析:
添加插件時讳嘱,已生成相關(guān)代碼,見platforms\android\app\src\main\java\com\aruistar\cordova\baidumap\BaiduMapLocation.java
路徑
解決方法:
移除Android酿愧,再添加
cordova platform remove android
cordova platform add android
再次運行
再次運行沥潭,成功執(zhí)行。
(11)執(zhí)行定位代碼時嬉挡,定位出錯
輸出IocType
為162
钝鸽,IocTypeDescription
為NetWork location failed because baidu location service can not decrypt the request query, please check the so file!
原因分析:
查找百度地圖相關(guān)開發(fā)文檔,發(fā)現(xiàn)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 {
結(jié)語
再次運行拔恰,成功輸出當(dāng)前地理位置信息,至此基括,終于完成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.http://www.reibang.com/p/e9a685c88258
9.https://jingyan.baidu.com/article/59703552e877f98fc00740f0.html
轉(zhuǎn)載請注明:作者gisxiaowei,首發(fā)簡書 jianshu.com