flutter build apk

电脑上需要安装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 的用法

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

windows 下运行:

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

  • 1
  • 2
  • 3

Linux OR MAC 下运行:

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

在项目目录的下的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>
  • 1
  • 2
  • 3
  • 4
  • 5

编辑项目文件下的 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'
    }
    。。。。。。。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

   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
       }
   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

修改为:

    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
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

切换到项目目录( /)下,运行 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).
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

可以查看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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

可以指定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
  • 1
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).

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

访问权限到/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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值