Flutter打包Android App

1. 填写应用配置

基本信息
参考:https://www.jianshu.com/p/2cc3ed69f01d
appID、应用名称、应用icon、launchImage

版本信息
pubspec.yaml文件中

2. 应用程序签名

  • 创建秘钥
    AndroidTerminal中终端指令:
keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

按提示输入信息即可

您的名字与姓氏是什么?
  [cy]:  
您的组织单位名称是什么?
  [cy_studio]:  
您的组织名称是什么?
  [cy_studio]:  
您所在的城市或区域名称是什么?
  [HangZhou]:  
您所在的省/市/自治区名称是什么?
  [ZheJiang]:  
该单位的双字母国家/地区代码是什么?
  [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" 迁移到行业标准格式 PKCS12。
liutiesngdeMBP2:cayj_cystudio liutiesong$ 
  • 在app内引用秘钥
    android包根目录下创建文件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 {}这一行找到内部以下相似代码

    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$ 

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容