获取 APP versionCode @param packageName 应用包名 @return versionCode
(final String packageName)
| 227 | * @return versionCode |
| 228 | */ |
| 229 | public static int getVersionCode(final String packageName) { |
| 230 | if (TextUtils.isEmpty(packageName)) return 0; |
| 231 | try { |
| 232 | // 执行 shell |
| 233 | ShellUtils.CommandResult result = ShellUtils.execCmd( |
| 234 | "dumpsys package " + packageName + " | grep version", |
| 235 | true |
| 236 | ); |
| 237 | if (result.isSuccessWithSuccessOutput()) { |
| 238 | String[] arrays = result.successMsg.split(DevFinal.REGEX.SPACE); |
| 239 | for (String value : arrays) { |
| 240 | if (!TextUtils.isEmpty(value)) { |
| 241 | try { |
| 242 | String[] splitArray = value.split("="); |
| 243 | if (splitArray.length == 2) { |
| 244 | if ("versionCode".equalsIgnoreCase(splitArray[0])) { |
| 245 | return Integer.parseInt(splitArray[1]); |
| 246 | } |
| 247 | } |
| 248 | } catch (Exception ignored) { |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } catch (Exception e) { |
| 254 | LogPrintUtils.eTag(TAG, e, "getVersionCode"); |
| 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * 获取 APP versionName |
nothing calls this directly
no test coverage detected