【HarmonyOS】关于鸿蒙原生多环境的设置和管理
开发语言:ArkTs开发工具:DevEco Studio 5.0.0 Release
API版本:API 12
demo演示Gitee:harmony-multiEnv.git
在开发中为了数据隔离和开发规范,一般环境下都必要设置多环境,方便开发、测试、部署,比如:dev、test、sit、gray、release等,差别公司在多环境利用上不尽雷同,本文利用dev、test、release三个环境演示鸿蒙的多环境。
一、开发工具设置多环境
1、环境切换入口
在DevEco Studio开发工具的工具条中,点击瞄准镜图标即可进行环境的切换。
https://i-blog.csdnimg.cn/direct/932f2c9b1503418d9915e72496cf1dcb.png
2、设置签名(debug和release)
打开工具栏File - Project Structure - Project - Signing Configs,默认工程只有一个default签名设置,可点击+或-添加删除设置,签名设置完成后,在工程目次下的build-profile.json5文件中可查看signingConfigs字段的签名设置。
本文演示设置debug和release两个签名。
https://i-blog.csdnimg.cn/direct/9da13b27f50c4db4bc95e077a8e740dc.png
{
"app": {
"signingConfigs": [
{
"name": "debug",
"type": "HarmonyOS",
"material": {
"certpath": "./debug.cer",
"storePassword": "xxxx",
"keyAlias": "debugKey",
"keyPassword": "xxxx",
"profile": "./debug.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "./debug.p12"
}
},
{
"name": "release",
"type": "HarmonyOS",
"material": {
"certpath": "./release.cer",
"storePassword": "xxxx",
"keyAlias": "debugKey",
"keyPassword": "xxxx",
"profile": "./release.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "./release.p12"
}
}
],
"products": [
//...
],
"buildModeSet": [
//...
]
},
"modules": [
//...
]
}
3、创建多环境设置文件
在entry/src/main/resources/rawfile目次下创建三个环境对应的设置文件,取名为app_config_dev.json、app_config_test.json和app_config_release.json。
每个设置文件包含雷同的三个字段:
ServerUrl表现请求服务器地点、ResourceUrl表现资源服务器地点、release表现是否为生产环境。
https://i-blog.csdnimg.cn/direct/3acbe5f192be433cad9794deb028af5f.png
{
"ServerUrl": "https://www.dev.com/api/",
"ResourceUrl": "https://www.dev.com/resource/",
"release": false
}
4、设置工程支持多环境
[*] 设置products
在工程目次下的build-profile.json5文件中设置products字段,可在默认的数据中新增arkOptions - buildProfileFields - APP_CONFIG字段,用来设置每个环境对应的设置文件。APP_CONFIG为自界说,可自行改名。
[*] 设置modules
在工程目次下的build-profile.json5文件中设置modules字段,在默认的数据targets - applyToProducts中新增步骤1中的各个环境。
https://i-blog.csdnimg.cn/direct/408b5d061407490ba1f581d53691be89.png
{
"app": {
"signingConfigs": [
// ...
],
"products": [
{
"name": "default",
"signingConfig": "debug",
"compatibleSdkVersion": "5.0.0(12)",
"runtimeOS": "HarmonyOS",
"buildOption": {
"strictMode": {
"caseSensitiveCheck": true,
"useNormalizedOHMUrl": true
},
"arkOptions": {
"buildProfileFields": {
"APP_CONFIG": "app_config_release.json"
}
}
}
},
{
"name": "dev",
"signingConfig": "debug",
"compatibleSdkVersion": "5.0.0(12)",
"runtimeOS": "HarmonyOS",
"buildOption": {
"strictMode": {
"caseSensitiveCheck": true,
"useNormalizedOHMUrl": true
},
"arkOptions": {
"buildProfileFields": {
"APP_CONFIG": "app_config_dev.json"
}
}
}
},
{
"name": "test",
"signingConfig": "debug",
"compatibleSdkVersion": "5.0.0(12)",
"runtimeOS": "HarmonyOS",
"buildOption": {
"strictMode": {
"caseSensitiveCheck": true,
"useNormalizedOHMUrl": true
},
"arkOptions": {
"buildProfileFields": {
"APP_CONFIG": "app_config_test.json"
}
}
}
},
{
"name": "release",
"signingConfig": "release",
"compatibleSdkVersion": "5.0.0(12)",
"runtimeOS": "HarmonyOS",
"buildOption": {
"strictMode": {
"caseSensitiveCheck": true,
"useNormalizedOHMUrl": true
},
"arkOptions": {
"buildProfileFields": {
"APP_CONFIG": "app_config_prod.json"
}
}
}
}
],
"buildModeSet": [
// ...
]
},
"modules": [
{
"name": "entry",
"srcPath": "./entry",
"targets": [
{
"name": "default",
"applyToProducts": [
"default",
"dev",
"test",
"release"
]
}
]
}
]
}
5、获取当前环境设置信息
创建AppConfig.ets文件,用来获取环境设置文件,并剖析文件数据。BuildProfile为利用开发工具切换环境后,build工程生成的编译文件,用来获取当前环境下的环境设置信息。
https://i-blog.csdnimg.cn/direct/c09d1491f7e246cb844489db247e9861.png
import BuildProfile from 'BuildProfile'
import { HashMap } from '@kit.ArkTS';
export class AppConfig {
// 单例
private static config: AppConfig;
static getInstance(): AppConfig {
if (AppConfig.config === undefined) {
AppConfig.config = new AppConfig();
}
return AppConfig.config;
}
// 请求服务器地址
public baseServerUrl: string = '';
// 资源服务器地址
public resourceServerUrl: string = '';
// 是否为生产环境
public isRelease: boolean = true;
// 记录context,后面方法使用
private context?: Context;
/**
* 初始化方法
*/
init(context: Context, filePath?: string): void {
try {
this.context = context;
// 配置文件路径(从环境配置中获取)
let configFilePath: string = filePath ?? BuildProfile.APP_CONFIG;
// 解析配置文件
let bytes: Uint8Array = context.resourceManager.getRawFileContentSync(configFilePath);
let content = this.bytesToString(bytes);
let jsonObj: object = JSON.parse(content);
// 重新组装map对象
let configMap: HashMap<string, string|boolean> = new HashMap();
let keys: string[] = Object.keys(jsonObj);
keys.forEach(key => {
let value: string|boolean = jsonObj;
configMap.set(key, value);
})
// 获取配置文件字段值
this.baseServerUrl = configMap.get("ServerUrl") as string;
this.resourceServerUrl = configMap.get("ResourceUrl") as string;
this.isRelease = configMap.get("release") as boolean;
} catch (e) {
}
}
/**
* Uint8Array类型转换String
* @param input bytes数据
* @returns 返回字符串
*/
public static bytesToString(input: Uint8Array): string {
let output: string = '';
try {
let textDecoder = util.TextDecoder.create("utf-8",{ignoreBOM: true});
output = textDecoder.decodeToString(input , {stream: false});
} catch (err) {}
return output;
}
}
6、生效当前环境信息
本文利用AbilityStage作为初始化环境信息的类,关于AbilityStage的先容可查看官方文档先容。
[*]创建HMAbilityStage.ets类,继续AbilityStage,重写onCreate()方法。
[*]在entry/src/main/module.json5文件中的设置srcEntry字段值为HMAbilityStage.ets文件的相对路径。
https://i-blog.csdnimg.cn/direct/26e8c540826d40fabcf04e99e957947e.png
{
"module": {
"name": "entry",
"type": "entry",
"srcEntry": "./ets/stage/HMAbilityStage.ets",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone",
"tablet",
"2in1"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
// ...
}
}
二、APP内动态切换环境
创建环境切换页面,用于切换差别环境,切换环境时再次调用AppConfig的初始化方法,传入指定设置文件路径即可。
AppConfig.getInstance().init(getContext(this), 'app_config_test.json')
示例中未进行数据长期化记录上次选中的环境,真正项目中可本地存储环境,下次启动利用切换后的环境设置信息
https://i-blog.csdnimg.cn/direct/cd1849320ef946e3a54f855e554b4e63.png
结尾
如各人发现文章形貌有问题或有更好的方案,还请批评回复,一起探究学习,感谢!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]