一、前言
因项目需求,需要uni-app 原生 APP-本地打包集成极光推送,现将集成过程梳理得出此文。
二、集成
2.1 uni-app 项目集成至 Android Studio
2.1.1 拷贝 HbuilderX uni-app 源码至 AS
hbuilderX中使用本地打包生成android资源如下:
构建空的android项目构建如下文件结构,apps下面放hbuilderX本地打包生成的资源文件。
2.1.2 下载最新 SDK
在 Dcloud里下载最新的SDK。
下载解压后目录如下
其中,
- HBuilder-Hello:是- HelloH5打包- App的示例,可以用- AS打包成- APK;
 
- HBuilder-Integrate-AS:是- HBuilder 5+ SDK集成- AS的示例;
 
- SDK:是- HBuilder SDK库文件
 
- UniPlugin-Hello-AS:是开发插件并集成到- Vue的示例。
 
2.1.3 复制 HBuilder-Hello 项目 data 到自己项目对应目录中
2.1.4 复制 HBuilder-Hello 项目 libs 下的三个文件到自己项目对应目录中
2.1.5 修改 dcloud_control.xml 配置文件
注意⚠️:appid 为 HbuilderX uni-app项目标识。
 <hbuilder><apps>    <app appid="__UNI__1DA6F85" appver=""/></apps></hbuilder>
   复制代码
 2.1.6 androidmanifest.xml 中添加 activity 节点
添加如下内容
 <activity android:name="io.dcloud.PandoraEntry"    android:configChanges="orientation|keyboardHidden|keyboard|navigation"    android:label="@string/app_name"    android:launchMode="singleTask"    android:hardwareAccelerated="true"    android:theme="@style/TranslucentTheme"    android:screenOrientation="user"    android:windowSoftInputMode="adjustResize" >    <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />    </intent-filter></activity>
<activity    android:name="io.dcloud.PandoraEntryActivity"    android:launchMode="singleTask"    android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard"    android:hardwareAccelerated="true"    android:permission="com.miui.securitycenter.permission.AppPermissionsEditor"    android:screenOrientation="user"    android:theme="@style/DCloudTheme"    android:windowSoftInputMode="adjustResize">
    <intent-filter>        <category            android:name="android.intent.category.DEFAULT" />        <category            android:name="android.intent.category.BROWSABLE" />        <action            android:name="android.intent.action.VIEW" />        <data            android:scheme="h56131bcf" />    </intent-filter></activity>
   复制代码
 
经过以上配置,可实现hbuilderx本地离线打包android项目,生成 APK 安装包。
首先下载jpush-hbuilder-demo Demo 应用。
拷贝 ./android/app/src/main/java/io.dcloud.feature.jpush 文件夹至你 Android Studio 工程的 /src/main/java/ 目录下。
拷贝 ./jpush.js 到你 Android Studio 工程的 /assets/apps/HBuilder应用名/js/ 下。
在 /assets/apps/你的应用名/www/manifest.json 文件中添加:
 "Push": {    "description": "消息推送"}
   复制代码
 
在 /assets/data/dcloud_properties.xml 中添加(如果已存在 Push feature,可以直接修改):
 <feature    name="Push"    value="io.dcloud.feature.jpush.JPushService" ></feature>
   复制代码
 
在 app/build.gradle 中添加:
 android {    ...    defaultConfig {        applicationId "com.xxx.xxx" // JPush 上注册的包名.        ...        ndk {            // 选择要添加的对应 cpu 类型的 .so 库。            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'            // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'        }        manifestPlaceholders = [            JPUSH_PKGNAME : applicationId,            JPUSH_APPKEY : "应用的 AppKey", // JPush上注册的包名对应的 appkey            JPUSH_CHANNEL : "developer-default", // 暂时填写默认值即可        ]        ...    }    ...}dependencies {    ...    compile 'cn.jiguang.sdk:jpush:3.3.4'  // 此处以JPush 3.3.4 版本为例。    compile 'cn.jiguang.sdk:jcore:2.1.2'  // 此处以JCore 2.1.2 版本为例。    ...}
   复制代码
 
在 AndroidManifest.xml 中添加:
  <!-- since 3.3.0 Required SDK 核心功能--> <!-- 可配置android:process参数将PushService放在其他进程中 --> <!--User defined.  For test only 继承自cn.jpush.android.service.JCommonService--> <service android:name="io.dcloud.feature.jpush.PushService"     android:process=":pushcore">     <intent-filter>         <action android:name="cn.jiguang.user.service.action" />     </intent-filter> </service>
 <!-- User defined.  For test only  用户自定义接收消息器,3.0.7开始支持,目前新tag/alias接口设置结果会在该广播接收器对应的方法中回调--> <!--since 3.3.0 接收JPush相关事件--> <receiver android:name="io.dcloud.feature.jpush.PushMessageReceiver">     <intent-filter>         <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />         <category android:name="${applicationId}"></category>     </intent-filter> </receiver><receiver  android:name="io.dcloud.feature.jpush.JPushReceiver"  android:enabled="true"  android:exported="false">    <intent-filter>      <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required 用户注册SDK的 intent -->      <action android:name="cn.jpush.android.intent.UNREGISTRATION" />      <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required 用户接收SDK消息的 intent -->      <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required 用户接收SDK通知栏信息的 intent -->      <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required 用户打开自定义通知栏的 intent -->      <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!-- Optional 用户接受 Rich Push Javascript 回调函数的intent -->      <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->      <category android:name="${JPUSH_PKGNAME}" />    </intent-filter></receiver>
   复制代码
 三、遇到的问题及解决方案
3.1 libs 中 sdk 版本与 app sdk 版本不一致
 uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [weex_videoplayer-release.aar] /Users/huaqiangsun/.gradle/caches/transforms-2/files-2.1/22285093409dba775963f444b0533dd8/weex_videoplayer-release/AndroidManifest.xml as the library might be using APIs not available in 16。Suggestion: use a compatible library with a minSdk of at most 16,or increase this project's minSdk version to at least 19,or use tools:overrideLibrary="io.dcloud.feature.weex_media" to force usage (may lead to runtime failures)
   复制代码
 
解决措施:需要在build.gradle(Moudle app)中minSdkVersion改为 19。
3.2 Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml to override
 Attribute meta-data#android.support.FILE_PROVIDER_PATHS@resource value=(@xml/filepaths) from [lib.5plus.base-release.aar] AndroidManifest.xml:243:17-61is also present at [torch-plgdtsdk-5.17.3157.aar] AndroidManifest.xml:48:17-57 value=(@xml/torch_file_paths).Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml to override.
   复制代码
 
解决措施:在AndroidManifest.xml的根标签下加上 xmlns:tools="http://schemas.android.com/tools",然后在application标签下加入tools:replace="android:name"。
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         package="com.**">   <application android:name="com.**.App"                android:allowBackup="true"                android:icon="@mipmap/app_icon"                android:label="@string/app_name"                android:supportsRtl="true"                android:theme="@style/AppTheme"                tools:replace="android:name">   </application></manifest>
   复制代码
 四、拓展阅读
评论