android集成cordova (cordovaios开发教程)

cordova开发的基本步骤,android打包流程

扩展阅读

基于Cordova批量打场景包(MAC)

APP包名称命名规则

1. 安装cordova打包应用

 `brew install cordova`

2. 创建cordova项目

执行命令 `create app com.githen.app 测试app`
 * `app` 项目的目录名称 ( 下面所有目录均以此目录为根目录说明 )
 * `com.githen.app` 项目包名称
 * `测试app` 项目展现名称

3. 创建代码快捷软链

`MAC/Linux` 执行命令 `ln -s 程序/build app/www`
`Window` 执行命令 `mklink /D 程序/build app/www`
* `程序/build` yarn 执行构建后的目录
* `app/www` cordova目录下的www目录

4. 项目配置调整

  • 修改默认启动图及图标配置

在config.xml中 <platform name="android"> 中添加以下信息

<preference name="orientation" value="portrait" />
<icon density="ldpi" src="res/icon/android/icon-36-ldpi.png" />
<icon density="mdpi" src="res/icon/android/icon-48-mdpi.png" />
<icon density="hdpi" src="res/icon/android/icon-72-hdpi.png" />
<icon density="xhdpi" src="res/icon/android/icon-96-xhdpi.png" />
<icon density="xxhdpi" src="res/icon/android/icon-144-xxhdpi.png" />
<icon density="xxxhdpi" src="res/icon/android/icon-192-xxxhdpi.png" />
<splash density="land-hdpi" src="res/screen/android/screen-hdpi-landscape.png" />
<splash density="land-ldpi" src="res/screen/android/screen-ldpi-landscape.png" />
<splash density="land-mdpi" src="res/screen/android/screen-mdpi-landscape.png" />
<splash density="land-xhdpi" src="res/screen/android/screen-xhdpi-landscape.png" />
<splash density="land-xxhdpi" src="res/screen/android/screen-xxhdpi-landscape.png" />
<splash density="land-xxxhdpi" src="res/screen/android/screen-xxxhdpi-landscape.png" />
<splash density="port-hdpi" src="res/screen/android/screen-hdpi-portrait.png" />
<splash density="port-ldpi" src="res/screen/android/screen-ldpi-portrait.png" />
<splash density="port-mdpi" src="res/screen/android/screen-mdpi-portrait.png" />
<splash density="port-xhdpi" src="res/screen/android/screen-xhdpi-portrait.png" />
<splash density="port-xxhdpi" src="res/screen/android/screen-xxhdpi-portrait.png" />
<splash density="port-xxxhdpi" src="res/screen/android/screen-xxxhdpi-portrait.png" /> 
  • 修改最低兼容版本
// 在build.gradel中的defaultMinSdkVersion=21支持系统为5.0+
  • 在AndroidManifest.xml 修改版本号 和 版本Code
  • 在AndroidManifest.xml 删除 <uses-sdk android:targetSdkVersion="27" />

5. 插件处理

  • 请求白名单
  • cordova plugin add cordova-plugin-whitelist

  • 页面loading
  • cordova plugin add cordova-plugin-splashscreen

  • 退出提醒插件
  • cordova plugin add cordova-plugin-x-toast

  • 相机插件
  • cordova plugin add cordova-plugin-camera

  • 获取版本号
  • cordova plugin add cordova-plugin-app-version

  • 设备信息
  • cordova plugin add cordova-plugin-device

  • 兼容低版本
  • `cordova plugin add cordova-x5webview-plugin`

  • 判断网络连接
  • cordova plugin add cordova-plugin-network-information

  • QQ分享
  • cordova plugin add cordova-plugin-qqsdk --variable QQ_APP_ID=申请的ID

  • 微博分享
  • cordova plugin add cordova-plugin-weibosdk --variable WEIBO_APP_ID=申请的ID

  • 浏览器打开
  • cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=app

  • 微信分享
  • cordova plugin add cordova-plugin-wechat@2.5.0 --variable wechatappid=申请的ID

    此版本插件为2.5.0,需要执行下面命令调整解决分享后的闪退问题
    修改`$your_project/plugins/cordova-plugin-wechat/scripts/android-install.js`中的
    var targetDir = path.join(projectRoot, "platforms", "android", "src", packageName.replace(/\./g, path.sep), "wxapi"); 
    为
    var targetDir = path.join(projectRoot, "platforms", "android", "app","src","main","java", packageName.replace(/\./g, path.sep), "wxapi"); 
    
  • 百度定位
  • cordova plugin add cordova-plugin-baidumaplocation ---variable ANDROID_KEY="申请的key" --variable IOS_KEY="申请的key"

    参考地址:兼容cordova8(https://www.jianshu.com/p/9328f374c504)参考地址:百度定位官网(http://lbsyun.baidu.com/)

    1.在根目录下的config.xml文件中标签后添加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>

    2.新建文件scripts/patch-android-studio-check.js,并写入如下内容

    /**
    * This hook overrides a function check at runtime. Currently, cordova-android 7+ incorrectly detects thatwe 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; };
    };

    3.修改plugins\cordova-plugin-baidumaplocation\plugin.xml如下

    注释下面代码:
    <!-- 
     <source-file src="src/android/BaiduMapLocation.java" target-dir="app/src/com/aruistar/cordova/baidumap"/>
     <source-file src="libs/android/armeabi/libindoor.so" target-dir="app/libs/armeabi"/>
     <source-file src="libs/android/armeabi/liblocSDK7b.so" target-dir="app/libs/armeabi"/>
     <source-file src="libs/android/armeabi-v7a/libindoor.so" target-dir="app/libs/armeabi-v7a"/>
     <source-file src="libs/android/armeabi-v7a/liblocSDK7b.so" target-dir="app/libs/armeabi-v7a"/>
     <source-file src="libs/android/arm64-v8a/libindoor.so" target-dir="app/libs/arm64-v8a"/>
     <source-file src="libs/android/arm64-v8a/liblocSDK7b.so" target-dir="app/libs/arm64-v8a"/>
     <source-file src="libs/android/x86/libindoor.so" target-dir="app/libs/x86"/>
     <source-file src="libs/android/x86/liblocSDK7b.so" target-dir="app/libs/x86"/>
     <source-file src="libs/android/x86_64/libindoor.so" target-dir="app/libs/x86_64"/>
     <source-file src="libs/android/x86_64/liblocSDK7b.so" target-dir="app/libs/x86_64"/>
     <source-file src="libs/android/BaiduLBS_Android.jar" target-dir="app/libs"/>
    -->
    替换为
     <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"/>
    
  • 百度统计
  • cordova plugin add cordova-plugin-baidumobstat

    1.在插件src/android/BaiduMobStat.java中添加start代码

    if ("start".equals(action)) {
     String pageName = "";
     try {
     pageName = args.getString(0);
     } catch (Exception e) {
     // TODO: handle exception
     }
    
     if (TextUtils.isEmpty(pageName)) {
     callbackContext.error("pageName invalid, error");
     return;
     }
    
     StatService.start(webView.getContext(), pageName); //System.out.println("baidu:" + StatService.getTestDeviceId(cordova.getActivity()));
    } else

    2.注册新方法 www/baidumobstat.js

    start : function (pageName) {
     exec(null, errorHandle, "BaiduMobStat", "start", [pageName]);
    },

    3.首页添加自动统计代码

    cordova.plugins.BaiduMobStatistics.start('stat');

    4.在AndroidManifest.xml中添加百度统计key代码

    <meta-data android:name="BaiduMobAd_STAT_ID" android:value="统计的ID" />
    <meta-data android:name="BaiduMobAd_CHANNEL" android:value="场景名称" />
    
  • 极光推送
  • cordova plugin add jpush-phonegap-plugin --variable APP_KEY=申请的ID

    参考地址:极光推送官网(https://www.jpush.cn/)

    拷贝plugins/cordova-plugin-jcore/src/android下所有带so文件到android/app/libs对应目录下

    6.APK签名

    • MAC命令
    jarsigner -verbose -keystore [keystorePath] -signedjar [apkOut] [apkin] [alias]
    
    -verbose -> 输出签名过程的详细信息
    -keystore [keystorePath] -> 密钥的库的位置
    -signedjar [apkOut] -> 签名后的输出文件名
    [apkin] -> 待签名的文件名
    [alias] -> 证书别名
    
    
    实例:
    jarsigner -tsa http://timestamp.digicert.com -sigalg SHA1withRSA -digestalg SHA1 -verbose -keystore debug.keystore -signedjar app_2.0.4.190116_release_sjqq_signed.apk app_2.0.4.190116_release_sjqq_unsign.apk androiddebugkey
    • Window

    可直接*载下**360加固(http://jiagu.360.cn/#/global/index)