HarmonyOS鸿蒙Next中如何将本地特有的环境属性提取到local.properties,避免硬编码敏感信息到项目代码里?

HarmonyOS鸿蒙Next中如何将本地特有的环境属性提取到local.properties,避免硬编码敏感信息到项目代码里? 如何将本地特有的环境属性(比如存放签名文件的路径)提取到 loccal.properties?使不用硬编码敏感信息到代码里。

每个人的本地环境属性不一样,比如 hwsdk.dir

hvigor 构建的项目里,如何在 hvigor 脚本和各种 build-profile.json5,引用 local.prepotes 里自定义的属性?

比如我不想硬编码个人的签名路径到 build-profile.json5,如何将这些信息导出到 local.prepotes,但又不影响构建功能?类似 Android Studio 的 gradle 可以读 local.prepotes 里自定义的属性。


更多关于HarmonyOS鸿蒙Next中如何将本地特有的环境属性提取到local.properties,避免硬编码敏感信息到项目代码里?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

可能需要的是获取自定义编译参数:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ide-hvigor-get-build-profile-para-guide-0000001759228286。关于提取签名文件:

  1. 建json文件存放签名信息,例如data.json内容:
{
  "certpath": ".\.ohos\config\default.cer",
  "storePassword": "*******",
  "keyAlias": "debugKey",
  "keyPassword": "*******",
  "profile": ".\.ohos\config\default.p7b",
  "signAlg": "SHA256withECDSA",
  "storeFile": ".\.ohos\config\default.p12"
}
  1. 删除build-profile.json5中signingConfigs信息

  2. 通过在hvigorfile.ts里使用函数方法,动态配置签名材料 //工程级别hvigorfile.ts

import { appTasks } from '@ohos/hvigor-ohos-plugin';
import * as data from "./data.json"

export default {
  system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
  plugins: [], /* Custom plugin to extend the functionality of Hvigor. */
  config: {
    ohos: {
      overrides:{
        signingConfig: getSigningConfig(), //签名配置对象
        appOpt: {
          versionCode: getVersionCode(),
          versionName: getVersionName(),
        } //app.json中的内容
      }
    }
  }
}

function getSigningConfig() {
  return {
    type: "HarmonyOS",
    material: {
      certpath: data.certpath,
      storePassword: data.storePassword,
      keyAlias: data.keyAlias,
      keyPassword: data.keyPassword,
      profile: data.profile,
      signAlg: data.signAlg,
      storeFile: data.storeFile
    }
  }
}

function getVersionCode() {
  return 100000;
}

function getVersionName() {
  return "1";
}
  1. 此时,build-profile.json5中不需要再配置签名信息,可以将data.json加入.gitignore,构建任务已经调用本地data.json中的信息。

更多关于HarmonyOS鸿蒙Next中如何将本地特有的环境属性提取到local.properties,避免硬编码敏感信息到项目代码里?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,提取本地特有的环境属性到local.properties文件,可以通过以下步骤实现:

  1. 创建local.properties文件:在项目的根目录下创建一个名为local.properties的文件。这个文件通常用于存储本地特有的配置,如API密钥、数据库连接信息等。

  2. 定义环境属性:在local.properties文件中,定义你需要的环境属性。例如:

    api_key=your_api_key_here
    database_url=your_database_url_here
    
  3. 读取local.properties文件:在HarmonyOS项目中,可以使用Properties类来读取local.properties文件中的属性。首先,确保在build.gradle文件中添加对Properties类的支持。

  4. 在代码中使用环境属性:通过Properties类读取local.properties文件中的属性,并在代码中使用这些属性。例如:

    Properties properties = new Properties()
    properties.load(new FileInputStream("local.properties"))
    String apiKey = properties.getProperty("api_key")
    String databaseUrl = properties.getProperty("database_url")
    
  5. 确保local.properties不被版本控制:为了避免敏感信息被提交到版本控制系统中,将local.properties文件添加到.gitignore文件中。这样,local.properties文件不会出现在版本控制中,从而保护敏感信息。

通过以上步骤,你可以将本地特有的环境属性提取到local.properties文件中,避免硬编码敏感信息到项目代码里。

在HarmonyOS鸿蒙Next中,可以通过以下步骤将本地特有的环境属性提取到local.properties文件中:

  1. 创建local.properties文件:在项目根目录下创建local.properties文件。

  2. 定义环境属性:在local.properties中定义敏感信息或环境变量,例如:

    apiKey=your_api_key_here
    
  3. build.gradle中读取属性:通过Properties类读取local.properties文件中的值:

    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def apiKey = properties.getProperty('apiKey')
    
  4. 使用属性:在代码中通过BuildConfiggradle配置使用这些属性,避免硬编码。

通过这种方式,可以确保敏感信息不直接暴露在代码中,提高安全性。

回到顶部