1、生成签名文件
在打包之前我们需要一个签名文件,证明文件的唯一性。
keytool -genkey -v -keystore F:\APP\sign.jks -keyalg RSA -keysize 2048 -validity 10000 -alias sign
F:\APP\sign.jks为签名问价存放目录,sign 为密钥名。
后面都有用,请牢记。
在控制台输入上方指令后,会依次让你输入密钥库口令、姓氏、组织机构等等,下方是我的截图:
查看证书SHA1:
keytool -list -v -keystore F:\APP\sign.jks -alias sign -storepass 123456 -keypass 123456
其中 sign、storepass、keypass 分别为: 别名、密钥库密码、 sign文件密码
2、Android Studio 配置
2.1、导入sign.jks
在Flutter工程中/android/app/key/sign.jks创建该目录。
要在app目录下创建key文件夹,然后把sign.jks拖进来。
如图所示:
2.2、创建key.properites
在Flutter工程中/android/key.properites创建该文件。
里面内容如下:
storePassword=123456
keyPassword=123456
keyAlias=sign
storeFile=key/sign.jks
2.3、配置/android/app/build.gradle文件
需要修改 app 包下的 build.gradle 文件,增加如下配置:
android {signingConfigs {release {keyAlias 'sign'keyPassword 'android'storeFile file('key/sign.jks')storePassword 'android'}}buildTypes {release {signingConfig signingConfigs.release}}
}
完整的代码:
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {localPropertiesFile.withReader('UTF-8') { reader ->localProperties.load(reader)}
}def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {flutterVersionCode = '1'
}def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {flutterVersionName = '1.0'
}apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"android {compileSdkVersion 28lintOptions {disable 'InvalidPackage'}defaultConfig {applicationId "sscai.club.flutter_shop"minSdkVersion 16targetSdkVersion 28versionCode flutterVersionCode.toInteger()versionName flutterVersionNametestInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}/*针对打包新加的*/signingConfigs {release {keyAlias 'sign'keyPassword '123456'storeFile file('key/sign.jks')storePassword '123456'}}/*针对打包新加的*/buildTypes {release {signingConfig signingConfigs.release}}
}flutter {source '../..'
}dependencies {testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.2'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
项目目录:
3、打包APK
在当前项目目录的终端中执行:flutter build apk
或者直接在 Android Studio 的 Terinal 执行命令:
生成的目录:当前项目\build\app\outputs\apk\release\xx.apk