1

I want to do some stuff in my own android application where I need to check some system applications versions before. (System & 3rd party apps)

How to do it in Android Studio?

In ADB I can dump it by:

adb shell dumpsys package com.samsung.android.calendar | grep versionName

but how to do it in code?

note: it's not duplicated question, none of solutions provided in comments works for me. I want to check ANY app version, not MY app

lgtkbz
  • 217
  • 3
  • 11
  • 1
    Hint: PackageManager – Marcin Orlowski Jun 11 '18 at 10:20
  • 3
    Possible duplicate of [How to get the build/version number of your Android application?](https://stackoverflow.com/questions/4616095/how-to-get-the-build-version-number-of-your-android-application) – Rohit5k2 Jun 11 '18 at 10:26
  • Cared to search? Already answered. One answer explains how to get the version of another application https://stackoverflow.com/questions/4616095/how-to-get-the-build-version-number-of-your-android-application – Rohit5k2 Jun 11 '18 at 10:26
  • what error are you getting? – Naveen Tamrakar Jun 11 '18 at 10:30
  • we are able to get versionName and versionCode using this Commend adb shell dumpsys package packgeName – Naveen Tamrakar Jun 11 '18 at 10:31
  • Please try adb shell dumpsys package com.google.android.gm – Naveen Tamrakar Jun 11 '18 at 10:34
  • I saw it but none of this command works for me. I want to check ANY application versin, not MY application in this 'solution': https://stackoverflow.com/questions/4616095/how-to-get-the-build-version-number-of-your-android-application I have error 'no such method' in methods: getPackageManager() and getPackageName() + I want to use it in UIAutomator module for my app, so it's 'androidTest' app, not Main application (my.app.package.TEST) – lgtkbz Jun 11 '18 at 10:49

1 Answers1

6

Prolem solved, getPackageManager was not working because of lack of context:

try {
        Context context = InstrumentationRegistry.getContext();
        PackageManager pm = context.getPackageManager();
        PackageInfo pInfo = pm.getPackageInfo("com.google.android.apps.photos", 0);
        String version = pInfo.versionName;
        Log.d(TAG, "checkVersion.DEBUG: App version: "+version);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
lgtkbz
  • 217
  • 3
  • 11