Flutter打包Android App

1. 填寫應用配置

基本信息
參考:http://www.itdecent.cn/p/2cc3ed69f01d
appID、應用名稱、應用icon、launchImage

版本信息
pubspec.yaml文件中

2. 應用程序簽名

  • 創(chuàng)建秘鑰
    AndroidTerminal中終端指令:
keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

按提示輸入信息即可

您的名字與姓氏是什么?
  [cy]:  
您的組織單位名稱是什么?
  [cy_studio]:  
您的組織名稱是什么?
  [cy_studio]:  
您所在的城市或區(qū)域名稱是什么?
  [HangZhou]:  
您所在的省/市/自治區(qū)名稱是什么?
  [ZheJiang]:  
該單位的雙字母國家/地區(qū)代碼是什么?
  [zh]:  
CN=cy, OU=cy_studio, O=cy_studio, L=HangZhou, ST=ZheJiang, C=zh是否正確?
  [否]:  是

正在為以下對象生成 2,048 位RSA密鑰對和自簽名證書 (SHA256withRSA) (有效期為 10,000 天):
         CN=cy, OU=cy_studio, O=cy_studio, L=HangZhou, ST=ZheJiang, C=zh
輸入 <key> 的密鑰口令
        (如果和密鑰庫口令相同, 按回車):  
再次輸入新口令: 
[正在存儲/Users/liutiesong/key.jks]

Warning:
JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore /Users/liutiesong/key.jks -destkeystore /Users/liutiesong/key.jks -deststoretype pkcs12" 遷移到行業(yè)標準格式 PKCS12。
liutiesngdeMBP2:cayj_cystudio liutiesong$ 
  • 在app內(nèi)引用秘鑰
    android包根目錄下創(chuàng)建文件key.properties

android/key.properties
編輯key.properties文件

storePassword=codetest
keyPassword=codetest
keyAlias=key
storeFile=/Users/liutiesong/key.jks

注意:不要將文件提交到代碼庫中,在.gitnore文件中忽略路徑:

gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java
/android/key.properties

3. 在gradle中配置生效

按照路徑找到文件:android/app/build.gradle
找到android {}這一行上邊寫入代碼

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()){
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

在android {}這一行找到內(nèi)部以下相似代碼

    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
        }
    }

將其替換為

    signingConfig{
        release{
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    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
        }
    }

4. 打包

可以打包兩種文件:APK和AAB(google最新推薦,但是部分應用商城不支持)

  • 打包APK
flutter build apk
liutiesngdeMBP2:cayj_cystudio liutiesong$ flutter build apk
You are building a fat APK that includes binaries for android-arm, android-arm64, android-x64.
If you are deploying the app to the Play Store, it's recommended to use app bundles or split the APK to reduce the APK size.
    To generate an app bundle, run:
        flutter build appbundle --target-platform android-arm,android-arm64,android-x64
        Learn more on: https://developer.android.com/guide/app-bundle
    To split the APKs per ABI, run:
        flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi
        Learn more on:  https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split
Running Gradle task 'assembleRelease'...                                
Running Gradle task 'assembleRelease'... Done                      12.5s
? Built build/app/outputs/flutter-apk/app-release.apk (17.4MB).
liutiesngdeMBP2:cayj_cystudio liutiesong$ 

apk包路徑: build/app/outputs/flutter-apk/app-release.apk (17.4MB)

  • 打包AAB
flutter build appbundle
liutiesngdeMBP2:cayj_cystudio liutiesong$ flutter build appbundle
Removed unused resources: Binary resource data reduced from 804KB to 790KB: Removed 1%
Running Gradle task 'bundleRelease'...                                  
Running Gradle task 'bundleRelease'... Done                         7.6s
? Built build/app/outputs/bundle/release/app-release.aab (17.5MB).
liutiesngdeMBP2:cayj_cystudio liutiesong$ 

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容