当前位置:首页 > 每日看点

flutter打包apk到底是怎样的才能成功?

卡卷网1年前 (2025-01-19)每日看点428

【03】完整flutter的APP打包流程-以apk设置图标-包名-签名-APP名-打包流程为例—-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈

章节内容【03】

章节内容【03】完整flutter的APP打包流程-以apk设置图标-包名-签名-APP名-打包流程为例

开发背景

我们以vs-code开发flutter 项目来打包 apk为例,安卓apk打包流程设置图标,包名-签名-APP名-打包流程,接上一篇我们已经初步写了一个注册页面代码。

闲话不多,开源仓库地址,可以观摩已经写好的代码:

gitee.com/youyacao/ff-f

实战开始

首先确保安装必需的工具

确保你已经安装了以下工具:

Flutter SDK

Android Studio(用于Android SDK和AVD)

Visual Studio Code

整体我们分为以下步骤:

1,设置包名
2,设置签名
3,设置APP名(应用名字)
4,设置LOGO
5,打包apk

详细步骤

1,设置包名:

修改android/app/src/main/AndroidManifest.xml文件中的package属性。


flutter打包apk到底是怎样的才能成功?  第1张


找到<manifest>标签,修改package属性值为你想要的新包名


flutter打包apk到底是怎样的才能成功?  第2张


优雅草央千澈设置为<manifest xmlns:android="schemas.android.com/apk" package="com.youyacao.freefirend">

打开android/app/build.gradle文件。


flutter打包apk到底是怎样的才能成功?  第3张


进入后提示java gradle环境需要安装插件,我们安装

扩展知识:

在Java中,Gradle 是一个构建自动化工具,通常用于编译、打包、测试和发布Java项目。它基于Groovy或者Kotlin的DSL(Domain-Specific Language),灵活且高效。Gradle在许多现代项目中取代了旧的构建工具,比如Apache Ant和Maven。

主要功能和特点

  1. 依赖管理:Gradle支持从Maven Central、JCenter和Ivy等仓库下载依赖库,自动解决依赖关系。
  2. 多项目构建:Gradle允许同时构建和管理多个相关联的项目,这在大型应用程序和企业级项目中特别有用。
  3. 插件系统:Gradle提供了丰富的插件来扩展其功能,比如Java插件、Groovy插件、Android插件等等。
  4. 灵活的配置:使用Groovy或Kotlin DSL,Gradle脚本非常灵活,能根据项目需求自定义构建逻辑。

找到defaultConfig部分,修改applicationId为新的包名


flutter打包apk到底是怎样的才能成功?  第4张


applicationId = "com.youyacao.freefirend"

2,设置签名

  • android目录下创建key.properties文件,


flutter打包apk到底是怎样的才能成功?  第5张


内容如下:

storePassword=<your-store-password> keyPassword=<your-key-password> keyAlias=<your-key-alias> storeFile=<path-to-your-keystore>


flutter打包apk到底是怎样的才能成功?  第6张


  • 修改android/app/build.gradle文件,添加签名配置:

def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file("key.properties") keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) android { ... signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile file(keystoreProperties['storeFile']) storePassword keystoreProperties['storePassword'] } } buildTypes { release { signingConfig signingConfigs.release } } }


flutter打包apk到底是怎样的才能成功?  第7张


3,设置应用名字

修改android/app/src/main/res/values/strings.xml文件中的<string name="app_name">

如果没有文件,就新建一个

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">FF社交</string> </resources>

4,设置图标

准备好你的应用图标,将其放置在以下目录中:

  • android/app/src/main/res/mipmap-hdpi/
  • android/app/src/main/res/mipmap-mdpi/
  • android/app/src/main/res/mipmap-xhdpi/
  • android/app/src/main/res/mipmap-xxhdpi/
  • android/app/src/main/res/mipmap-xxxhdpi/

为什么有这么多目录,在Flutter项目中,mipmap文件夹用于存放不同分辨率的应用图标。这些文件夹名字中的hdpi、mdpi、xhdpi、xxhdpi、xxxhdpi等表示不同的屏幕密度等级

但是我们设置名字必须用ic_launcher.png


flutter打包apk到底是怎样的才能成功?  第8张


5,打包apk

flutter build apk --release

构建完成后,可以在build/app/outputs/flutter-apk/目录下找到生成的APK文件

执行后,提示报错:

PS G:\code\fluttertest\freefirend> flutter build apk --release Downloading android-arm-profile/windows-x64 tools... 2,717ms Downloading android-arm-release/windows-x64 tools... 1,261ms Downloading android-arm64-profile/windows-x64 tools... 1,577ms Downloading android-arm64-release/windows-x64 tools... 1,127ms Downloading android-x64-profile/windows-x64 tools... 1,098ms Downloading android-x64-release/windows-x64 tools... 846ms [!] No Android SDK found. Try setting the ANDROID_HOME environment variable. PS G:\code\fluttertest\freefirend>

原因很简单,提示我们没有找到Android sdk, 我们需要安装下。

Android studio下载地址:

developer.android.com/s


flutter打包apk到底是怎样的才能成功?  第9张


1.5g,问题不大,优雅草央央的用的网络3分钟下载完毕,嘻嘻。


flutter打包apk到底是怎样的才能成功?  第10张


习惯性安装d盘,安装完整后运行打开做默认配置


flutter打包apk到底是怎样的才能成功?  第11张


选择自定义安装,安装sdk

flutter打包apk到底是怎样的才能成功?  第12张


配置环境变量-系统变量

新建:
变量名: ANDROID_HOME
变量值: D:\soft\Android\sdk

flutter打包apk到底是怎样的才能成功?  第13张


在path中添加:

%ANDROID_HOME%\tools
%ANDROID_HOME%\platform-tools

flutter打包apk到底是怎样的才能成功?  第14张


安装完成后执行

flutter doctor
验证是否安装完成,执行后报错


flutter打包apk到底是怎样的才能成功?  第15张


提示缺少 cmdline-tools ,在新的Android SDK版本中,默认情况下可能不会包括tools目录。我们可以通过手动安装cmdline-tools


flutter打包apk到底是怎样的才能成功?  第16张


登录官网手动下载,手动在目录下 创建\cmdline-tools\latest


flutter打包apk到底是怎样的才能成功?  第17张


将下载的内容手动放进去,添加bin目录到path 环境变量


flutter打包apk到底是怎样的才能成功?  第18张


再次执行只有2个问题了

[!] Android toolchain - develop for Android devices (Android SDK version 35.0.1) ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses [X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe) ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

第一个是提示没接受协议,第二个是找不到谷歌浏览器,

第一步,
执行:
flutter doctor --android-licenses

flutter打包apk到底是怎样的才能成功?  第19张


执行后有很多协议,都输入Y接受即可,

第二步,


flutter打包apk到底是怎样的才能成功?  第20张


找到谷歌浏览器的软件地址,

加入谷歌浏览器进入环境变量,

变量名: CHROME_EXECUTABLE
变量值: C:\Program Files\Google\Chrome\Application\chrome.exe

flutter打包apk到底是怎样的才能成功?  第21张


再次执行验证


flutter打包apk到底是怎样的才能成功?  第22张


所有都是绿色的√,完成,执行打包命令再次。

这回报错直接一堆:

Warning: SDK processing. This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times. Checking the license for package Android SDK Build-Tools 33.0.1 in D:\soft\Android\sdk\licenses License for package Android SDK Build-Tools 33.0.1 accepted. Preparing "Install Android SDK Build-Tools 33.0.1 v.33.0.1". "Install Android SDK Build-Tools 33.0.1 v.33.0.1" ready. Installing Android SDK Build-Tools 33.0.1 in D:\soft\Android\sdk\build-tools\33.0.1 "Install Android SDK Build-Tools 33.0.1 v.33.0.1" complete. "Install Android SDK Build-Tools 33.0.1 v.33.0.1" finished. Font asset "MaterialIcons-Regular.otf" was tree-shaken, reducing it from 1645184 to 1480 bytes (99.9% reduction). Tree-shaking can be disabled by providing the --no-tree-shake-icons flag when building your app. Incorrect package="com.youyacao.freefirend" found in source AndroidManifest.xml: G:\code\fluttertest\freefirend\android\app\src\main\AndroidManifest.xml. Setting the namespace via the package attribute in the source AndroidManifest.xml is no longer supported. Recommendation: remove package="com.youyacao.freefirend" from the source AndroidManifest.xml: G:\code\fluttertest\freefirend\android\app\src\main\AndroidManifest.xml. FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processReleaseMainManifest'. > Incorrect package="com.youyacao.freefirend" found in source AndroidManifest.xml: G:\code\fluttertest\freefirend\android\app\src\main\AndroidManifest.xml. Setting the namespace via the package attribute in the source AndroidManifest.xml is no longer supported. Recommendation: remove package="com.youyacao.freefirend" from the source AndroidManifest.xml: G:\code\fluttertest\freefirend\android\app\src\main\AndroidManifest.xml. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 3m 43s Running Gradle task 'assembleRelease'... 224.5s Gradle task assembleRelease failed with exit code 1


flutter打包apk到底是怎样的才能成功?  第23张


大意看完后,错误信息指出,在新的Android SDK中,不能再通过AndroidManifest.xml中的package属性来设置命名空间。你需要在build.gradle文件中设置namespace


flutter打包apk到底是怎样的才能成功?  第24张


删掉之前写的这个包名


flutter打包apk到底是怎样的才能成功?  第25张


namespace中设置,再次执行打包


flutter打包apk到底是怎样的才能成功?  第25张


一套下来,行云流水,技术又提高了,这对于今后项目维护至关重要!优雅草央千澈-只发干货!欢迎点赞关注+收藏!

扫描二维码推送至手机访问。

版权声明:本文由卡卷网发布,如需转载请注明出处。

本文链接:https://www.kajuan.net/ttnews/2025/01/10174.html

分享给朋友:

相关文章

微博和B站屏蔽马保国

微博和B站屏蔽马保国

11月29日消息,近日微博和B站两大平台纷纷屏蔽了马保国相关信息,解散了一些相关的群组,也搜索不到相关信息了。马保国成为近期的新闻热点,并且被大量自媒体人讽刺和评价。由于关注度极高,这些视频和文章吸引了很多围观群众,不能说所有的内容都很低俗…

为什么程序员不自己开发微信小程序这类似的东西赚钱?

为什么程序员不自己开发微信小程序这类似的东西赚钱?

你如果有好的想法是可以挣钱的 首先大家说的个人资质限制确实多,也不建议直接拿个人资质去用小程序盈利,因为很麻烦 我说一下我的大体操作: 1.首先去申请个体户,这个可以用住宅来注册申请,而且速度很快就几天就下来了,经营类目主要是互联网销售这些…

有哪些好用不火的软件?

有哪些好用不火的软件?

20个无敌冷门的小众APP,好用到 内存爆了都不想卸载,个个是宝藏! 特别是第4、13、19个,大多数人都没玩过~ 1、【毒汤日历 】 – 你的每日快乐源泉 [iPhone/Android]好用指数:⭐⭐⭐⭐⭐ 下载地址:各大应用商店…

我爸讽刺我,写个破代码一年才十几万,他在工地带50个人,让我回去跟他干,写代码没出路,我该怎么选择?

我跟你一样的情况,本人现身说法,千万不要跟你爸干,我就是反面教材,现在想回去都回不去了,快十年没写代码了,再就是岁数大了,38岁了,35岁以上的码农根本就没公司愿意要,而且会受歧视。 工程不好干,首先就是不合法,在法律层面,根本就没有包工头…

苹果为什么不做千元机?

苹果为什么不做千元机?

第一步,打开苹果官网,注意是.com,不是.cn; 第二步,点击iPhone,选择Compare iPhone; 第三步,选择最新iPhone 15系列,查看起售价格,分别为$1199,$999,$799。 这不妥妥的千元机吗,怎么苹果…

为什么扫码支付在中国流行,在发达国家被排斥?

因为这是一种落后的技术。 卖菜的大爷花5毛钱就可以打印出一张二维码来接受付款。 你觉着这种先进么?跟先进完全不沾边的。正是因为不先进,所以才能流行。 卖菜大爷用不起一台先进的、具有NFC感应功能的、还能刷各种银行卡的收款机。 这就是现实。…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。