flutter vscode 安卓打包apk文件

 

配置

 

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。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章