lgtkbz
lgtkbz

Reputation: 217

Android - How to get any application version by package name

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

Upvotes: 1

Views: 7707

Answers (1)

lgtkbz
lgtkbz

Reputation: 217

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();
    }

Upvotes: 6

Related Questions