uniapp离线打包:将uniapp中的版本号动态配置到Android

       uniapp开发安卓移动端,采用离线打包的时候,我们需要同时在uniapp的项目中、android项目同时去配置版本号,就特别的麻烦!o(╥﹏╥)o   同时android中的版本号不得不进行配置,不配置上架各平台的时候就有所限制。

       花了两天时间,研究了一波只在uniapp中配置版本号然后同步到android中!(〃'▽'〃)

步骤如下:

1. 在uniapp的manifest.json文件中配置 versionCode(String)、versionName(String)

2. npm run build:app-plus 打包uniapp项目。我这儿做了一个配置,直接将打包之后的文件放置在android中,platform目录与src同级

    "build:app-plus": "cross-env NODE_ENV=production UNI_PLATFORM=app-plus UNI_OUTPUT_DIR=platform/android/app/src/main/assets/apps/xxxdclound appid/www/ vue-cli-service uni-build",

3.  找到android下的build.gradle  "platform\android\app\build.gradle"

4. 找到打包后的打包后manifest.json  “platform/android/app/src/main/assets/apps/xxxdclound appid/www/manifest.json”

5. 在build.gradle中获取打包后manifest.json数据。与android同级写一个获取json文件的方法

// 获取uniappbuild之后的manifest.json中的版本信息
JSONObject getAppVersion() {
    try {
        File file = new File("${rootDir}/app/src/main/assets/apps/xxxdclound appid/www/manifest.json")
        FileReader fileReader = new FileReader(file);
        String line = null;
        StringBuilder sb = new StringBuilder()
        while ( (line = fileReader.readLine()) != null){
            sb.append(line)
        }
        fileReader.close()
        JSONObject jsonObject = new JSONObject(sb.toString())
        JSONObject version = jsonObject.getJSONObject("version")
        print("app version=${version}")
        return version
    } catch (IOException | JSONException e) {
        e.printStackTrace()
    }
    return null

}

6. android 下调用,同时给versionCode(int)、versionName(string)赋值

android{
    JSONObject json = getAppVersion()
    String v_code = json.getString("code")
    String v_name = json.getString("name")

    defaultConfig {
        versionCode v_code.toInteger()
        versionName v_name
    }
}

7. 这样,就可以只在manifest.json中配置一次版本号就可以同步到android中

完美؏؏☝ᖗ乛◡乛ᖘ☝؏؏

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值