问题分析
在华为Pura70(HarmonyOS 4.2.0)上使用com.huawei.hms:arenginesdk:4.0.0.5时,调用session.configure()抛出ARUnavailableServiceNotInstalledException异常,原因如下:
- HarmonyOS 4.0+的安装机制变更:
HarmonyOS 4.0及以上版本移除了从应用市场独立安装AREngine Server的方式,改为由应用自行集成并安装AREngine Server APK。 - APK版本不匹配:
您安装的AREngine Server 4.0.11.100与SDK版本(4.0.0.5)可能不兼容,导致服务无法正常启动。
解决方案(Java安卓工程)
步骤1:获取正确的AREngine Server APK
步骤2:将APK集成到安卓工程中
- 将下载的APK文件(如
AREngine_Server.apk)放入项目的app/src/main/assets目录。 - 在应用启动时检查并安装AREngine Server:
// 检查AREngine服务是否安装 if (!AREngineAvailability.isAREngineInstalled(context)) { installAREngineServer(context); } private void installAREngineServer(Context context) { try { // 从assets目录读取APK InputStream is = context.getAssets().open("AREngine_Server.apk"); File outputDir = context.getExternalCacheDir(); File apkFile = new File(outputDir, "AREngine_Server.apk"); FileOutputStream fos = new FileOutputStream(apkFile); byte[] buffer = new byte; int len; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.close(); is.close(); // 安装APK(适配Android 7.0+) Intent intent = new Intent(Intent.ACTION_VIEW); Uri apkUri = FileProvider.getUriForFile( context, context.getPackageName() + ".fileprovider", apkFile ); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } catch (IOException e) { e.printStackTrace(); } }
步骤3:配置AndroidManifest.xml
- 注册FileProvider:
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
- 创建
res/xml/file_paths.xml: <paths> <external-cache-path name="external_cache" path="." /> </paths>
步骤4:调用AR Session前确保服务已安装
// 初始化前检查服务状态 if (AREngineAvailability.isAREngineInstalled(context)) { ARSession session = new ARSession(context); ARConfigBase config = new ARWorldTrackingConfig(); // 按需配置 session.configure(config); } else { // 提示用户安装失败 }
注意事项
- 权限处理:
- 确保应用有
READ_EXTERNAL_STORAGE权限(Android 10以下需动态申请)。 - 首次安装时需引导用户开启“允许安装未知应用”权限。
- 版本兼容性:
- 必须使用与SDK版本匹配的AREngine Server(如SDK 4.0.0.5 → Server 4.0.0.5)。
- 备选方案:
- 若仍需从第三方渠道安装,需验证APK签名是否来自华为(非官方渠道存在风险)。
替代方案建议
- 迁移至HarmonyOS AR Kit(长期推荐):
若项目允许,升级为HarmonyOS应用并使用新版@kit.AR能力(需适配ArkTS),避免兼容性问题。
通过以上步骤,您的安卓工程可在P70设备上正常运行AREngine功能。