配置
VSCode默认是没有使用密钥签名的,往往我们在正式项目中是需要签名的。那就创建好了。。。所以需要自己创建并使用密钥签名
步骤一 创建密钥库
执行以下命令:
keytool -genkey -v -keystore F:/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key
这句命令大概意思是说:生成 2,048 位RSA密钥对和自签名证书 (SHA256withRSA) (有效期为 10,000 天)
步骤二 填写密钥内容
执行以上命令后会提示一次输入密钥库密码、确认密码、名字、单位等信息,最后问你是否确认创建,回复‘y’回车确认。
回复‘y’回车确认后会输出详细信息,并且让你输入密码(上边设置的密码)
此时在F根目录中会出现一个key.jks文件。(F:/key.jks目录结构自己自定义即可)
注意:
- 保持文件私密; 不要将它加入到公共源代码控制中。
- 此操作生成的签名是*.jks格式
第三步 引用密钥库
创建一个名为/android/key.properties的文件,其中包含对密钥库的引用:
storePassword=<创建keystore时的storePassword> keyPassword=<创建keystore时的keyPassword> keyAlias=key storeFile=<密钥库文件的位置 , 例如: /Users/<user name>/key.jks>
文件内容和层级如图所示:
注意:
- 保持文件私密; 不要将它加入公共源代码控制中.
- storeFile 这里要使用绝对路径
第四步 配置gradle中的签名
通过编辑/android/app/build.gradle文件为您的应用配置签名
如图所示:
build.gradle 文件代码如下:
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 plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
// 增加这三行代码
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
android {
// 增加这块代码
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
compileSdkVersion 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 "com.example.flutter_login_demo"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
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.release //debug修改为release
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
第五步 打包
执行命令 flutter build apk 打包信息如下:
第六步 大功告成!!!
打包好的发布APK位于/build/app/outputs/apk/app-release.apk。