Flutter 打包APP (Android & IOS)

打包Android apk

参考
https://flutter.dev/docs/deployment/android
https://flutterchina.club/android-release/
Flutter项目打包成安卓apk详解来了(解决安装没网络问题)
【Flutter 专题】39 图解 iOS 打包 IPA 文件
Flutter - 打包APK、IPA 及 IOS上传APPLE Store详解
Flutter-Apk 大小优化探索
Flutter apk最简单的瘦身方式

检查AndroidManifest.xml
  1. 修改app名字
  2. 修改包名
  3. 配置权限(解决apk安装后无网络)

注意,main 和 profile 目录下的Manifest文件都要检查
这两个 main & profile 目录下的Manifest文件都要添加权限
在这里插入图片描述

main & profile 目录下的Manifest文件都添加权限

<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" />
main.dart

在这里插入图片描述

build.gradle (app目录)
  1. application id
  2. version code & version name(在 local.properties统一定义)
  3. minSdk & targetSdk

versionCode和versionName 在 local.properties统一定义

修改启动图标

小工具: 一键生成所有尺寸的应用图标/启动图,同时生成 iOS、Android 和 PhoneGap 应用的图标。遵循 Apple、 Google 官方标准
https://icon.wuruihong.com/

把logo上传到上述网站中,就能导出Android 和 iOS的启动图标了,直接复制到对应的目录替换即可。

APP签名
1. 创建 keystore

如果没有,请通过在运行以下命令来创建一个:

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

或者参考以下命令:

keytool -genkey -v -keystore 自定义.keystore -alias 自定义别名 -keyalg RSA -keysize 2048 -validity 10000 -storepass 自定义密码 -keypass 自定义密码

如用第一个命令,创建的目录会在/Users/your_user_name/key.jks,移动到flutter项目的Android目录下

2. 引用应用程序中的keystore

创建一个名为< app dir >/android/key.properties的文件,其中包含对密钥库的引用:

storePassword=<password from previous step>
keyPassword=<password from previous step>
keyAlias=key
storeFile=<location of the key store file, e.g.  ../key.jks>
3. 配置app build.gradle - 在gradle中配置签名

1.替换:

android {

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

android {

2.替换:

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 {
        signingConfig signingConfigs.release
        //signingConfig signingConfigs.debug
    }
}
[可选] 开启混淆
  • 对apk的代码安全和包大小有要求
  • 确保使用的第三方库不被混淆

默认情况下 flutter 不会开启 Android 的混淆。

如果使用了第三方 Java 或 Android 库,也许你想减小 apk 文件的大小或者防止代码被逆向破解。

  1. 配置混淆
    创建 /android/app/proguard-rules.pro 文件,并添加以下规则:
    !!! 注意, 是app目录下的
#如果使用了第三方 Java 或 Android 库 也需要添加与之对应的规则

#Flutter Wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
  1. 启用 混淆/压缩
    打开 /android/app/build.gradle 文件,定位到 buildTypes 块。
    release 配置中将 minifyEnableduseProguard 设为 true,再将混淆文件指向上一步创建的文件。
android {

    ...

    buildTypes {

        release {

            signingConfig signingConfigs.release

            minifyEnabled true
            useProguard true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
}

到此,app打包的准备工作已经完成

构建一个发布版(release)APK

使用命令行:

  1. cd flutter工程根目录
  2. 运行flutter build apk (flutter build 默认会包含 --release选项).

打包好的发布APK位于< app dir >/build/app/outputs/apk/app-release.apk。


flutter build apk 的一些细节

  • 对apk进行解压,可以看到flutter默认打包的flutter so库架构为 armeabi-v7a
    在这里插入图片描述
  • 分架构打包,可以减少apk体积
flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi

最后的 --split-per-abi 则表示告知需要按照我们指定的类型分别打包(会得到多个apk,每个apk对应一个架构),如果移除则直接构建包含所有 CPU 架构的 Apk 包


打包IOS (待更新…)

在打包前,需要已经申请了Apple的开发者账号

在AppleStore Connect 注册APP&注册APP bundle ID

https://flutter.cn/docs/deployment/ios

ios打包过程遇到的问题

执行flutter build ios

error: The linked framework 'Pods_Runner.framework' is missing one or more architectures required by this target: armv7. (in target 'Runner' from project 'Runner')

在这里插入图片描述
在XCode中,Runner下的Build Settings标签中,找到 architectures - ExcludedArchitectures
为Release和Profile添加如上图4/5所示的架构


执行flutter build ios

Xcode build done.                                           41.3s
Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳
    /Users/keihong/.pub-cache/hosted/pub.flutter-io.cn/flutter_webview_plugin-0.4.0/ios/Classes/FlutterWebviewPlugin.m:92:22: warning: incompatible pointer to integer conversion assigning to 'BOOL' (aka 'signed char') from 'id
    _Nullable' [-Wint-conversion]
        _enableAppScheme = call.arguments[@"enableAppScheme"];
                         ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /Users/keihong/.pub-cache/hosted/pub.flutter-io.cn/flutter_webview_plugin-0.4.0/ios/Classes/FlutterWebviewPlugin.m:434:98: warning: values of type 'NSInteger' should not be used as format arguments; add an explicit cast to
    'long' instead [-Wformat]
        [channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", error.code], @"url": url}];
                                                                                               ~~~   ^~~~~~~~~~
                                                                                               %ld   (long)
    /Users/keihong/.pub-cache/hosted/pub.flutter-io.cn/flutter_webview_plugin-0.4.0/ios/Classes/FlutterWebviewPlugin.m:442:98: warning: values of type 'NSInteger' should not be used as format arguments; add an explicit cast to
    'long' instead [-Wformat]
        [channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", error.code], @"error": error.localizedDescription}];
                                                                                               ~~~   ^~~~~~~~~~
                                                                                               %ld   (long)
    /Users/keihong/.pub-cache/hosted/pub.flutter-io.cn/flutter_webview_plugin-0.4.0/ios/Classes/FlutterWebviewPlugin.m:450:106: warning: values of type 'NSInteger' should not be used as format arguments; add an explicit cast to
    'long' instead [-Wformat]
                [channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", response.statusCode], @"url": webView.URL.absoluteString}];
                                                                                                       ~~~   ^~~~~~~~~~~~~~~~~~~
                                                                                                       %ld   (long)
    4 warnings generated.
    ../../../../../../../.pub-cache/hosted/pub.flutter-io.cn/flutter_color_plugin-1.0.0/lib/flutter_color_plugin.dart:35:35: Warning: Operand of null-aware operation '!' has type 'String' which excludes null.
          int? color = sColorNameMap[(colorString!.toLowerCase())];
                                      ^

    Command PhaseScriptExecution failed with a nonzero exit code
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Analyzing workspace
    note: Constructing build description
    note: Build preparation complete
    warning: None of the architectures in ARCHS (arm64, armv7) are valid. Consider setting ARCHS to $(ARCHS_STANDARD) or updating it to include at least one value from VALID_ARCHS (arm64, arm64e, armv7, armv7s) which is not in
    EXCLUDED_ARCHS (arm64, armv7). (in target 'Runner' from project 'Runner')

Encountered error while building for device.

我这样解决了 flutter_webview_plugin的报错:
https://github.com/Baseflow/flutter-permission-handler/issues/191#issuecomment-575022497

flutter clean
flutter build ios

Xcode build done.                                           36.7s
Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳
    4 warnings generated.
    ../../../../../../../.pub-cache/hosted/pub.flutter-io.cn/flutter_color_plugin-1.0.0/lib/flutter_color_plugin.dart:35:35: Warning: Operand of null-aware operation '!' has type 'String' which excludes null.
          int? color = sColorNameMap[(colorString!.toLowerCase())];
                                      ^

    Command PhaseScriptExecution failed with a nonzero exit code
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Analyzing workspace
    note: Constructing build description
    note: Build preparation complete
    warning: None of the architectures in ARCHS (arm64, armv7) are valid. Consider setting ARCHS to $(ARCHS_STANDARD) or updating it to include at least one value from VALID_ARCHS (arm64, arm64e, armv7, armv7s) which is not in
    EXCLUDED_ARCHS (arm64, armv7). (in target 'Runner' from project 'Runner')

Encountered error while building for device.

解决方法:flutter_color_plugin 的错误,自己写一个ColorUtil…


Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳

    Command PhaseScriptExecution failed with a nonzero exit code
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Analyzing workspace
    note: Constructing build description
    note: Build preparation complete
    warning: None of the architectures in ARCHS (arm64, armv7) are valid. Consider setting ARCHS to $(ARCHS_STANDARD) or updating it to include at least one value from VALID_ARCHS (arm64, arm64e, armv7, armv7s) which is not in
    EXCLUDED_ARCHS (arm64, armv7). (in target 'Runner' from project 'Runner')

Encountered error while building for device.

改成下图所示内容后,出现了另一个错误:
在这里插入图片描述

error: The linked framework 'Pods_Runner.framework' is missing one or more architectures required by this target: armv7s. (in target 'Runner' from project 'Runner')

参考:https://www.jianshu.com/p/5af69bb58916

我的解决:删除ios目录,然后重建ios项目…

flutter create -i swift .

IOS打包步骤记录

  1. flutter build ios
  2. 打开xcode,修改版本号
  3. Project - archive
  4. Distribute app
  5. 根据实际需要选择 Ad-Hoc等
  6. 取消rebuild from bitcode
  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
### 回答1: 我不是很清楚Flutter打包androidios的具体流程,但是根据我从网上查询的信息,可以通过使用Flutter CLI来打包AndroidiOS应用程序,并使用Flutter提供的构建工具来生成最终的应用程序文件。 ### 回答2: Flutter是一种开源的跨平台移动应用开发框架,它可以同时打包AndroidiOS两个平台的应用。下面将分别介绍Flutter打包AndroidiOS的步骤。 Flutter打包Android应用的步骤如下: 1. 首先,我们需要在Flutter项目的根目录下执行命令`flutter build apk`,该命令会利用Flutter打包工具将代码编译成Android应用可执行文件。 2. 打包完成后,在项目的`build/app/outputs/apk/release`路径下会生成一个名为`app.apk`的文件,这就是我们需要的Android安装包。 3. 如果需要对该安装包进行签名,可以使用Java开发工具包(JDK)自带的`keytool`生成一个密钥库文件(keystore),然后利用Android Studio自带的`apksigner`工具对APK进行签名。 4. 最后,我们可以将签名后的APK安装包通过各种方式进行发布和安装。 Flutter打包iOS应用的步骤如下: 1. 在Flutter项目的根目录下执行命令`flutter build ios`,该命令会利用Flutter打包工具将代码编译成iOS应用可执行文件。 2. 打包完成后,在项目的`build/ios/iphoneos`路径下会生成一个名为`Runner.app`的文件,这就是我们需要的iOS应用。 3. 接下来,我们需要在Xcode中打开`Runner.xcworkspace`文件,并配置开发者账号和相关证书。 4. 在Xcode中选择设备或模拟器,然后点击菜单栏的`Product -> Archive`选项,Xcode将会对应用进行打包。 5. 打包完成后,在`Xcode -> Window -> Organizer`界面中可以找到App Archives,我们可以选择对应的应用版本进行导出或发布。 总结来说,Flutter通过使用自身的打包工具,可以快速便捷地打包AndroidiOS应用,对于开发者来说非常方便。同时,开发者还可以根据需要对打包后的应用进行签名或发布。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

忘词木头人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值