flutter 打包安卓apk 常用配置

发布时间:2024年01月09日

打包之前需要先不配置不然会报错 Execution failed for task ‘:app:mergeReleaseResources’.

APP目录下的build.gradle

aaptOptions.cruncherEnabled = false
    aaptOptions.useNewCruncher = false

如图

配置targetSdkVersion 、minSdkVersion

在android/app/src目录下的build.gradle配置
在这两个地方配置就可以 比如安卓应用商店是安卓targetSdkVersion api 30

defaultConfig {
        applicationId "com.example.tianxi_user"
        minSdkVersion flutter.minSdkVersion //最低兼容的安卓版本,flutter中默认是16
        targetSdkVersion flutter.targetSdkVersion //应用适配的安卓版本 flutter中默认是33
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
         ndk {
            //设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)
            abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","x86_64"
            // biFilters "armeabi", "armeabi-v7a", "arm64-v8a","x86","x86_64"
        }
    }

或者去flutter安装目录中去配置 一劳永逸

flutter\packages\flutter_tools\gradle
flutter.gradle 修改这里的配置


class FlutterExtension {
    /** Sets the compileSdkVersion used by default in Flutter app projects. */
    static int compileSdkVersion = 33

    /** Sets the minSdkVersion used by default in Flutter app projects. */
    static int minSdkVersion = 16

    /** Sets the targetSdkVersion used by default in Flutter app projects. */
    static int targetSdkVersion = 33

    /**
     * Sets the ndkVersion used by default in Flutter app projects.
     * Chosen as default version of the AGP version below as found in
     * https://developer.android.com/studio/projects/install-ndk#default-ndk-per-agp
     */
    static String ndkVersion = "23.1.7779620"

    /**
     * Specifies the relative directory to the Flutter project directory.
     * In an app project, this is ../.. since the app's build.gradle is under android/app.
     */
    String source

    /** Allows to override the target file. Otherwise, the target is lib/main.dart. */
    String target
}

使用自定义证书签名打包

使用 keytool 生成签名证书,placement certificate
keytool -genkey -v -keystore text.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

根据提示生成证书keystore

  • 将 text.keystore 文件放到 android/app/ 目录下。
  • 编辑 android/app/build.gradle,添加 signingConfigs:
android {
  ...
  signingConfigs {
    release {
      keyAlias 'text'
      keyPassword '***' 
      storeFile file('text.keystore ')
      storePassword '***'
    }
  }
  buildTypes {
    ...
    release {
      signingConfig signingConfigs.release
    }
  }
}

填入 keystore 的密码。

最后使用 flutter build apk --release 进行签名打包。

报错Could not create task ':flutter_plugin_android_lifecycle:generateDebugUnitTestConfig

在Android Studio中 打开安卓目录
修改文件,把下面代码注释,在flutter中运行的时候放开


subprojects {
//    // 把这行注释掉,在flutter运行的时候,记得取消这行的注释
//    project.buildDir = "${rootProject.buildDir}/${project.name}"
}

文章来源:https://blog.csdn.net/u010843503/article/details/135473476
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。