flutter发布android

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

flutter build apk --obfuscate --split-debug-info=run/app.android-arm64.symbols

混淆应用

keyStore 创建:

https://blog.csdn.net/qq_42351033/article/details/122279278?

flutter 操作:

https://flutter.cn/docs/deployment/android#adding-a-launcher-icon

从 app 中引用密钥库
创建一个名为 [project]/android/key.properties 的文件,它包含了密钥库位置的定义。在替换内容时请去除 < > 括号:

content_copy
storePassword=
keyPassword=
keyAlias=upload
storeFile=
storeFile 密钥路径在 macOS 上类似于 /Users//upload-keystore.jks,在 Windows 上类似于 C:\Users\\upload-keystore.jks。

请注意:

(再次)请保证这个文件的私有性,不要将它提交到公共的代码管理空间。

在 gradle 中配置签名
在以 release 模式下构建你的应用时,修改 [project]/android/app/build.gradle 文件,以通过 gradle 配置你的上传密钥。

在 android 代码块之前将你 properties 文件的密钥库信息添加进去:

以下代码已更新,可直接使用

local.properties放在android根目录。

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    
    
    localPropertiesFile.withReader('UTF-8') {
    
     reader ->
        localProperties.load(reader)
    }
}


   android {
    
    
         ...
   }

将 key.properties 文件加载到 keystoreProperties 对象中。

找到 buildTypes 代码块:

content_copy
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 localProperties.getProperty('keyAlias')
            keyPassword localProperties.getProperty('keyPassword')
            storeFile localProperties.getProperty('storeFile') ? file(localProperties.getProperty('storeFile')) : null
            storePassword localProperties.getProperty('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
        }
    }

现在我们 app 的发布版本就会被自动签名了。

提示: You might need to run flutter clean after changing the gradle file. This prevents cached builds from affecting the signing process.

当你更改 gradle 文件后,也许需要运行一下 flutter clean。这将防止缓存的版本影响签名过程。

有关应用签名的更多信息,请查看 developer.android.com 的 为您的应用设置签名。

发布优化

启用缩减、混淆处理和优化功能,

android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}

}
复制代码

根据不同CPU打包,减小包大小

flutter build apk --split-per-abi

https://blog.csdn.net/u013038616/article/details/119135165

添加 launcher icon

launcher icon 就是下面图中的这些小方块。

如果要手动设置 launcher icon 还是很麻烦的。幸运的是,这个问题被一个叫 flutter_launcher_icons 的插件解决了。所以原来那个超级麻烦的问题现在就转变为学会使用这个插件就行了。

安装 flutter_launcher_icons 插件
flutter pub add dev:flutter_launcher_icons
复制代码
注意前面的 dev: 表示作为开发期间的依赖,flutter_launcher_icons 会出现在 dev_dependencies 下面。出现在这里的好处是等发版的时候 flutter_launcher_icons 不会被包括里 apk 中。

准备一张 1024x1024 的图片。如果你手边没有合适的,17 亲手画了一张,可以 点击这里,打开原图,右键另存下载。在根目录下建一个文件夹 images,把图片命名为 icon1024.png 放入 images 文件夹中。

添加配置信息,打开 pubspec.yaml

flutter_icons:
image_path: “images/icon1024.png”
android: true
ios: true
复制代码
执行创建命令
flutter pub run flutter_launcher_icons:main
复制代码
检查生成的图片
打开 android/app/src/main/res/drawable 文件夹查看已经生成好的图片,发现所有图片都已经自动生成好了。

为 ios 生成的图片路径 在 ios/Runner/Assets.xcassets/AppIcon.appiconset

注意事项
Format: 32-bit PNG
icon 大小必须是 1024x1024
确保在 40px 大小时也能清晰可见,这是 icon 最小的尺寸。
icon 不能大于 1024KB
icon 不能是透明的。
必须是正方形,不能有圆角。
icon 的边可能会被切掉,因为 Icon 最后展示的可能不是正方形,所以一般边上需要留白。

其他参考:
Flutter Android 打包保姆式全流程 2023 版

https://www.modb.pro/db/617269

猜你喜欢

转载自blog.csdn.net/shelutai/article/details/132600712