7

For some reasons, my app can be installed with different apk names. At launch, I would like to know what is the name of the apk file. Do you know how to do that ?

Thanks !

toto_tata
  • 14,526
  • 27
  • 108
  • 198
  • 2
    better is to try some [search](http://stackoverflow.com/questions/5841161/get-application-name-from-package-name/5841353#5841353) before posting a question – Adil Soomro Sep 30 '11 at 09:09
  • 4
    Thanks Adil but it doesn't answer my question. I want to get the name of the apk file which has been copied to the device, not the application name or package name. Any idea on how to do that ? – toto_tata Sep 30 '11 at 11:28
  • Any news on this? – W.M. Feb 28 '20 at 09:03

5 Answers5

5
final PackageManager pm = getPackageManager();
String apkName = "example.apk";
String fullPath = Environment.getExternalStorageDirectory() + "/" + apkName;        
PackageInfo info = pm.getPackageArchiveInfo(fullPath, 0);

you can got it with:

info.versionCod

and

info.versionName

hope this help you

mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
2

The apk or app name can be retrieved using package manager. According to alex's answer here - https://stackoverflow.com/a/30348666/2026280

Assuming you have the path of the apk file.

public static String getAppLabel(PackageManager pm, String pathToApk) {  
PackageInfo packageInfo = pm.getPackageArchiveInfo(pathToApk, 0);

if (Build.VERSION.SDK_INT >= 8) {
    // those two lines do the magic:
    packageInfo.applicationInfo.sourceDir = pathToApk;
    packageInfo.applicationInfo.publicSourceDir = pathToApk;
}

CharSequence label = pm.getApplicationLabel(packageInfo.applicationInfo);
return label != null ? label.toString() : null;
}
Community
  • 1
  • 1
Sandeep R
  • 2,284
  • 3
  • 25
  • 51
1

try this:

ActivityManager actMngr = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
string runningPkg = actMngr.getRunningTasks(1).get(0).topActivity.getPackageName();

PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(runningPkg , 0);

Added:

If you are not getting the apk name then you can compare it with intalled pkg and get the corresponding app name. You can fetch the installed app's pkg name and app name like this:

ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
PackageManager pm = ctx.getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);

for(int i=0;i<packs.size();i++) {
    PackageInfo p = packs.get(i);
    String description = (String) p.applicationInfo.loadDescription(pm);
    String  label= p.applicationInfo.loadLabel(pm).toString();
//Continue to extract other info about the app...
}

Note: Add this permission to the manifest file:

<uses-permission android:name="android.permission.GET_TASKS" />
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • 1
    i think this give only package name not apk name – Nikunj Patel Sep 30 '11 at 09:22
  • it is difficult to get apk name because it rename at sign time – Nikunj Patel Sep 30 '11 at 09:23
  • totally different apk name and package name. – Nikunj Patel Sep 30 '11 at 09:46
  • what you will set only that you will get corresponding to pkg name. – Vineet Shukla Sep 30 '11 at 09:47
  • i don't get you! what you say? – Nikunj Patel Sep 30 '11 at 09:48
  • Hi Vineet. I initilly thought that your code works but it is actually not the case. Indeed, if I change the name of my apk, the properties of applicationInfo variable remains unchanged: ai.dataDir remains unchanged, ai.packageName remains unchanged, ai.sourceDir remains unchanged... Any solution ? – toto_tata Sep 30 '11 at 11:12
  • 1
    Yes. What I can see when exploring the content of the phone on which I ran the app is that the apk name is changed to "pkg.apk" whatever the apk name is. Consequently, it seems not possible to know what was the name of the apk before installation. This "pkg.apk" file is installed in /mnt/asec/package_name I have the feeling that getting the original name of the apk before installation is not possible... Tell me if you find a solution. – toto_tata Sep 30 '11 at 13:07
  • @Regis_AG, year is `2020` any news on this? – W.M. Feb 28 '20 at 09:02
0

This is achieved with getPackageArchiveInfo from an already packaged apk file:

  PackageInfo package_info = pm.getPackageArchiveInfo(file_path_to_apk, 0);
Secko
  • 7,664
  • 5
  • 31
  • 37
0

Try this it work for me:

String versionName = "null"; //String apk versionName
                int versionCode = -1; //int versionCode
                String APKFilePath = "/storage/emulated/0/apkname.apk"; //For example
                PackageManager pm = getPackageManager();
                PackageInfo    packageInfo = pm.getPackageArchiveInfo(APKFilePath, 0);
                // the secret are these two lines
                packageInfo.applicationInfo.sourceDir       = APKFilePath;
                packageInfo.applicationInfo.publicSourceDir = APKFilePath;
                Drawable APKicon = packageInfo.applicationInfo.loadIcon(pm); //drawable apk icon
                String   AppName = (String)packageInfo.applicationInfo.loadLabel(pm); // String apk name
                    versionName = packageInfo.versionName; //String version name
                    versionCode = packageInfo.versionCode; //int version code
                    apk_icon.setImageDrawable(APKicon);    //ImageView apk icon
                    Apkpackage.setText(packageInfo.packageName); //TextView apk package name
                    Apkname.setText(AppName); //TextView apk name
                    Apkversion.setText(String.format("Version Name: %s Version Code:%d", versionName, versionCode)); //TextView versionName and versionCode