docs/api/install-apk.md
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-installApk
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-installApk
:::
安装apk
注意:仅支持本地文件路径,网络路径需先通过 uni.downloadFile 下载到本地再调用此 API 安装。
| Web | 微信小程序 | Android | iOS | HarmonyOS |
|---|---|---|---|---|
| <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> | <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> | 3.94 | <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> | <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> |
安装Apk最常见的场景是App的升级,更推荐使用uni的App升级中心,这是一个云端一体开源项目,想达到该项目的体验细节需要大量代码,不如直接拿走使用。
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| options | InstallApkOptions | 是 | - | Web: x; 微信小程序: x; Android: -; iOS: x; HarmonyOS: x |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| filePath | string | 是 | - | Web: x; 微信小程序: x; Android: -; iOS: x; HarmonyOS: x | apk文件地址(仅支持本地文件路径) |
| success | (res: InstallApkSuccess) => void | 否 | null | Web: x; 微信小程序: x; Android: -; iOS: x; HarmonyOS: x | 接口调用成功的回调函数 |
| fail | (err: InstallApkFail) => void | 否 | null | Web: x; 微信小程序: x; Android: -; iOS: x; HarmonyOS: x | 接口调用失败的回调函数 |
| complete | (res: any) => void | 否 | null | Web: x; 微信小程序: x; Android: -; iOS: x; HarmonyOS: x | 接口调用结束的回调函数(调用成功、失败都会执行) |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: x; 微信小程序: x; Android: -; iOS: x; HarmonyOS: x | 安装成功消息 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errCode | number | 是 | - | Web: x; 微信小程序: x; Android: -; iOS: x; HarmonyOS: x | 错误码 |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
::: preview
appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/install-apk/install-apk
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1">
<!-- #endif -->
<view>
<page-head :title="title"></page-head>
<view class="uni-common-mt">
<view class="uni-padding-wrap">
<view class="uni-btn-v">
<button type="primary" @tap="installApk">
installApk
</button>
</view>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
const title = ref('installApk')
const installApk = () => {
uni.installApk({
filePath: "/static/app-android/test.apk",
complete(res : any) {
console.log(res);
}
})
}
</script>
:::
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 错误信息 |