flutter官方文档链接:https://flutterchina.club/android-release/
1.androidStudio在安卓模式下打开flutter项目,初次编译后目录结构如下:
2.修改app下manifests下AndroidManifest.xml文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hljlingan.app_4in1">
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="这里是打包应用后的app名"
android:icon="@mipmap/ic_launcher">
<activity
android:name="这里是app的快照名"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
3.app快照名修改
打开flutter项目中的main.dart,修改title属性名
MaterialApp(
title: 'app的快照名称',
4.修改app的build.gradle文件
打开此gradle文件,安卓兼容版本说明
android {
compileSdkVersion 28 //安卓编译的版本,调用api有超出安卓28的版本会编译报错
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "app包名唯一的标识"
minSdkVersion 16 //安卓兼容的最低版本
targetSdkVersion 28 //安卓实际打包编译的版本
versionCode flutterVersionCode.toInteger() //flutter项目版本号
versionName flutterVersionName //flutter项目版本名
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
点击次处打开安卓sdk版本对应
5.添加app的启动图标
打开此目录,将app的图标放置此目录下,可删除其他尺寸图标
6.app签名
(1)创建签名的密钥证书,appkey
build-生成一个apk
选择apk
创建apk路径
根据实际需要选择存放地址及创建文件名称
(2)生成apk后位置如下
(3)配置使用证书
安卓目录下创建key.pro[erties文件,打开文件,按照官方文档将代码粘入
storePassword=<password from previous step> //密码
keyPassword=<password from previous step> //密码
keyAlias=key //别名
storeFile=<location of the key store file, e.g. /Users/<user name>/key.jks> //地址
地址指向的位置是基于此文件的位置
7.配置安卓节点下build.gradle文件使用证书
在安卓节点上添加如下代码
def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
8.启用混淆配置
在app文件夹下创建proguard-rules.pro文件
添加flutter混淆配置
#Flutter Wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
启用混淆配置,在app节点下的build.gradle文件配置
signingConfig signingConfigs.release
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
8.至此app打包编译前的配置修改完毕
9.执行命令打包
终端打开切换到flutter项目根目录中,执行flutter build apk命令
打包完成后会出现如下
10.到指定目录下查找安装包即可