Flutter打包Android App

终端生成签名文件
keytool -genkey -v -keystore <存放的路径>/文件名字.jks -keyalg RSA -keysize 2048 -validity 有效时间 -alias 别名

// 输入正确命令然后回车,即会请求输入密码(密码不要忘记)

例如:keytool -genkey -v -keystore E:/FlutterProjects/test.jks -keyalg RSA -keysize 2048 -validity 10000 -alias sign

  • genkey : 产生密钥
  • keystore :
  • <存放的路径>/key.jks :证书的存放路径和名字
  • keyalg RSA -keysize 2048 :使用 2048 位 RSA 算法对签名加密
  • validity 10000 :有效期时间,这里是 10000天
  • alias sign : 别名 sign
配置/android/app/build.gradle文件

1.在flutter的/android/app/build.gradle文件中的android前加入

def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

2.在android里的signingConfigs加入release

 signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.release
        }
    }
创建 key.properties 文件
storePassword= 刚刚创建密钥库时的密码
keyPassword= 刚刚创建密钥的密码
keyAlias=sign(别名)
storeFile=E:/FlutterProjects/test.jks
运行打包命令
flutter build apk --release --target-platform android-arm,android-arm64,android-x64
配置启用代码缩减、混淆和优化功能
buildTypes {
    release {
        // Enables code shrinking, obfuscation, and optimization for only
        // your project's release build type.
        minifyEnabled true

        // Enables resource shrinking, which is performed by the
        // Android Gradle plugin.
        shrinkResources true
        // 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
        proguardFiles getDefaultProguardFile(
                'proguard-android-optimize.txt'),
                'proguard-rules.pro'
    }
}


参考: https://juejin.cn/post/6944000831465193480

文章作者: Caesar
文章链接: https://pcnto.com/daily-learning/50-flutter-build-android-app.html
版权声明: 本博客所有文章除特别声明外,转载请注明来自 Peng blog

如果您喜欢本站,你可以点击主页广告以示支持,谢谢。

广告是本站收益的来源希望您能够谅解。