flutter 安卓 版本升级
2022-12-21 本文已影响0人
码农界四爷__King
需要改的地方
1.android\app\src\main\AndroidManifest.xml里添加 android:versionCode="1" android:versionName="1.0.0"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.meshapp" android:versionCode="1" android:versionName="1.0.0">
image.png
2.pubspec.yaml里的version 此处版本号需和上方相同
version: 1.0.0+1
image.png
升级代码 AppVersion.dart
import 'dart:io';
import 'package:dio/adapter.dart';
import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';
import 'package:bruno/bruno.dart';
import 'package:http_parser/http_parser.dart';
import 'package:dio/dio.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
// ignore: slash_for_doc_comments
/**
* App版本升级
* package_info 检测版本号 https://pub.dev/packages/package_info package_info: ^2.0.2
* path_provider 获取文件存储路径 https://pub.dev/packages/path_provider path_provider: ^2.0.11
* Dio 下载文件 https://pub.dev/packages/dio dio: ^4.0.6
* open_file 打开文件插件 https://pub.dev/packages/open_file open_file: ^3.2.1
* permission_handler 检测 Apk 是否拥有权限 https://pub.dev/packages/permission_handler permission_handler: ^10.2.0
*
*/
class AppVersionPage extends StatefulWidget {
const AppVersionPage({super.key});
@override
State<AppVersionPage> createState() => _AppVersionPageState();
}
class _AppVersionPageState extends State<AppVersionPage> {
late String _progressValue = "";
@override
void initState() {
// TODO: implement initState
super.initState();
//获取版本号
_getPackageInfo();
//获取路径
_getAppPath();
}
_getPackageInfo() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;
print('0000000000000000000000');
print('appName:$appName');
print('packageName:$packageName');
print('varsion:$version');
print('buildNumber:$buildNumber');
print('0000000000000000000000');
}
_getAppPath() async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
print('tempPath:$tempPath');
print('appDocPath:$appDocPath');
}
//检查权限
Future<bool> _checkPermission() async {
if (Theme.of(context).platform == TargetPlatform.android) {
final status = await Permission.storage.status;
if (status != PermissionStatus.granted) {
final result = await Permission.storage.request();
if (result == PermissionStatus.granted) {
return true;
}
} else {
return true;
}
}
return false;
}
_downLoadApp() async {
// BrnToast.show('4545', context);
var permission = await _checkPermission();
if (permission) {
Directory? directory = await getExternalStorageDirectory();
String? _localPath;
//注意Flutter2.2以后的版本加入了Null safety
if (directory != null) {
_localPath = directory.path;
String appName = "aaa.apk";
String savePath = "$_localPath/$appName";
String apkUrl = "https://192.168.33.250:8001/app-release.apk";
///参数一 文件的网络储存URL
///参数二 下载的本地目录文件
///参数三 下载监听
Dio dio = Dio();
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(client) {
client.badCertificateCallback = (cert, host, port) {
return true;
};
};
await dio.download(apkUrl, savePath,
onReceiveProgress: (received, total) {
if (total != -1) {
///当前下载的百分比例
print((received / total * 100).toStringAsFixed(0) + "%");
setState(() {
_progressValue =
"${(received / total * 100).toStringAsFixed(0)}%";
});
}
});
print(savePath);
await OpenFile.open(savePath,
type: "application/vnd.android.package-archive");
} else {
print("获取目录失败");
}
} else {
print("没有权限");
}
}
Widget build(BuildContext context) {
return Container(
// width: 120,
margin: const EdgeInsets.all(30),
child: BrnBigGhostButton(
title: '第八升级版本$_progressValue',
onTap: _downLoadApp,
),
);
}
}