赞
踩
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。