Flutter - Android compileSdkVersion targetSdkVersion 配置

前言

最近不少朋友问我关于 android compileSdkVersion SDK Manager 配置问题。

今天就来讲下,相关的配置属性有 compileSdkVersion buildToolsVersion targetSdkVersion minSdkVersion

视频

https://www.bilibili.com/video/BV1oA41197Sk/

正文

本文 Flutter SDK

❯ flutter--version
Flutter 3.3.9 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b8f7f1f986 (3 周前)2022-11-23 06:43:51 +0900
Engine • revision 8f2221fbef
Tools • Dart 2.18.5 • DevTools 2.15.0

参考

https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration

gradle 包管理

Gradle 是一个基于 JVM 的构建工具,用的 Groovy 脚本进行配置。

https://gradle.org/

打印 gradle 配置

我们先来看下这个默认配置值

  • 文件 android/app/build.gradle
// 打印配置
println "gradle.gradleVersion: $gradle.gradleVersion"
println "android.buildToolsVersion: $android.buildToolsVersion"
println "flutter.compileSdkVersion: $flutter.compileSdkVersion"
println "flutter.targetSdkVersion: $flutter.targetSdkVersion"
println "flutter.minSdkVersion: $flutter.minSdkVersion"
println "flutter.ndkVersion: $flutter.ndkVersion"

android {
  ...
  • 双击 gradle 面板 tasks 输出

gradle.gradleVersion: 7.4
android.buildToolsVersion: 30.0.3
flutter.compileSdkVersion: 31
flutter.targetSdkVersion: 31
flutter.minSdkVersion: 16
flutter.ndkVersion: 21.1.6352462

SDK 包管理

在这里下载 SDK、SDK Tools、NDK

名词解释

  • compileSdkVersion 指使用哪个 api level SDK 来编译你的代码

  • minSdkVersion 最低的可运行 api level 版本

    • 你用的三方库有自己的 minSdkVersion, lint 会在编译的时候检查是否满足
  • targetSdkVersion 目标设备最小兼容 api level 设备,前提是你在这个设备上充分测试可以运行

    • 如 22,表示运行在 android 5.1 以上的设备, 没有动态授权特性
    • 如 23,表示运行在 android 6.0 以上的设备,有动态授权特性
  • buildToolsVersion 构建工具版本

  • 版本关系

    minSdkVersion <= targetSdkVersion <= compileSdkVersion

配置参考

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

// 定义属性读取对象,读取 android/key.properties
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

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

// 打印配置
println "flutter.compileSdkVersion: $flutter.compileSdkVersion"
println "android.buildToolsVersion: $android.buildToolsVersion"
println "flutter.targetSdkVersion: $flutter.targetSdkVersion"
println "flutter.minSdkVersion: $flutter.minSdkVersion"
println "flutter.ndkVersion: $flutter.ndkVersion"

android {
    compileSdkVersion 33
    buildToolsVersion = '33.0.0'
    // compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

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

        // 使用 FULL 在 Play 管理中心的符号化解析后的堆栈轨迹中获取函数名称、文件和行号。
        // https://developer.android.com/studio/build/shrink-code#native-crash-support
        ndk {
            debugSymbolLevel 'FULL'
        }
    }

    // 签名配置
    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            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.debug

//            debuggable false
//            jniDebuggable false
//            minifyEnabled true
//            shrinkResources false
//            multiDexEnabled true
//            proguardFiles getDefaultProguardFile('proguard-android.txt'), '../TMessagesProj/proguard-rules.pro'

            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

android.buildTypes.release.ndk.debugSymbolLevel = 'FULL'

结束语

如果本文对你有帮助,请转发让更多的朋友阅读。

也许这个操作只要你 3 秒钟,对我来说是一个激励,感谢。

祝你有一个美好的一天~

猫哥课程


© 猫哥

  • 微信 ducafecat

  • https://wiki.ducafecat.tech

  • https://ducafecat.com

Last Updated:
Contributors: ducafecat