Flutter:打包apk,详细图文介绍(一)

困扰了一天,终于能正常打包apk安装了,记录下打包的流程。建议参考我这篇文章时,同时看下官网的构建说明。
官网构建并发布 Android 应用详情

1、AS创建Flutter项目



2、cmd执行命令

生成一个sunluyi.jks的文件,可以自行把sunluyi替换)

-alias sunluyi(别名),方便管理都用一个名字

haskell 复制代码
keytool -genkey -v -keystore sunluyi.jks -keyalg RSA -keysize 2048 -validity 10000 -alias sunluyi

然后你会得到一个命名sunluyi.jks的文件

3、cmd执行命令,查看文件详情

haskell 复制代码
keytool -list -v -keystore sunluyi.jks

4、把生成好的.jks文件放在Flutter项目android/app目录下

并在android目录下手动创建key.properties文件


key.properties文件内容就是第二步自己设置的口令密码和别名storeFile对应的文件路径./sunluyi.jks(app目录)

haskell 复制代码
storePassword=sunluyi
keyPassword=sunluyi
keyAlias=sunluyi
storeFile=./sunluyi.jks

5、修改/android/app/build.gradle文件

下边代码中我标注了哪些是新增的内容,哪些是修改的内容

js 复制代码
plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}

// 新增内容 ↓
def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader("UTF-8") { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
    flutterVersionCode = "1"
}

def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
    flutterVersionName = "1.0"
}

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

// 新增内容↑

android {
    namespace = "com.sunluyi.app.flutter_sunluyi"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.sunluyi.app.flutter_sunluyi"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        // 修改内容 ↓
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
        // 修改内容 ↑
    }

    // 新增内容 ↓
    signingConfigs {
        release {
            keyAlias = keystoreProperties['keyAlias']
            keyPassword = keystoreProperties['keyPassword']
            storeFile = keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            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.release
            // 修改内容 ↑
        }
    }
}

flutter {
    source = "../.."
}

6、最后你可能还需要配置应用的权限
/android/app/src/main/AndroidManifest.xml目录

js 复制代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:label="[project]"
        ...
    </application>
    ...
    // 这里是新增的权限
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

打包时有个提示警告:

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.

记录下如何解决。

haskell 复制代码
1、cmd执行:flutter doctor -v

下图是我解决过警告的,最开始是Platform android-35, build-tools 34.0.0

百度了下解决方案:

设置/Android SDK中最开始我是勾选的35,换成下方的勾选之后,再次执行
flutter doctor -v得到:Platform android-35, build-tools 34.0.0

重新执行flutter build apk后没有警告,完成了打包流程。

补充内容

这里都选择34

最后根目录执行flutter build apk打包成apk文件。

这篇文章内容我是创建的新项目进行打包测试的。

我的运行环境,希望这篇文章能对你有所帮助

相关推荐
LuiChun15 小时前
Flutter中的网络请求图片存储为缓存,与定制删除本地缓存
flutter·缓存
leluckys17 小时前
flutter 专题二十四 Flutter性能优化在携程酒店的实践
flutter·性能优化
西辰Knight1 天前
【Flutter入门】2. 快速掌握Dart语言 - 从 Java、JavaScript 转型必看
flutter·dart
weixin_460783871 天前
Flutter Android修改应用名称、应用图片、应用启动画面
android·flutter
轻口味1 天前
【每日学点鸿蒙知识】RelativeContainer组件、List回弹、Flutter方法调用、Profiler工具等
flutter·list·harmonyos
low神2 天前
Flutter入门,Flutter基础知识总结。
前端·javascript·flutter·react native·uni-app·dart
BAStriver2 天前
关于Flutter应用国际化语言的设置
flutter
sunly_2 天前
Flutter:邀请海报,Widget转图片,保存相册
flutter
放下华子我只抽RuiKe52 天前
Vue.js 表单验证实战:一个简单的登录页面
前端·javascript·vue.js·学习·flutter·node.js·json