本文主要是介绍flutter开发实战-实现多渠道打包及友盟统计(亲测有效),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
flutter开发实战-实现多渠道打包及友盟统计(亲测有效)
最近开发过程中,需要引入友盟进行统计服务。友盟统计还需要区分不同渠道的打开应用的情况,所以需要处理多渠道打包的问题。
一、引入友盟统计
在工程的pubspec.yaml中引入插件
// 在工程 pubspec.yaml 中加入 dependencies:umeng_common_sdk: ^1.2.3
导入及调用初始化友盟
import 'package:umeng_common_sdk/umeng_common_sdk.dart';
调用友盟统计
@overridevoid initState() {super.initState();initPlatformState();UmengCommonSdk.initCommon('5e3f96f3cb23d2a070000048', '5e3f96f3cb23d2a070000048', 'Umeng');UmengCommonSdk.setPageCollectionModeManual();}
这里需要填写channel渠道名
后续我们需要根据打包的渠道来设置。
二、flutter代码中获取渠道
Flutter命令工具增加了自定义参数的功能 --dart-define,可以用这个命令参数在打包或运行 App 时设置参数。
如:
flutter run --dart-define=CHANNEL=YYB
在lib/main.dart中定义变量配置,可以方便调用
/// 定义环境变量配置
class EnvironmentConfig {static const CHANNEL = String.fromEnvironment('CHANNEL');
}
在需要的地方调用获取渠道名
String currentChannel = "";@overridevoid initState() {// TODO: implement initStatesuper.initState();// 获取CHANNEL 参数值String channel = EnvironmentConfig.CHANNEL;print("channel:${channel}");currentChannel = channel;setState(() {});}
显示渠道名
Container(height: 136,width: 300,color: Colors.lightGreen,alignment: Alignment.center,child: Text('当前渠道:${currentChannel}',style: TextStyle(fontSize: 12, color: Colors.white),),),
最终获得渠道显示效果图如下
三、android中gradle配置
我们需要在android/app/build.gradle中添加一下配置
/// 获取渠道参数使用,这里设置一下默认值
def dartEnvironmentVariables = [CHANNEL: 'guanfang-app',
]if (project.hasProperty('dart-defines')) {dartEnvironmentVariables = dartEnvironmentVariables + project.property('dart-defines').split(',').collectEntries { entry ->def pair = new String(entry.decodeBase64(), 'UTF-8').split('=')[(pair.first()): pair.last()]}
}
在输出的apk中,添加对应渠道名,在android一下${dartEnvironmentVariables.CHANNEL}
进行区分不同渠道的apk名称。
ext {publishName = 'AppDemoLab'
}android {android.applicationVariants.all {variant ->variant.outputs.all {def buildTime = new Date().format('yyyyMMddHHmm')outputFileName = "${project.publishName}_${variant.versionName}_${variant.versionCode}_${buildTime}_${variant.buildType.name}_${dartEnvironmentVariables.CHANNEL}.apk"}}
}
打包与测试命令
# 调试例子1:设置渠道为应用宝。
flutter run --dart-define=CHANNEL=YYB#打包例子1:打包应用宝渠道包
flutter build apk --dart-define=CHANNEL=YYB
可以是多个–dart-define,如:
#打包例子2:打包应用宝渠道包,DEBUG参数是Y
flutter build apk --dart-define=CHANNEL=YYB --dart-define=DEBUG=Y
四、apk.sh多渠道打包脚本
在脚本中定义了渠道channels=(YYB HUAWEI MI OPPO VIVO)
在工程目录下创建shell目录,将apk.sh放到shell目录下。
在工程目录下创建prod目录,prod目录下创建apk目录,用于存放打包的渠道apk文件
apk.sh多渠道打包脚本如下
#!/bin/sh#---------------------请修改渠道数组----------------#
channels=(YYB HUAWEI MI OPPO VIVO)#当前工程绝对路径
project_path=$(pwd)#安卓包product文件夹路径
prod_path=${project_path}/prod/apk/
#Flutter打包生成的最初地址
release_path=${project_path}/build/app/outputs/apk/release/clean_tips="执行flutter clean(默认:n) [ y/n ]"
echo $clean_tips
read -t 5 is_clean
if [ ! -n "${is_clean}" ];thenis_clean="n"
fi
while([[ $is_clean != "y" ]] && [[ $is_clean != "n" ]])
doecho "错误!只能输入[ y/n ] !!!"echo $clean_tipsread is_clean
donetips="请输入选择渠道(默认:0) [ ALL: 0 "
c_length=${#channels[@]};
for(( i=0; i<$c_length; i++)) doif (($i < $c_length-1 )); thentips="${tips}${channels[i]}: $((i+1)) "elsetips="${tips}${channels[i]}: $((i+1)) ]"fi
done;echo $tips
read -t 5 number
if [ ! -n "${number}" ];thennumber=0
fi
while(( $number < "0" || $number > $c_length ))
doecho "错误!只能输入0到${c_length} !!!"echo $tipsread number
done#如果有product/apk文件夹则删除,然后再创建一个空文件夹
if [ -d ${prod_path} ]; thenrm -rf ${prod_path}
fi
#创建目录
mkdir -p ${prod_path}if [ ${is_clean} = "y" ];thenecho "=============== 开始清理 ==============="flutter clean
fiif (($number == 0 )); thenecho "=============== 开始构建:全部渠道包 ==============="for(( i=0;i<${c_length};i++)) doecho "正在构建:${channels[$i]} 渠道包"flutter build apk --no-shrink --dart-define=CHANNEL=${channels[$i]}cp -R ${release_path}*.apk ${prod_path}done;
elseecho "=============== 正在构建:${channels[$((number-1))]} 渠道包 ==============="flutter build apk --no-shrink --dart-define=CHANNEL=${channels[$((number-1))]}cp -R ${release_path}*.apk ${prod_path}
fi#判断apk目录下是否有文件
if [ "$(ls -A $prod_path)" ]; thenecho "=============== APK包已导出:$prod_path ==============="open $prod_path
elseecho '=============== APK包导出失败 ==============='exit 1
fi
exit 0
查看脚本可以看出
控制是否执行flutter clean
输入是全部渠道打包
打包后的apk拷贝到prod/apk文件夹下。
通过cd切换到shell目录下,执行apk.sh脚本进行多渠道打包
./shell/papk.sh
在prod/apk目录下,可以看到打包的apk
参考:https://github.com/sugood/flutter_shell
五、更改友盟渠道
在文中,使用友盟时候,需要传递渠道名,我们通过EnvironmentConfig.CHANNEL
拿到渠道名后作为参数传给友盟。
友盟即可根据渠道进行统计。
六、附录(完整的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
}
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'
}def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}/// 获取渠道参数使用,这里设置一下默认值
def dartEnvironmentVariables = [CHANNEL: 'GuanFang',
]if (project.hasProperty('dart-defines')) {dartEnvironmentVariables = dartEnvironmentVariables + project.property('dart-defines').split(',').collectEntries { entry ->def pair = new String(entry.decodeBase64(), 'UTF-8').split('=')[(pair.first()): pair.last()]}
}ext {publishName = 'AppDemoLab'
}apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"android {compileSdkVersion 34ndkVersion flutter.ndkVersioncompileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility 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 21targetSdkVersion flutter.targetSdkVersionversionCode flutterVersionCode.toInteger()versionName flutterVersionNamendk {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}_${dartEnvironmentVariables.CHANNEL}.apk"}}
}flutter {source '../..'
}
七、小结
flutter开发实战-实现多渠道打包及友盟统计(亲测有效),根据自身需求调整后亲测有效,可以根据渠道来做一些代码上的区分。
学习记录,每天不停进步。
本文地址:https://brucegwo.blog.csdn.net/article/details/138907985
快快微信扫码玩一玩小游戏吧
战机长空 | 小绳套牛 |
---|---|
这篇关于flutter开发实战-实现多渠道打包及友盟统计(亲测有效)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!