正式打包
用 Android studio 打开 flutter 项目的 android 文件夹,进行APP打包
步骤一 点击 build – Generate Signed Bundle orApk
步骤二 创建 key Store
步骤三 第一次打包项目需要生成证书 先点击 create new 按钮创建 keyStore
步骤四 创建完成 keyStore 后 点击 ok 返回上一层弹窗,自动填入了证书的相关信息
步骤五 点击Next按钮 选择 release 选项, 点击 create 按钮开始打包
打包APK文件在 android\app\release 文件中
安卓APP自动升级流程
升级流程
- 获取本地应用版本号
- 调用后端接口获取服务器保存的版本号
- 如果两个版本号不一致,则弹窗提示用户更新
- 监听下载进度,打开APK安装
注意: IOS 只能跳转到APP Store 中下载
配置安卓权限
AndroidMenifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
需要调用第三方库
插件 | 作用 | |
---|---|---|
package_info_plus | 获取应用版本号 | |
path_provider | 获取文件存储路径 | |
Dio | 网络请求 | |
open_file | 打开文件 | |
permission_handler | 检查APK是否拥有权限 |
完整代码
// 获取应用信息
_getAppInfo()async{
// 获取应用信息
PackageInfo packageInfo = await PackageInfo.fromPlatform();
print(packageInfo.version);
}
// 获取存储路径
_getDocDirectory()async{
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
print("-------");
print(appDocPath);
}
// 检查权限
_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;
}
// 下载文件
_downLoad()async{
var flag = await _checkPermission();
if(flag){
final directory = await getExternalStorageDirectory();
print("获取本地存储路径");
print(directory);
String localPath = directory!.path;
String appName = "aaa.apk";
String savePath="$localPath/$appName";
String ajaxUrl = 'https:xxx.sdk;
Dio dio = Dio();
await dio.download(ajaxUrl, savePath);
await OpenFilex.open(savePath,type: "application/vnd.android.package-archive");
}
}
// 初始化调用
@override
void initState() {
// TODO: implement initState
super.initState();
_getAppInfo();
_getDocDirectory();
}
Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: (){
_downLoad();
},
child:const Icon(Icons.add),
),
)
修改打包名称\图标\启动页面
修改打包的APP名称
地址 android / app / src / main / AndroidManifest.xml
修改图标
android / app / src / main / res / mipmap-* 的文件夹, 需要同文件替换相应尺寸的图标
修改启动页面
android / app / src / main / res /drawable / launch_background.xml
在 android / app / src / main / res / mipmap-* 文件中添加 launch_image.png 图片
更改打包时候版本号
在 pubspec.yaml 文件中的 version 属性
version: 2.0.0 // 根据迭代规则,打包前手动更改