capacitor-android-install
v0.0.1
Published
Capacitor plugin for Android APK installation
Maintainers
Readme
capacitor-android-install
Capacitor插件,用于在Android设备上安装本地APK文件。
支持平台
- Android 7.0+ (API级别24+)
Install
npm install capacitor-android-install
npx cap sync基本用法
import { AndroidInstall } from 'capacitor-android-install';
// 安装本地APK文件
async function installApk() {
try {
const result = await AndroidInstall.installApk({
filePath: '/storage/emulated/0/Download/app-update.apk'
});
console.log('Installation started:', result.success);
} catch (error) {
console.error('Installation failed:', error);
}
}注意: filePath 必须是设备上的绝对路径,建议搭配 @capacitor/filesystem 和 @capacitor/file-transfer 插件使用,先获取文件路径,再下载到指定目录,最后调用该插件安装;
@capacitor/filesystem插件获取的目录会有前缀file://,需自行去除;
权限配置
在 AndroidManifest.xml 中,插件已经包含了必要的权限声明和FileProvider配置:
<!-- 安装未知来源应用的权限(Android 8.0+需要) -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- FileProvider配置,用于Android 7.0+的文件共享 -->
<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>file_paths示例,此处举例 Download 目录
<!-- file_paths.xml -->
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="download" path="download/"/>
</paths>注意事项
插件不会自动处理前缀,需使用设备上的绝对路径,如 /storage/emulated/0/Download/app-update.apk;
