多渠道打包需求很普遍,甚至开发环境下 也需要不同环境测试,提供不同名称或者 不同包名的APK(方便测试 防止覆盖)
纯 Android 多渠道思路:
配置 .gradle 中的 buildTypes, 但是这个思路在 Flutter 中有坑!
因为在配置 多个 buildTypes, 最终都是执行 一下脚本执行
- assembleDbug
- assembleRelease
- assembleProfile
- assembleUat
- assembleXXX
然而 Flutter 编译顺序却不是这样的,他是 最后再执行 assemblexxx 的;
所以 如果直接执行 assemblexxx 的话 有些 flutter 内部 操作就没执行; 所以会出现其未知问题。 我们边察觉的问题就是: versionCode versionName 还是以前 旧的
这里也建议 :
打包的时候, 最好就是用 flutter 提供的命令。以免翻车
以下写法 不适用于 flutter!!! (仅仅作为记录)
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' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion 33 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.pcsy.game" minSdkVersion 26 //8.0 targetSdkVersion 30 versionCode flutterVersionCode.toInteger() versionName flutterVersionName ndk { // abiFilters "armeabi-v7a","arm64-v8a","x86_64","x86" //只打包flutter所支持的架构,flutter没有armeabi架构的so,加x86的原因是为了能够兼容模拟器 abiFilters "armeabi-v7a", "arm64-v8a" //release 时只打"armeabi-v7 arm64-v8a 包 // abiFilters "arm64-v8a" //release 时只打"arm64-v8a包 //abiFilters "armeabi-v7a" //release 时只打"armeabi-v7a包 } manifestPlaceholders = [ appName : '品创数娱', applicationName : "io.flutter.app.FlutterApplication", scheme : "xxxx", OPENINSTALL_APPKEY: "xxxx", ] } signingConfigs { release { storeFile file('../key/keystore_hzsyu.keystore') storePassword 'aaaa*' keyPassword 'aaaa*aaaa' keyAlias 'game' v1SigningEnabled true v2SigningEnabled true } profile { storeFile file('../key/keystore_hzsyu.keystore') storePassword 'aaaa*' keyPassword 'aaaa' keyAlias 'game' v1SigningEnabled true v2SigningEnabled true } uat { storeFile file('../key/keystore_hzsyu.keystore') storePassword 'aaaa*' keyPassword 'aaa' keyAlias 'game' v1SigningEnabled true v2SigningEnabled true } debug { storeFile file('../key/keystore_hzsyu.keystore') storePassword 'aaaa*' keyPassword 'aaaa' keyAlias 'game' v1SigningEnabled true v2SigningEnabled true } } buildTypes { debug { // applicationIdSuffix "debug" zipAlignEnabled true shrinkResources false minifyEnabled false signingConfig signingConfigs.debug proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' buildConfigField("String","APP_HOST","\"xxxxx.com\"") buildConfigField("String","APP_TYPE","\"debug\"") manifestPlaceholders = [ appName: '品创数娱-debug', ] //sourceSets.debug.res.srcDirs=['src/debug/res'] } profile { applicationIdSuffix "test" zipAlignEnabled true shrinkResources false minifyEnabled false signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' buildConfigField("String","APP_HOST","\"xxxxx.com\"") buildConfigField("String","APP_TYPE","\"test\"") manifestPlaceholders = [ appName : '品创数娱-test', scheme : "xxxx", OPENINSTALL_APPKEY: "xxxx", ] sourceSets.profile.res.srcDirs = ['src/profile/res'] } uat { applicationIdSuffix "uat" matchingFallbacks = ['profile','release'] zipAlignEnabled true shrinkResources false minifyEnabled false signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' buildConfigField("String","APP_HOST","\"xxxxx.com\"") buildConfigField("String","APP_TYPE","\"uat\"") manifestPlaceholders = [ appName : '品创数娱-uat', scheme : "xxxxx", OPENINSTALL_APPKEY: "xxxxx", ] sourceSets.profile.res.srcDirs = ['src/profile/res'] } release { zipAlignEnabled true shrinkResources false //删除无用资源 minifyEnabled false //少出无用代码 signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' buildConfigField("String","APP_HOST","\"xxxxx.com\"") buildConfigField("String","APP_TYPE","\"release\"") manifestPlaceholders = [ appName: '品创数娱', ] //sourceSets.release.res.srcDirs=['src/release/res'] } } // lintOptions { // checkReleaseBuilds false // } } flutter { source '../..' } dependencies { //广告相关 implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.android.material:material:1.4.0' implementation(fileTree("libs")); implementation project(':KeplerSDK2.2.5') compileOnly fileTree(dir: '../KeplerSDK2.2.5/libs', include: ['JDSDK_h.jar', 'cps_1.0.0.jar']) implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' // // 建议开发者优先集成OAID。Android 10 无法通过常规方式获取IMEI,影响广告测试及正式广告的填充 // implementation(name: 'oaid_sdk_1.0.25', ext: 'aar') // // SDK // implementation(name: 'zj_sdk_x.y.z', ext: 'aar') // implementation(name: 'kssdk-ct-x.y.z', ext: 'aar') // // x5内核,如已引入,可忽略 // implementation(name: 'tbs_sdk_thirdapp_v4.3.0.313_44213_sharewithdownloadwithfile_withoutGame_obfs_20220722_092328', ext: 'jar') // implementation files('libs/kssdk-ct-3.3.32-publishRelease.aar') // implementation files('libs/oaid_sdk_1.0.25.aar') // implementation files('libs/tbs_sdk_thirdapp_v4.3.0.313_44213_sharewithdownloadwithfile_withoutGame_obfs_20220722_092328.jar') // implementation files('libs/zj_sdk_2.4.11.aar') // implementation files('libs/tbs_sdk_thirdapp_v4.3.0.253_44153.jar') // implementation files('libs/gamemodule-release.aar') }
Flutter 多渠道办法:
flutter 提供 :
--dart-define
这个参数可以 传递到 flutter dart 层,也可以传递到 android .gradle 层; 这样一来 就可以操作很多问题;我这里是 替换包名 和 应用名称
例如:flutter run 的时候 如下
flutter run --profile --dart-define=TYPE=test --dart-define=APP_NAME=品创数娱test --dart-define=APP_SUFFIX=test
android .gradle 中我们就能获取到一下参数
APP_NAMEAPP_SUFFIXdef dartEne = [ APP_NAME : project.hasProperty('APP_NAME') ? appName : '品创数娱', APP_SUFFIX: project.hasProperty('APP_SUFFIX') ? appName : null, ] if (project.hasProperty('dart-defines')) { dartEne = dartEne + project.property('dart-defines') .split(',') .collectEntries { entry -> // 1.22.6 版本 //def pair = URLDecoder.decode(entry).split('=') // 2.2 版本 def pair = new String(entry.decodeBase64(), 'UTF-8').split('=') [(pair.first()): pair.last()] } }
profile { applicationIdSuffix dartEne.APP_SUFFIX zipAlignEnabled true shrinkResources false minifyEnabled false signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' manifestPlaceholders = [ appName : dartEne.APP_NAME, scheme : "xxxx", OPENINSTALL_APPKEY: "xxxx", ] sourceSets.profile.res.srcDirs = ['src/profile/res'] }
我们将它 打印出来发了也是 传递过来了!
这里就能做很多操作了;不管是替换 包名 应用名称,资源都可以;或者可以直接定义 host 都可以传递过来!
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' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" def dartEne = [ APP_NAME : project.hasProperty('APP_NAME') ? appName : '品创数娱', APP_SUFFIX: project.hasProperty('APP_SUFFIX') ? appName : null, ] if (project.hasProperty('dart-defines')) { dartEne = dartEne + project.property('dart-defines') .split(',') .collectEntries { entry -> // 1.22.6 版本 //def pair = URLDecoder.decode(entry).split('=') // 2.2 版本 def pair = new String(entry.decodeBase64(), 'UTF-8').split('=') [(pair.first()): pair.last()] } } println("project.property('dart-defines')") println project.hasProperty('dart-defines') println dartEne android { compileSdkVersion 33 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.pcsy.game" minSdkVersion 26 //8.0 targetSdkVersion 30 versionCode flutterVersionCode.toInteger() versionName flutterVersionName ndk { // abiFilters "armeabi-v7a","arm64-v8a","x86_64","x86" //只打包flutter所支持的架构,flutter没有armeabi架构的so,加x86的原因是为了能够兼容模拟器 abiFilters "armeabi-v7a", "arm64-v8a" //release 时只打"armeabi-v7 arm64-v8a 包 // abiFilters "arm64-v8a" //release 时只打"arm64-v8a包 //abiFilters "armeabi-v7a" //release 时只打"armeabi-v7a包 } manifestPlaceholders = [ appName : '品创数娱', applicationName : "io.flutter.app.FlutterApplication", scheme : "xxxx", OPENINSTALL_APPKEY: "xxxx", ] } signingConfigs { release { storeFile file('../key/keystore_hzsyu.keystore') storePassword 'xxxx*' keyPassword 'xxxx' keyAlias 'game' v1SigningEnabled true v2SigningEnabled true } profile { storeFile file('../key/keystore_hzsyu.keystore') storePassword 'xxxx' keyPassword 'xxxx' keyAlias 'game' v1SigningEnabled true v2SigningEnabled true } debug { storeFile file('../key/keystore_hzsyu.keystore') storePassword 'xxxx' keyPassword 'xxxx' keyAlias 'game' v1SigningEnabled true v2SigningEnabled true } } buildTypes { debug { zipAlignEnabled true shrinkResources false minifyEnabled false signingConfig signingConfigs.debug proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' manifestPlaceholders = [ appName: dartEne.APP_NAME, ] //sourceSets.debug.res.srcDirs=['src/debug/res'] } profile { applicationIdSuffix dartEne.APP_SUFFIX zipAlignEnabled true shrinkResources false minifyEnabled false signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' manifestPlaceholders = [ appName : dartEne.APP_NAME, scheme : "xxxx", OPENINSTALL_APPKEY: "xxxx", ] sourceSets.profile.res.srcDirs = ['src/profile/res'] } release { zipAlignEnabled true shrinkResources false //删除无用资源 minifyEnabled false //少出无用代码 signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' manifestPlaceholders = [ appName: '品创数娱', ] //sourceSets.release.res.srcDirs=['src/release/res'] } } // lintOptions { // checkReleaseBuilds false // } } flutter { source '../..' } dependencies { //广告相关 implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.android.material:material:1.4.0' implementation(fileTree("libs")); implementation project(':KeplerSDK2.2.5') compileOnly fileTree(dir: '../KeplerSDK2.2.5/libs', include: ['JDSDK_h.jar', 'cps_1.0.0.jar']) implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' // // 建议开发者优先集成OAID。Android 10 无法通过常规方式获取IMEI,影响广告测试及正式广告的填充 // implementation(name: 'oaid_sdk_1.0.25', ext: 'aar') // // SDK // implementation(name: 'zj_sdk_x.y.z', ext: 'aar') // implementation(name: 'kssdk-ct-x.y.z', ext: 'aar') // // x5内核,如已引入,可忽略 // implementation(name: 'tbs_sdk_thirdapp_v4.3.0.313_44213_sharewithdownloadwithfile_withoutGame_obfs_20220722_092328', ext: 'jar') // implementation files('libs/kssdk-ct-3.3.32-publishRelease.aar') // implementation files('libs/oaid_sdk_1.0.25.aar') // implementation files('libs/tbs_sdk_thirdapp_v4.3.0.313_44213_sharewithdownloadwithfile_withoutGame_obfs_20220722_092328.jar') // implementation files('libs/zj_sdk_2.4.11.aar') // implementation files('libs/tbs_sdk_thirdapp_v4.3.0.253_44153.jar') // implementation files('libs/gamemodule-release.aar') }
参考链接:
https://itnext.io/flutter-3-7-and-a-new-way-of-defining-compile-time-variables-f63db8a4f6e2
https://itnext.io/secure-your-flutter-project-the-right-way-to-set-environment-variables-with-compile-time-variables-67c3163ff9f4
【Flutter】从安卓与iOS包体优化到一键自动打包脚本的一条龙服务 - 知乎
Gradle_gradle 打印_咋就那么爱学习的博客-CSDN博客