MCPcopy Create free account
hub / github.com/afkT/DevUtils / getVersionCode

Method getVersionCode

lib/DevApp/src/main/java/dev/utils/app/ADBUtils.java:229–257  ·  view source on GitHub ↗

获取 APP versionCode @param packageName 应用包名 @return versionCode

(final String packageName)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

execCmdMethod · 0.95
eTagMethod · 0.95
isEmptyMethod · 0.45
splitMethod · 0.45
equalsIgnoreCaseMethod · 0.45
parseIntMethod · 0.45

Tested by

no test coverage detected