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中
完美؏؏☝ᖗ乛◡乛ᖘ☝؏؏