flutter_android_package_installer 0.0.2+3 flutter_android_package_installer: ^0.0.2+3 copied to clipboard
A Flutter plugin for installing Android package from apk file. Plugin uses Android Package Installer and requires minimum API Level version 21.
flutter_android_package_installer #
本库基于android_package_installer改造, 适配最新Flutter 3.24.0
版本, 并修改了以下内容:
- 原库的
installApk
方法调用的是PackageInstaller.Session
方法实现的. PackageInstaller 我这里测试就没成功过. 所以这里更改了此方法的默认实现为Intent.ACTION_VIEW
- 原库的
installApk
方法改名为installApkSession
- 修改
compileSdk 34
- 新增
getPlatformVersion
方法 - 新增
openAppMarket
方法 - 新增
openAppSettingDetail
方法
补充一点 #
FileProvider
资源配置需要包含root-path
:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<root-path name="root" path="" />
</paths>
android_package_installer #
A Flutter plugin for installing Android package from apk file. Plugin uses Android Package Installer and requires minimum API Level version 21.
Using #
import 'package:android_package_installer/android_package_installer.dart';
int? statusCode = await AndroidPackageInstaller.installApk(apkFilePath: '/sdcard/Download/com.example.apk');
if (code != null) {
PackageInstallerStatus installationStatus = PackageInstallerStatus.byCode(statusCode);
print(installationStatus.name);
}
To install the Android package the application will need permissions.
You can use the permission_handler
package to request them.
Setup #
- Add the permissions to your AndroidManifest.xml file in
<projectDir>/android/app/src/main/AndroidManifest.xml
:android.permission.REQUEST_INSTALL_PACKAGES
- required for installing android packages.android.permission.READ_EXTERNAL_STORAGE
- required to access the external storage where the apk file is located.
<manifest xmlns:tools="http://schemas.android.com/tools" ...>
<!-- ADD THESE PERMISSIONS -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application ...>
<activity ...>
...
<!-- ADD THIS INTENT FILTER -->
<intent-filter>
<action
android:name="com.android_package_installer.content.SESSION_API_PACKAGE_INSTALLED"
android:exported="false"/>
</intent-filter>
</activity>
<!-- ADD THIS PROVIDER -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
- Check external path in your custom paths file. If it doesn't exist, create it in
<projectDir>/android/app/src/main/res/xml/file_paths.xml
:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>