记一次 Flutter 打包APK,安装后闪退的问题
9-24 11:35 浏览(48 更新于 9-24 14:16

执行 

flutter build apk --release

打包 apk 后,安装到手机,打开直接闪退


问题排查

1、检查权限

开始以为是权限没有配置,增加了很多可能需要用到的权限,无效


2、配置打包架构

应该是这个是最不会出错误的,我还是手动设置了一下android/app/build.gradle无效

defaultConfig {
    ...
    ndk {
        abiFilters "arm64-v8a","armeabi-v7a"
    }
}


3、打包混淆

先关闭混淆,再打包试试,同样再文件 android/app/build.gradle  中修改

buildTypes {
    release {
        shrinkResources false
        minifyEnabled false
    }
}

重新打包,安装后成功打开 app

确定是 混淆  的问题,找 AI 要了一份过滤的文件 proguard-rules.pro ,放到  android/app/ 目录下,内容如下

############################################
## Flutter 基础
############################################
# 保留 Flutter 核心类和插件
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.embedding.** { *; }
-keep class io.flutter.embedding.engine.** { *; }
-keep class io.flutter.embedding.android.** { *; }
-keep class io.flutter.plugins.** { *; }

# 保留 Dart JNI 相关
-keep class io.flutter.** { *; }

# 不要警告 Flutter
-dontwarn io.flutter.**

############################################
## Google / AndroidX 常见库
############################################
-dontwarn androidx.**
-dontwarn com.google.**

# 保留反射用到的类
-keepattributes *Annotation*
-keepattributes InnerClasses

############################################
## 常见 Flutter 插件
############################################

# device_info_plus
-keep class dev.fluttercommunity.plus.device_info.** { *; }

# path_provider
-keep class io.flutter.plugins.pathprovider.** { *; }

# shared_preferences
-keep class io.flutter.plugins.sharedpreferences.** { *; }

# sqflite
-keep class com.tekartik.sqflite.** { *; }

# webview_flutter
-keep class io.flutter.plugins.webviewflutter.** { *; }
-keep class android.webkit.** { *; }

# image_picker
-keep class io.flutter.plugins.imagepicker.** { *; }

############################################
## 其他通用配置
############################################
# 保留本地方法
-keepclasseswithmembernames class * {
    native <methods>;
}
############################################
## Baidu Map SDK
############################################
-keep class com.baidu.** { *; }
-dontwarn com.baidu.**

我用到了百度地图的插件,根据就自己插件情况自己增减

最后配置  android/app/build.gradle 

buildTypes {
	release {
		...
		debuggable false
		minifyEnabled true
		shrinkResources true
		proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
	}
}

打包搞定😄



评论(0)
发布评论
回复X
聊天室(0