flutter开发实战-build apk名称及指令abiFilters常用gradle设置

flutter开发实战-build apk名称及指令abiFilters常用gradle设置

最近通过打包flutter build apk lib/main.dart --release,发现apk命名规则需要在build.gradle设置。这里记录一下。

一、apk命名规则

在android/app/build.gradle中需要设置

  android.applicationVariants.all {variant ->
        variant.outputs.all {
            def buildTime = new Date().format('yyyyMMddHHmm')
            outputFileName = "${project.publishName}_${variant.versionName}_${variant.versionCode}_${buildTime}_${variant.buildType.name}.apk"
        }
    }
    

指令abiFilters

defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.flutter_app_demolab"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName

        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
    }
    

最后通过指令

flutter build apk lib/main.dart --release
    

打包处理的apk在build/app/outputs目录下
在这里插入图片描述
这里的版本name及版本code是需要在pubspec.yaml设置

# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+1

environment:
  sdk: '>=2.19.6 <3.0.0'
    

二、完整的android/build.gradle

在工程的android/build.gradle完整设置如下

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        maven { url "https://maven.aliyun.com/repository/google" }
        maven { url "https://maven.aliyun.com/repository/central" }
        maven { url "https://maven.aliyun.com/repository/jcenter" }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        maven { url "https://maven.aliyun.com/repository/google" }
        maven { url "https://maven.aliyun.com/repository/central" }
        maven { url "https://maven.aliyun.com/repository/jcenter" }
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

    

在工程的gradle.properties,如果工程用到了AndroidX,需要设置android.useAndroidX=true

org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
    

三、android/app/build.gradle设置

在工程的android/app/build.gradle设置如下

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

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

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

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

ext {
    publishName = 'AppDemoLab'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 34
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.flutter_app_demolab"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName

        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
    }

    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
        }
    }

    android.applicationVariants.all {variant ->
        variant.outputs.all {
            def buildTime = new Date().format('yyyyMMddHHmm')
            outputFileName = "${project.publishName}_${variant.versionName}_${variant.versionCode}_${buildTime}_${variant.buildType.name}.apk"
        }
    }
}

flutter {
    source '../..'
}
    

四、小结

flutter开发实战-build apk名称及指令abiFilters常用gradle设置

学习记录,每天不停进步。

相关推荐

  1. flutter 开发实战

    2024-04-26 15:32:06       9 阅读
  2. MyBatis实战指南(三):注解使用方法

    2024-04-26 15:32:06       37 阅读
  3. Conda简介指令

    2024-04-26 15:32:06       37 阅读
  4. Conda简介指令

    2024-04-26 15:32:06       26 阅读
  5. git指令应用案例

    2024-04-26 15:32:06       42 阅读
  6. ROS+UBUNTU开发指令

    2024-04-26 15:32:06       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-26 15:32:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-26 15:32:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-26 15:32:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-26 15:32:06       18 阅读

热门阅读

  1. 多路IO复用--epoll

    2024-04-26 15:32:06       23 阅读
  2. python笔记(15)函数

    2024-04-26 15:32:06       26 阅读
  3. WPF之RadioButton单选框和checkbox多选框

    2024-04-26 15:32:06       15 阅读
  4. MyBatis笔记——一对多参映射问题解决

    2024-04-26 15:32:06       13 阅读
  5. 成为程序员后你都明白了什么?

    2024-04-26 15:32:06       15 阅读