flutter Android Release 打包

https://blog.csdn.net/rznice/article/details/103169221

电脑上需要安装JDK,并配置好JDK运行环境,并可以正常运行 keytool 命令

keytool
密钥和证书管理工具

命令:

 -certreq            生成证书请求
 -changealias        更改条目的别名
 -delete             删除条目
 -exportcert         导出证书
 -genkeypair         生成密钥对
 -genseckey          生成密钥
 -gencert            根据证书请求生成证书
 -importcert         导入证书或证书链
 -importpass         导入口令
 -importkeystore     从其他密钥库导入一个或所有条目
 -keypasswd          更改条目的密钥口令
 -list               列出密钥库中的条目
 -printcert          打印证书内容
 -printcertreq       打印证书请求的内容
 -printcrl           打印 CRL 文件的内容
 -storepasswd        更改密钥库的存储口令

使用 "keytool -command_name -help" 获取 command_name 的用法

  

windows 下运行:

keytool -genkey -v -keystore c:/flutter/key.jks
 -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias key

 

Linux OR MAC 下运行:

keytool -genkey -v -keystore ~/key.jks -keyalg RSA
-keysize 2048 -validity 10000 -alias key

   

在项目目录的下的android/下面创建 key.properties,其内容如下:

storePassword=<password from previous step>
keyPassword=<password from previous step>
keyAlias=key
storeFile=  c:/flutter/key.jks  
#<location of the key store file, such as /Users/<user name>/key.jks>

  

编辑项目文件下的 android/app/build.gradle,在 android { 上面加上如下内容:

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

android {
    compileSdkVersion 28

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    。。。。。。。

  

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

 

修改为:

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            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.debug
            signingConfig signingConfigs.release
        }
    }

  

切换到项目目录( /)下,运行 flutter build apk 打包即可:
运行成功如下:

flutter build apk
You are building a fat APK that includes binaries for android-arm, android-arm64.
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
        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 --split-per-abi
        Learn more on:  https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split
Initializing gradle...                                              2.8s
Resolving dependencies...                                          11.9s
Running Gradle task 'assembleRelease'...

Running Gradle task 'assembleRelease'... Done                     203.1s (!)
Built build\app\outputs\apk\release\app-release.apk (15.0MB).

 

可以查看flutter build 的帮助文件:

flutter build --help
Flutter build commands.

Usage: flutter build <subcommand> [arguments]
-h, --help    Print this usage information.

Available subcommands:
  aar         Build a repository containing an AAR and a POM file.
  aot         Build an ahead-of-time compiled snapshot of your app's Dart code.
  apk         Build an Android APK file from your app.
  appbundle   Build an Android App Bundle file from your app.
  bundle      Build the Flutter assets directory from your app.
  ios         Build an iOS application bundle (Mac OS X host only).

Run "flutter help" to see global options.

 

flutter build apk --help

flutter build apk --help
Build an Android APK file from your app.

This command can build debug and release versions of your application. 'debug' builds support debugging and a quick
development cycle. 'release' builds don't support debugging and are suitable for deploying to app stores.

Usage: flutter build apk [arguments]
-h, --help                          Print this usage information.
-t, --target=<path>                 The main entry-point file of the application, as run on the device.
                                    If the --target option is omitted, but a file name is provided on the command line,
                                    then that is used instead.
                                    (defaults to "lib\main.dart")

    --debug                         Build a debug version of your app.
    --profile                       Build a version of your app specialized for performance profiling.
    --release                       Build a release version of your app (default mode).
    --flavor                        Build a custom app flavor as defined by platform-specific build setup.
                                    Supports the use of product flavors in Android Gradle scripts, and the use of custom
                                    Xcode schemes.

    --[no-]pub                      Whether to run "flutter pub get" before executing this command.
                                    (defaults to on)

    --build-number                  An identifier used as an internal version number.
                                    Each build must have a unique identifier to differentiate it from previous builds.
                                    It is used to determine whether one build is more recent than another, with higher
                                    numbers indicating more recent build.
                                    On Android it is used as 'versionCode'.
                                    On Xcode builds it is used as 'CFBundleVersion'

    --build-name=<x.y.z>            A "x.y.z" string used as the version number shown to users.
                                    For each new version of your app, you will provide a version number to differentiate
                                    it from previous versions.
                                    On Android it is used as 'versionName'.
                                    On Xcode builds it is used as 'CFBundleShortVersionString'

    --split-per-abi                 Whether to split the APKs per ABIs.To learn more, see:
                                    https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split

    --target-platform               The target platform for which the app is compiled.
                                    [android-arm (default), android-arm64 (default), android-x86, android-x64]

    --[no-]track-widget-creation    Track widget creation locations. This enables features such as the widget inspector.
                                    This parameter is only functional in debug mode (i.e. when compiling JIT, not AOT).

Run "flutter help" to see global options.

 

可以指定debug,release版本,也可以通过target-platform指定生成的apk支持什么架构的, [android-arm (default), android-arm64 (default), android-x86, android-x64] ,默认生成android-arm (default), android-arm64 (default),这样生成的文件比较大,如果指定arm64生成的apk的文件会接近一半,打包时间也明显缩短,生成的apk文件只能在arm64上跑,

flutter build apk --release --target-platform android-arm64

  

flutter build apk --release --target-platform android-arm64
Initializing gradle...                                              1.1s
Resolving dependencies...                                           5.9s
Running Gradle task 'assembleRelease'...
Running Gradle task 'assembleRelease'... Done                       6.3s
Built build\app\outputs\apk\release\app-release.apk (9.8MB).

 

访问权限到/android/app/src/main/AndroidManifest.xml 中添加,在application标签同级添加如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.******.flutter_app">

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.MODE_WORLD_READABLE" />
    <uses-permission android:name="android.permission.MODE_WORLD_WRITEABLE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
          。。。。。。。。
    </application>
</manifest>

 

参考:
https://flutter.dev/docs/deployment/android#review-the-app-manifest
https://blog.csdn.net/weixin_33813128/article/details/93179546
https://www.jianshu.com/p/825e305de61d
https://www.jianshu.com/p/d58dab805ca6
https://blog.csdn.net/duo_shine/article/details/81382757
————————————————
版权声明:本文为CSDN博主「rznice」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/rznice/article/details/103169221

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
打包 Flutter 应用的发布版本(release),可以按照以下步骤进行操作: 1. 在终端或命令提示符中,进入 Flutter 项目的根目录。 2. 运行以下命令,以生成签名密钥(如果已经有签名密钥可以跳过此步骤): ``` keytool -genkey -v -keystore <keystore_name>.jks -keyalg RSA -keysize 2048 -validity 10000 -alias <alias_name> ``` `<keystore_name>` 是生成的密钥库文件名,`<alias_name>` 是密钥别名。按照提示输入密码、姓名等信息即可生成密钥库文件。 3. 在 Flutter 项目的根目录下创建一个名为 `key.properties` 的文件,并在文件中添加以下内容: ``` storePassword=<keystore_password> keyPassword=<key_password> keyAlias=<alias_name> storeFile=<keystore_path> ``` `<keystore_password>` 是密钥库的密码,`<key_password>` 是密钥的密码,`<alias_name>` 是密钥别名,`<keystore_path>` 是密钥库文件的路径。 4. 打开 `android/app/build.gradle` 文件,在 android.defaultConfig 块中添加如下代码: ``` signingConfigs { release { // 这里的 signingConfig 填写你的签名配置 // 如果没有签名配置可以在这里使用默认配置 } } ``` 5. 在同一个 `build.gradle` 文件中,找到 `buildTypes` 部分,在 `release` 块中添加如下代码: ``` signingConfig signingConfigs.release ``` 6. 在终端或命令提示符中,运行以下命令来生成发布版本的 APK 文件: ``` flutter build apk --release ``` 运行完成后,将会在 `build/app/outputs/apk/release/` 目录下生成一个名为 `app-release.apk` 的 APK 文件。 现在,你已经成功地打包Flutter 应用的发布版本(release)。你可以将生成的 APK 文件安装到 Android 设备上进行测试或发布到应用商店。请注意,对于 iOS 平台,你需要使用 Xcode 来打包发布版本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sundaysme

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值